From c28135173201fd546f85676fd834d81340b64f95 Mon Sep 17 00:00:00 2001 From: Katherine Whitlock Date: Fri, 21 Feb 2025 14:02:55 -0500 Subject: [PATCH] Finish up transition from black-format to ruff (#8294) --- .devcontainer/devcontainer.json | 9 +- .github/workflows/matchers/lint-python.json | 4 +- pyproject.toml | 6 +- script/lint-python | 113 ++++++++++++-------- 4 files changed, 76 insertions(+), 56 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 8d9565ad5f..86f35cc47b 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -31,7 +31,7 @@ "ms-python.python", "ms-python.pylint", "ms-python.flake8", - "ms-python.black-formatter", + "charliermarsh.ruff", "visualstudioexptteam.vscodeintellicode", // yaml "redhat.vscode-yaml", @@ -49,14 +49,11 @@ "flake8.args": [ "--config=${workspaceFolder}/.flake8" ], - "black-formatter.args": [ - "--config", - "${workspaceFolder}/pyproject.toml" - ], + "ruff.configuration": "${workspaceFolder}/pyproject.toml", "[python]": { // VS will say "Value is not accepted" before building the devcontainer, but the warning // should go away after build is completed. - "editor.defaultFormatter": "ms-python.black-formatter" + "editor.defaultFormatter": "charliermarsh.ruff" }, "editor.formatOnPaste": false, "editor.formatOnSave": true, diff --git a/.github/workflows/matchers/lint-python.json b/.github/workflows/matchers/lint-python.json index 6a09f04770..6f750f209a 100644 --- a/.github/workflows/matchers/lint-python.json +++ b/.github/workflows/matchers/lint-python.json @@ -1,11 +1,11 @@ { "problemMatcher": [ { - "owner": "black", + "owner": "ruff", "severity": "error", "pattern": [ { - "regexp": "^(.*): (Please format this file with the black formatter)", + "regexp": "^(.*): (Please format this file with the ruff formatter)", "file": 1, "message": 2 } diff --git a/pyproject.toml b/pyproject.toml index 7789f6d645..5cdf4a77b5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,10 +51,6 @@ version = {attr = "esphome.const.__version__"} [tool.setuptools.packages.find] include = ["esphome*"] -[tool.black] -target-version = ["py39", "py310"] -exclude = 'generated' - [tool.pytest.ini_options] testpaths = [ "tests", @@ -108,6 +104,8 @@ expected-line-ending-format = "LF" [tool.ruff] required-version = ">=0.5.0" +target-version = "py39" +exclude = ['generated'] [tool.ruff.lint] select = [ diff --git a/script/lint-python b/script/lint-python index 01e5e76190..c9f1789160 100755 --- a/script/lint-python +++ b/script/lint-python @@ -19,7 +19,7 @@ curfile = None def print_error(file, lineno, msg): - global curfile + global curfile # noqa: PLW0603 if curfile != file: 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}") +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(): colorama.init() @@ -69,61 +85,70 @@ def main(): errors = 0 - cmd = ["black", "--verbose"] + ([] if args.apply else ["--check"]) + files - print("Running black...") - print() - log = get_err(*cmd) - for line in log.splitlines(): - WOULD_REFORMAT = "would reformat" - if line.startswith(WOULD_REFORMAT): - file_ = line[len(WOULD_REFORMAT) + 1 :] - print_error(file_, None, "Please format this file with the black formatter") - errors += 1 + # Needed to get around command-line string limits in Windows. + filesets = split_args_platform_compatible(files) + + print("Running ruff...") + print() + for fileset in filesets: + cmd = ["ruff", "format"] + ([] if args.apply else ["--check"]) + fileset + log = get_err(*cmd) + for line in log.splitlines(): + WOULD_REFORMAT = "would reformat" + if line.startswith(WOULD_REFORMAT): + file_ = line[len(WOULD_REFORMAT) + 1 :] + print_error( + file_, None, "Please format this file with the ruff formatter" + ) + errors += 1 - cmd = ["flake8"] + files print() print("Running flake8...") print() - log = get_output(*cmd) - for line in log.splitlines(): - line = line.split(":", 4) - if len(line) < 4: - continue - file_ = line[0] - linno = line[1] - msg = (":".join(line[3:])).strip() - print_error(file_, linno, msg) - errors += 1 + for files in filesets: + cmd = ["flake8"] + files + log = get_output(*cmd) + for line in log.splitlines(): + line = line.split(":", 4) + if len(line) < 4: + continue + file_ = line[0] + linno = line[1] + msg = (":".join(line[3:])).strip() + print_error(file_, linno, msg) + errors += 1 - cmd = ["pylint", "-f", "parseable", "--persistent=n"] + files print() print("Running pylint...") print() - log = get_output(*cmd) - for line in log.splitlines(): - line = line.split(":", 3) - if len(line) < 3: - continue - file_ = line[0] - linno = line[1] - msg = (":".join(line[2:])).strip() - print_error(file_, linno, msg) - errors += 1 + for files in filesets: + cmd = ["pylint", "-f", "parseable", "--persistent=n"] + files + log = get_output(*cmd) + for line in log.splitlines(): + line = line.split(":", 3) + if len(line) < 3: + continue + file_ = line[0] + linno = line[1] + msg = (":".join(line[2:])).strip() + print_error(file_, linno, msg) + errors += 1 - PYUPGRADE_TARGET = "--py39-plus" - cmd = ["pyupgrade", PYUPGRADE_TARGET] + files print() print("Running pyupgrade...") print() - log = get_err(*cmd) - for line in log.splitlines(): - REWRITING = "Rewriting" - if line.startswith(REWRITING): - file_ = line[len(REWRITING) + 1 :] - print_error( - file_, None, f"Please run pyupgrade {PYUPGRADE_TARGET} on this file" - ) - errors += 1 + PYUPGRADE_TARGET = "--py39-plus" + for files in filesets: + cmd = ["pyupgrade", PYUPGRADE_TARGET] + files + log = get_err(*cmd) + for line in log.splitlines(): + REWRITING = "Rewriting" + if line.startswith(REWRITING): + file_ = line[len(REWRITING) + 1 :] + print_error( + file_, None, f"Please run pyupgrade {PYUPGRADE_TARGET} on this file" + ) + errors += 1 sys.exit(errors)