Implement RUF028 to detect useless formatter suppression comments#9899
Implement RUF028 to detect useless formatter suppression comments#9899snowsignal merged 13 commits intomainfrom
Conversation
Draft Checklist
|
2de4f0a to
4864875
Compare
CodSpeed Performance ReportMerging #9899 will not alter performanceComparing Summary
|
MichaReiser
left a comment
There was a problem hiding this comment.
Uhh, it seems there are many failing tests. I'm not sure what happened. It doesn't seem like you changed much on the formatter side.
MichaReiser
left a comment
There was a problem hiding this comment.
Nice work. You make it look less complicated than it is. There are some perf opportunities and simplifications that we can do to the code. But there are also still a few cases that are not handled correctly which make it difficult to assess if other cases are handled correctly. We should have a look at those.
I played around with it in the playground and found a few cases that need improvement:
def body():
# fmt: off
test
# fmt: on
test2The rule reports that the last fmt: on comment is unnecessary because formatting is already enabled. This is not the case, formatting was disabled
The rule reports that the `fmt: off` in the `if True` body is unused but that's a valid use. One check the rule doesn't cover today but I'm okay leaving to an extension is missing fmt: on comments to re-enable formatting without relying on the automatic re-enabling at the end of a block.
04deb9a to
9b87600
Compare
…esolved, particularly with dangling comments.
…ressed and several new problems are now reported. Edge cases involving dangling comments still need to be handled
…comments when determining suppression state
9b87600 to
dc27643
Compare
…le checks that need contextual information
dc27643 to
dad765d
Compare
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| RUF028 | 2 | 2 | 0 | 0 | 0 |
Formatter (stable)
ℹ️ ecosystem check encountered format errors. (no format changes; 2 project errors)
openai/openai-cookbook (error)
warning: Detected debug build without --no-cache.
error: Failed to read examples/How_to_handle_rate_limits.ipynb: Expected a Jupyter Notebook, which must be internally stored as JSON, but this file isn't valid JSON: trailing comma at line 47 column 4
Formatter (preview)
ℹ️ ecosystem check encountered format errors. (no format changes; 2 project errors)
indico/indico (error)
ruff format --preview
ruff failed
Cause: Rule `S410` was removed and cannot be selected.
openai/openai-cookbook (error)
ruff format --preview
warning: Detected debug build without --no-cache.
error: Failed to read examples/How_to_handle_rate_limits.ipynb: Expected a Jupyter Notebook, which must be internally stored as JSON, but this file isn't valid JSON: trailing comma at line 47 column 4
MichaReiser
left a comment
There was a problem hiding this comment.
I really like how you narrowed down the scope of the rule. This looks good to me. There's one issue around suppression comments in nodes that aren't expressions that we need solving but this is ready otherwise
…mments on function definitions
5e1ea63 to
8fc186e
Compare
…tral-sh#9899) <!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> Fixes astral-sh#6611 ## Summary This lint rule spots comments that are _intended_ to suppress or enable the formatter, but will be ignored by the Ruff formatter. We borrow some functions the formatter uses for determining comment placement / putting them in context within an AST. The analysis function uses an AST visitor to visit each comment and attach it to the AST. It then uses that context to check: 1. Is this comment in an expression? 2. Does this comment have bad placement? (e.g. a `# fmt: skip` above a function instead of at the end of a line) 3. Is this comment redundant? 4. Does this comment actually suppress any code? 5. Does this comment have ambiguous placement? (e.g. a `# fmt: off` above an `else:` block) If any of these are true, a violation is thrown. The reported reason depends on the order of the above check-list: in other words, a `# fmt: skip` comment on its own line within a list expression will be reported as being in an expression, since that reason takes priority. The lint suggests removing the comment as an unsafe fix, regardless of the reason. ## Test Plan A snapshot test has been created.
Fixes #6611
Summary
This lint rule spots comments that are intended to suppress or enable the formatter, but will be ignored by the Ruff formatter.
We borrow some functions the formatter uses for determining comment placement / putting them in context within an AST.
The analysis function uses an AST visitor to visit each comment and attach it to the AST. It then uses that context to check:
# fmt: skipabove a function instead of at the end of a line)# fmt: offabove anelse:block)If any of these are true, a violation is thrown. The reported reason depends on the order of the above check-list: in other words, a
# fmt: skipcomment on its own line within a list expression will be reported as being in an expression, since that reason takes priority.The lint suggests removing the comment as an unsafe fix, regardless of the reason.
Test Plan
A snapshot test has been created.