-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[flake8-simplify] Detect unnecessary None default for additional key expression types (SIM910)
#20343
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
Merged
Merged
[flake8-simplify] Detect unnecessary None default for additional key expression types (SIM910)
#20343
Changes from 2 commits
Commits
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
This file contains hidden or 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
32 changes: 32 additions & 0 deletions
32
crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM910_preview.py
This file contains hidden or 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,32 @@ | ||
| # Method call as key | ||
| ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| key_source = {"Thomas": "Tom"} | ||
| age = ages.get(key_source.get("Thomas", "Tom"), None) | ||
|
|
||
| # Property access as key | ||
| class Data: | ||
| def __init__(self): | ||
| self.name = "Tom" | ||
|
|
||
| data = Data() | ||
| ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| age = ages.get(data.name, None) | ||
|
|
||
| # Complex expression as key | ||
| ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| key = "Tom" if True else "Maria" | ||
| age = ages.get(key, None) | ||
|
|
||
| # Function call as key | ||
| def get_key(): | ||
| return "Tom" | ||
|
|
||
| ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| age = ages.get(get_key(), None) | ||
|
|
||
| # OK - these should not trigger even in preview mode | ||
| ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| age = ages.get("Tom") # No default value | ||
|
|
||
| ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| age = ages.get("Tom", "Unknown") # Non-None default | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
81 changes: 81 additions & 0 deletions
81
...pshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM910_SIM910_preview.py.snap
This file contains hidden or 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,81 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs | ||
| --- | ||
| SIM910 [*] Use `ages.get(key_source.get("Thomas", "Tom"))` instead of `ages.get(key_source.get("Thomas", "Tom"), None)` | ||
| --> SIM910_preview.py:4:7 | ||
| | | ||
| 2 | ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| 3 | key_source = {"Thomas": "Tom"} | ||
| 4 | age = ages.get(key_source.get("Thomas", "Tom"), None) | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 5 | | ||
| 6 | # Property access as key | ||
| | | ||
| help: Replace `ages.get(key_source.get("Thomas", "Tom"), None)` with `ages.get(key_source.get("Thomas", "Tom"))` | ||
| 1 | # Method call as key | ||
| 2 | ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| 3 | key_source = {"Thomas": "Tom"} | ||
| - age = ages.get(key_source.get("Thomas", "Tom"), None) | ||
| 4 + age = ages.get(key_source.get("Thomas", "Tom")) | ||
| 5 | | ||
| 6 | # Property access as key | ||
| 7 | class Data: | ||
|
|
||
| SIM910 [*] Use `ages.get(data.name)` instead of `ages.get(data.name, None)` | ||
| --> SIM910_preview.py:13:7 | ||
| | | ||
| 11 | data = Data() | ||
| 12 | ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| 13 | age = ages.get(data.name, None) | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 14 | | ||
| 15 | # Complex expression as key | ||
| | | ||
| help: Replace `ages.get(data.name, None)` with `ages.get(data.name)` | ||
| 10 | | ||
| 11 | data = Data() | ||
| 12 | ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| - age = ages.get(data.name, None) | ||
| 13 + age = ages.get(data.name) | ||
| 14 | | ||
| 15 | # Complex expression as key | ||
| 16 | ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
|
|
||
| SIM910 [*] Use `ages.get(key)` instead of `ages.get(key, None)` | ||
| --> SIM910_preview.py:18:7 | ||
| | | ||
| 16 | ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| 17 | key = "Tom" if True else "Maria" | ||
| 18 | age = ages.get(key, None) | ||
| | ^^^^^^^^^^^^^^^^^^^ | ||
| 19 | | ||
| 20 | # Function call as key | ||
| | | ||
| help: Replace `ages.get(key, None)` with `ages.get(key)` | ||
| 15 | # Complex expression as key | ||
| 16 | ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| 17 | key = "Tom" if True else "Maria" | ||
| - age = ages.get(key, None) | ||
| 18 + age = ages.get(key) | ||
| 19 | | ||
| 20 | # Function call as key | ||
| 21 | def get_key(): | ||
|
|
||
| SIM910 [*] Use `ages.get(get_key())` instead of `ages.get(get_key(), None)` | ||
| --> SIM910_preview.py:25:7 | ||
| | | ||
| 24 | ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| 25 | age = ages.get(get_key(), None) | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 26 | | ||
| 27 | # OK - these should not trigger even in preview mode | ||
| | | ||
| help: Replace `ages.get(get_key(), None)` with `ages.get(get_key())` | ||
| 22 | return "Tom" | ||
| 23 | | ||
| 24 | ages = {"Tom": 23, "Maria": 23, "Dog": 11} | ||
| - age = ages.get(get_key(), None) | ||
| 25 + age = ages.get(get_key()) | ||
| 26 | | ||
| 27 | # OK - these should not trigger even in preview mode | ||
| 28 | ages = {"Tom": 23, "Maria": 23, "Dog": 11} |
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.
This is actually just another
Expr::Nameas key since it's assigned to a variable. Even the if expression itself is only a singleExpr::If, but this is fine either way!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 inlined this too while I was at it)