1
0
mirror of https://github.com/esphome/esphome.git synced 2025-02-25 14:28:14 +00:00

Finish up transition from black-format to ruff (#8294)

This commit is contained in:
Katherine Whitlock 2025-02-21 14:02:55 -05:00 committed by GitHub
parent 9f603a474f
commit c281351732
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 76 additions and 56 deletions

View File

@ -31,7 +31,7 @@
"ms-python.python", "ms-python.python",
"ms-python.pylint", "ms-python.pylint",
"ms-python.flake8", "ms-python.flake8",
"ms-python.black-formatter", "charliermarsh.ruff",
"visualstudioexptteam.vscodeintellicode", "visualstudioexptteam.vscodeintellicode",
// yaml // yaml
"redhat.vscode-yaml", "redhat.vscode-yaml",
@ -49,14 +49,11 @@
"flake8.args": [ "flake8.args": [
"--config=${workspaceFolder}/.flake8" "--config=${workspaceFolder}/.flake8"
], ],
"black-formatter.args": [ "ruff.configuration": "${workspaceFolder}/pyproject.toml",
"--config",
"${workspaceFolder}/pyproject.toml"
],
"[python]": { "[python]": {
// VS will say "Value is not accepted" before building the devcontainer, but the warning // VS will say "Value is not accepted" before building the devcontainer, but the warning
// should go away after build is completed. // should go away after build is completed.
"editor.defaultFormatter": "ms-python.black-formatter" "editor.defaultFormatter": "charliermarsh.ruff"
}, },
"editor.formatOnPaste": false, "editor.formatOnPaste": false,
"editor.formatOnSave": true, "editor.formatOnSave": true,

View File

@ -1,11 +1,11 @@
{ {
"problemMatcher": [ "problemMatcher": [
{ {
"owner": "black", "owner": "ruff",
"severity": "error", "severity": "error",
"pattern": [ "pattern": [
{ {
"regexp": "^(.*): (Please format this file with the black formatter)", "regexp": "^(.*): (Please format this file with the ruff formatter)",
"file": 1, "file": 1,
"message": 2 "message": 2
} }

View File

@ -51,10 +51,6 @@ version = {attr = "esphome.const.__version__"}
[tool.setuptools.packages.find] [tool.setuptools.packages.find]
include = ["esphome*"] include = ["esphome*"]
[tool.black]
target-version = ["py39", "py310"]
exclude = 'generated'
[tool.pytest.ini_options] [tool.pytest.ini_options]
testpaths = [ testpaths = [
"tests", "tests",
@ -108,6 +104,8 @@ expected-line-ending-format = "LF"
[tool.ruff] [tool.ruff]
required-version = ">=0.5.0" required-version = ">=0.5.0"
target-version = "py39"
exclude = ['generated']
[tool.ruff.lint] [tool.ruff.lint]
select = [ select = [

View File

@ -19,7 +19,7 @@ curfile = None
def print_error(file, lineno, msg): def print_error(file, lineno, msg):
global curfile global curfile # noqa: PLW0603
if curfile != file: if curfile != file:
print_error_for_file(file, None) print_error_for_file(file, None)
@ -31,6 +31,22 @@ def print_error(file, lineno, msg):
print(f"{styled(colorama.Style.BRIGHT, f'{file}:')} {msg}") print(f"{styled(colorama.Style.BRIGHT, f'{file}:')} {msg}")
def split_args_platform_compatible(args):
if os.name == "posix":
return [args]
char_length = 0
argsets = []
for index, arg in enumerate(args):
# Windows is techincally 8191, but we need to leave some room for the command itself
if char_length + len(arg) > 8000:
argsets.append(args[:index])
args = args[index:]
char_length = 0
char_length += len(arg)
return argsets
def main(): def main():
colorama.init() colorama.init()
@ -69,21 +85,28 @@ def main():
errors = 0 errors = 0
cmd = ["black", "--verbose"] + ([] if args.apply else ["--check"]) + files # Needed to get around command-line string limits in Windows.
print("Running black...") filesets = split_args_platform_compatible(files)
print("Running ruff...")
print() print()
for fileset in filesets:
cmd = ["ruff", "format"] + ([] if args.apply else ["--check"]) + fileset
log = get_err(*cmd) log = get_err(*cmd)
for line in log.splitlines(): for line in log.splitlines():
WOULD_REFORMAT = "would reformat" WOULD_REFORMAT = "would reformat"
if line.startswith(WOULD_REFORMAT): if line.startswith(WOULD_REFORMAT):
file_ = line[len(WOULD_REFORMAT) + 1 :] file_ = line[len(WOULD_REFORMAT) + 1 :]
print_error(file_, None, "Please format this file with the black formatter") print_error(
file_, None, "Please format this file with the ruff formatter"
)
errors += 1 errors += 1
cmd = ["flake8"] + files
print() print()
print("Running flake8...") print("Running flake8...")
print() print()
for files in filesets:
cmd = ["flake8"] + files
log = get_output(*cmd) log = get_output(*cmd)
for line in log.splitlines(): for line in log.splitlines():
line = line.split(":", 4) line = line.split(":", 4)
@ -95,10 +118,11 @@ def main():
print_error(file_, linno, msg) print_error(file_, linno, msg)
errors += 1 errors += 1
cmd = ["pylint", "-f", "parseable", "--persistent=n"] + files
print() print()
print("Running pylint...") print("Running pylint...")
print() print()
for files in filesets:
cmd = ["pylint", "-f", "parseable", "--persistent=n"] + files
log = get_output(*cmd) log = get_output(*cmd)
for line in log.splitlines(): for line in log.splitlines():
line = line.split(":", 3) line = line.split(":", 3)
@ -110,11 +134,12 @@ def main():
print_error(file_, linno, msg) print_error(file_, linno, msg)
errors += 1 errors += 1
PYUPGRADE_TARGET = "--py39-plus"
cmd = ["pyupgrade", PYUPGRADE_TARGET] + files
print() print()
print("Running pyupgrade...") print("Running pyupgrade...")
print() print()
PYUPGRADE_TARGET = "--py39-plus"
for files in filesets:
cmd = ["pyupgrade", PYUPGRADE_TARGET] + files
log = get_err(*cmd) log = get_err(*cmd)
for line in log.splitlines(): for line in log.splitlines():
REWRITING = "Rewriting" REWRITING = "Rewriting"