-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Python lint: Use ruff instead of flake8. NFC #23085
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like ruff is indeed an improvement over flake8!
Is there any stuff that flake8 does that ruff does now catch?
test/jsrun.py
Outdated
@@ -106,7 +106,8 @@ def run_js(filename, engine, args=None, | |||
stderr=stderr, | |||
cwd=cwd, | |||
timeout=timeout, | |||
universal_newlines=True) | |||
universal_newlines=True, | |||
check=skip_check) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like maybe a semantic change.. did ruff make this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ruff rule PLW1510 will set check=False
which ignores failures as recently discussed at https://github.com/pyodide/pyodide-build/pull/66/files#r1861265823 We can set check=False
if we always want to ignore errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we simply revert this?
tools/emscripten.py
Outdated
@@ -368,13 +368,13 @@ def emscript(in_wasm, out_wasm, outfile_js, js_syms, finalize=True, base_metadat | |||
# check_call doesn't support the `input` argument. | |||
if asm_consts: | |||
validate = '\n'.join([f'var tmp = {f};' for _, f in asm_consts]) | |||
proc = subprocess.run(config.NODE_JS + ['--check', '-'], input=validate.encode('utf-8')) | |||
proc = subprocess.run(config.NODE_JS + ['--check', '-'], input=validate.encode('utf-8'), check=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this? check is false by default isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default is to ignore errors.
Why is this bad?
By default, subprocess.run
does not check the return code of the process
it runs. This can lead to silent failures.
Instead, consider using check=True
to raise an exception if the process
fails, or set check=False
explicitly to mark the behavior as intentional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think just leaving it out is fine/clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we revert revert this hunk.
Can we stick with |
https://docs.astral.sh/ruff/faq/#how-does-ruffs-linter-compare-to-flake8
I would have migrated |
Lets just go with |
b43b435
to
71f966f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm % comments
Below, the Required |
This PR will not auto-merge because the Required |
@cclauss it looks like lost the check for the correct number of newlines between methods and functions: https://github.com/emscripten-core/emscripten/pull/23139/files#r1883095264 |
https://docs.astral.sh/ruff is an extremely fast Python linter and code formatter, written in Rust. With over 800 linting rules, ruff can be used to replace [Flake8](https://pypi.org/project/flake8/) (plus dozens of plugins), [Black](https://github.com/psf/black), [isort](https://pypi.org/project/isort/), [pydocstyle](https://pypi.org/project/pydocstyle/), [pyupgrade](https://pypi.org/project/pyupgrade/), [autoflake](https://pypi.org/project/autoflake/), and more, all while executing tens or hundreds of times faster than any individual tool.
It looks like we also regressed on the ability to detect trailing whitespace in function calls:
Is this just something we need to enable? |
I guess E202 is in preview too: https://docs.astral.sh/ruff/rules/whitespace-before-close-bracket/. Somewhat worrying that such simple checks are in preview.. and that we have to enable them explicitly on the command line. |
This is solved in #23154. %
Flake8 was created before great code formatters were available but Ruff was not. |
It would have been good to know that before we made the transition to ruff. |
Upgrade to ruff v0.9.1 with 2025 stable format. Required no code changes. https://github.com/astral-sh/ruff/releases Fix #23085 (comment) Add https://docs.astral.sh/ruff/rules/#flake8-pie-pie Add [`ruff rule PLR1714`](https://docs.astral.sh/ruff/rules/repeated-equality-comparison)
Turns out ruff still doesn't support several flake8 warnings. I've enabled some more that are in preview-only mode. See emscripten-core#23085
Turns out ruff still doesn't support several flake8 warnings. I've enabled some more that are in preview-only mode. See #23085
https://docs.astral.sh/ruff is an extremely fast Python linter and code formatter, written in Rust. With over 800 linting rules, ruff can be used to replace Flake8 (plus dozens of plugins), Black, isort, pydocstyle, pyupgrade, autoflake, and more, all while executing tens or hundreds of times faster than any individual tool.
Set some upper limits on code complexity using ruff rules for McCabe cyclomatic complexity and Pylint.
%
git grep flake8
%
flake8-to-ruff .flake8 > pyproject.toml
%
validate-pyproject pyproject.toml
%
pyproject-fmt pyproject.toml
%
ruff check
The Required
ci/circleci: flake8
job was renamed toci/circleci: ruff
so the Required label should be removed from the old job and added to the new one.%
ruff rule C901
complex-structure (C901)
Derived from the mccabe linter.
What it does
Checks for functions with a high
McCabe
complexity.Why is this bad?
The
McCabe
complexity of a function is a measure of the complexity ofthe control flow graph of the function. It is calculated by adding
one to the number of decision points in the function. A decision
point is a place in the code where the program has a choice of two
or more paths to follow.
Functions with a high complexity are hard to understand and maintain.
Example
Use instead:
Options
lint.mccabe.max-complexity