-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add autofix for PIE800 #8668
Merged
Merged
Add autofix for PIE800 #8668
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1acd749
Add autofix for PIE800
alanhdu c003bbf
Use BackwardsTokenizer
alanhdu bb30495
Use SimpleTokenizer instead of BackwardTokenizer
alanhdu 0ec74f2
Merge branch 'main' into alan/pie800
charliermarsh 0a728b1
Add some docs, tweaks
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
crates/ruff_linter/resources/test/fixtures/flake8_pie/PIE800.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...lake8_pie/snapshots/ruff_linter__rules__flake8_pie__tests__preview__PIE800_PIE800.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_pie/mod.rs | ||
--- | ||
PIE800.py:1:14: PIE800 [*] Unnecessary spread `**` | ||
| | ||
1 | {"foo": 1, **{"bar": 1}} # PIE800 | ||
| ^^^^^^^^^^ PIE800 | ||
2 | | ||
3 | {**{"bar": 10}, "a": "b"} # PIE800 | ||
| | ||
= help: Remove unnecessary dict | ||
|
||
ℹ Safe fix | ||
1 |-{"foo": 1, **{"bar": 1}} # PIE800 | ||
1 |+{"foo": 1, "bar": 1} # PIE800 | ||
2 2 | | ||
3 3 | {**{"bar": 10}, "a": "b"} # PIE800 | ||
4 4 | | ||
|
||
PIE800.py:3:4: PIE800 [*] Unnecessary spread `**` | ||
| | ||
1 | {"foo": 1, **{"bar": 1}} # PIE800 | ||
2 | | ||
3 | {**{"bar": 10}, "a": "b"} # PIE800 | ||
| ^^^^^^^^^^^ PIE800 | ||
4 | | ||
5 | foo({**foo, **{"bar": True}}) # PIE800 | ||
| | ||
= help: Remove unnecessary dict | ||
|
||
ℹ Safe fix | ||
1 1 | {"foo": 1, **{"bar": 1}} # PIE800 | ||
2 2 | | ||
3 |-{**{"bar": 10}, "a": "b"} # PIE800 | ||
3 |+{"bar": 10, "a": "b"} # PIE800 | ||
4 4 | | ||
5 5 | foo({**foo, **{"bar": True}}) # PIE800 | ||
6 6 | | ||
|
||
PIE800.py:5:15: PIE800 [*] Unnecessary spread `**` | ||
| | ||
3 | {**{"bar": 10}, "a": "b"} # PIE800 | ||
4 | | ||
5 | foo({**foo, **{"bar": True}}) # PIE800 | ||
| ^^^^^^^^^^^^^ PIE800 | ||
6 | | ||
7 | {**foo, **{"bar": 10}} # PIE800 | ||
| | ||
= help: Remove unnecessary dict | ||
|
||
ℹ Safe fix | ||
2 2 | | ||
3 3 | {**{"bar": 10}, "a": "b"} # PIE800 | ||
4 4 | | ||
5 |-foo({**foo, **{"bar": True}}) # PIE800 | ||
5 |+foo({**foo, "bar": True}) # PIE800 | ||
6 6 | | ||
7 7 | {**foo, **{"bar": 10}} # PIE800 | ||
8 8 | | ||
|
||
PIE800.py:7:11: PIE800 [*] Unnecessary spread `**` | ||
| | ||
5 | foo({**foo, **{"bar": True}}) # PIE800 | ||
6 | | ||
7 | {**foo, **{"bar": 10}} # PIE800 | ||
| ^^^^^^^^^^^ PIE800 | ||
8 | | ||
9 | { # PIE800 | ||
| | ||
= help: Remove unnecessary dict | ||
|
||
ℹ Safe fix | ||
4 4 | | ||
5 5 | foo({**foo, **{"bar": True}}) # PIE800 | ||
6 6 | | ||
7 |-{**foo, **{"bar": 10}} # PIE800 | ||
7 |+{**foo, "bar": 10} # PIE800 | ||
8 8 | | ||
9 9 | { # PIE800 | ||
10 10 | "a": "b", | ||
|
||
PIE800.py:12:7: PIE800 [*] Unnecessary spread `**` | ||
| | ||
10 | "a": "b", | ||
11 | # Preserve | ||
12 | **{ | ||
| _______^ | ||
13 | | # all | ||
14 | | "bar": 10, # the | ||
15 | | # comments | ||
16 | | }, | ||
| |_____^ PIE800 | ||
17 | } | ||
| | ||
= help: Remove unnecessary dict | ||
|
||
ℹ Safe fix | ||
9 9 | { # PIE800 | ||
10 10 | "a": "b", | ||
11 11 | # Preserve | ||
12 |- **{ | ||
12 |+ | ||
13 13 | # all | ||
14 14 | "bar": 10, # the | ||
15 |- # comments | ||
16 |- }, | ||
15 |+ # comments, | ||
17 16 | } | ||
18 17 | | ||
19 18 | {**foo, **buzz, **{bar: 10}} # PIE800 | ||
|
||
PIE800.py:19:19: PIE800 [*] Unnecessary spread `**` | ||
| | ||
17 | } | ||
18 | | ||
19 | {**foo, **buzz, **{bar: 10}} # PIE800 | ||
| ^^^^^^^^^ PIE800 | ||
20 | | ||
21 | {**foo, "bar": True } # OK | ||
| | ||
= help: Remove unnecessary dict | ||
|
||
ℹ Safe fix | ||
16 16 | }, | ||
17 17 | } | ||
18 18 | | ||
19 |-{**foo, **buzz, **{bar: 10}} # PIE800 | ||
19 |+{**foo, **buzz, bar: 10} # PIE800 | ||
20 20 | | ||
21 21 | {**foo, "bar": True } # OK | ||
22 22 | | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there any way to restructure this to use
SimpleTokenizer
, i.e., forwards lexing? We've considered removing the backwards lexer in the past since it's quite complex, so I'd prefer to minimize usages if possible.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.
Yeah, I think that should be possible:
**{
, I can lex forward from the end of the previous value in the outer dictionary.}
, I can lex forward from the last value of the inner dictionary.I might have to change the function signature to passi n the whole dict (and not just the keys + values) to handle the case where
{**{"a": "b"}}
case where there's no previous value (in which case I'd lex forward from the dict start).(might not get to this until tomorrow though).
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.
Ok -- I think this should work.
I ended up leaving the formatting fairly ugly -- the more I tried to make it look nice, the less confident I became that I handled all the edge-cases correctly (e.g. handling comments correctly, avoiding double commas, getting the indentation right, etc) so I ended up going for something simple and just letting an autoformatter figure it out afterwards.
I might try and take another crack at having the fix provide better formatted code later.
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.
Thanks for doing a second pass here!