-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Enable Ruff SIM #13309
base: main
Are you sure you want to change the base?
Enable Ruff SIM #13309
Conversation
This comment has been minimized.
This comment has been minimized.
if sys.version_info >= (3, 11): | ||
OP_IGNORE_UNEXPECTED_EOF: int | ||
elif sys.version_info >= (3, 8) and sys.platform == "linux": | ||
if sys.version_info >= (3, 11) or sys.platform == "linux": |
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 feel these mostly make the code less readable. |
This comment has been minimized.
This comment has been minimized.
I kinda like SIM110 using |
I would revert the changes related to if-else-block-instead-of-if-exp (SIM108) and ignore that one. I just don't really agree that it's always an improvement to use a ternary expression instead of explicit I don't mind most of the other changes, but the first In general this category has quite a few opinionated rules, and I'm not sure I'd want the whole category enabled. I think I'd probably want to think explicitly about which rules make useful, uncontroversial suggestions and which don't. |
Wholly agree with you Alex. I'm a fan of ternaries in other languages, but Python formatting and the need for parenthesis or For the sake of discussion, I'm offering an alternative to the |
This comment has been minimized.
This comment has been minimized.
scripts/stubsabot.py
Outdated
return False | ||
return True | ||
return all( | ||
# fmt: skip |
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 is this needed? This looks like how Black would format the code anyway.
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.
With our 130 line length black puts the whole thing on a single line. Which greatly affected readability. (Black and Ruff don't tend to have great vertical formatting for ternaries and comprehensions when the line length is long).
For the sake of discussion about the "reimplemented-builtin (SIM110)" rule, I worked around this formatting issue
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.
If you need to manually format the result, that seems like an argument against applying this "simplification" rule.
This comment has been minimized.
This comment has been minimized.
@@ -410,93 +410,6 @@ if sys.platform == "win32": | |||
"WindowsSelectorEventLoopPolicy", # from windows_events | |||
"WindowsProactorEventLoopPolicy", # from windows_events | |||
) | |||
elif sys.version_info >= (3, 10): |
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.
Apparently the 3.9 and 3.10 branches were duplicate (manual fix)
This comment has been minimized.
This comment has been minimized.
pyproject.toml
Outdated
@@ -46,6 +46,7 @@ select = [ | |||
"N", # pep8-naming | |||
"PGH", # pygrep-hooks | |||
"RUF", # Ruff-specific and unused-noqa | |||
"SIM", # flake8-simplify |
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 still think I'd rather explicitly list the rules we think are useful here, rather than enabling the whole category and disabling specific rules. This category is just too hit-and-miss for me: some of the rules are pretty clear wins, but quite a few make some very opinionated suggestions that have high false-positive rates. Even if some of those rules don't have any false positives on typeshed today, there's a chance we might add code in the future that they'd emit false positives on, or there's a chance that Ruff might stabilise new SIM
rules that have false positives on typeshed code.
If lint rules end up irritating us (or confusing contributors) more than they actually help us write better code, then it defeats the purpose of a linter, in my opinion :-)
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.
Updated to be more specific about rules selection
I listed the rules I didn't select in the PR's description
"SIM201", # Use `{left} != {right}` instead of not `{left} == {right}` | ||
"SIM202", # Use `{left} == {right}` instead of not `{left} != {right}` | ||
"SIM208", # Use `{expr}` instead of `not (not {expr})` | ||
"SIM210", # Remove unnecessary `True if ... else False` | ||
"SIM211", # Use `not ...` instead of `False if ... else True` | ||
"SIM212", # Use `{expr_else} if {expr_else} else {expr_body}` instead of `{expr_body} if not {expr_else} else {expr_else}` | ||
"SIM220", # Use `False` instead of `{name} and not {name}` | ||
"SIM221", # Use `True` instead of `{name} or not {name}` | ||
"SIM222", # Use `{expr}` instead of `{replaced}` | ||
"SIM223", # Use `{expr}` instead of `{replaced}` |
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.
Could be
"SIM201", # Use `{left} != {right}` instead of not `{left} == {right}` | |
"SIM202", # Use `{left} == {right}` instead of not `{left} != {right}` | |
"SIM208", # Use `{expr}` instead of `not (not {expr})` | |
"SIM210", # Remove unnecessary `True if ... else False` | |
"SIM211", # Use `not ...` instead of `False if ... else True` | |
"SIM212", # Use `{expr_else} if {expr_else} else {expr_body}` instead of `{expr_body} if not {expr_else} else {expr_else}` | |
"SIM220", # Use `False` instead of `{name} and not {name}` | |
"SIM221", # Use `True` instead of `{name} or not {name}` | |
"SIM222", # Use `{expr}` instead of `{replaced}` | |
"SIM223", # Use `{expr}` instead of `{replaced}` | |
"SIM2", # flake8-simplify conditional ordering rules |
but as asked I explicitly selected them individually
This comment has been minimized.
This comment has been minimized.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Ref #13295
https://docs.astral.sh/ruff/rules/#flake8-simplify-sim
Most rules are autofixable
Noting the rules we didn't select, and why, for future references:
About suppressible-exception (SIM105):