forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[flake8_comprehensions] add sum/min/max to unnecessary comprehension …
…check (C419) (astral-sh#10759) Fixes astral-sh#3259 ## Summary Renames `UnnecessaryComprehensionAnyAll` to `UnnecessaryComprehensionInCall` and extends the check to `sum`, `min`, and `max`, in addition to `any` and `all`. ## Test Plan Updated snapshot test. Built docs locally and verified the docs for this rule still render correctly.
- Loading branch information
Showing
12 changed files
with
178 additions
and
72 deletions.
There are no files selected for viewing
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
8 changes: 8 additions & 0 deletions
8
crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C419_1.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
sum([x.val for x in bar]) | ||
min([x.val for x in bar]) | ||
max([x.val for x in bar]) | ||
|
||
# Ok | ||
sum(x.val for x in bar) | ||
min(x.val for x in bar) | ||
max(x.val for x in bar) |
3 changes: 3 additions & 0 deletions
3
crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C419_2.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# no lint if shadowed | ||
def all(x): pass | ||
all([x.id for x in bar]) |
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
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
4 changes: 4 additions & 0 deletions
4
...ehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C419_C419_2.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,4 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs | ||
--- | ||
|
55 changes: 55 additions & 0 deletions
55
.../snapshots/ruff_linter__rules__flake8_comprehensions__tests__preview__C419_C419_1.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,55 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs | ||
--- | ||
C419_1.py:1:5: C419 [*] Unnecessary list comprehension | ||
| | ||
1 | sum([x.val for x in bar]) | ||
| ^^^^^^^^^^^^^^^^^^^^ C419 | ||
2 | min([x.val for x in bar]) | ||
3 | max([x.val for x in bar]) | ||
| | ||
= help: Remove unnecessary list comprehension | ||
|
||
ℹ Unsafe fix | ||
1 |-sum([x.val for x in bar]) | ||
1 |+sum(x.val for x in bar) | ||
2 2 | min([x.val for x in bar]) | ||
3 3 | max([x.val for x in bar]) | ||
4 4 | | ||
|
||
C419_1.py:2:5: C419 [*] Unnecessary list comprehension | ||
| | ||
1 | sum([x.val for x in bar]) | ||
2 | min([x.val for x in bar]) | ||
| ^^^^^^^^^^^^^^^^^^^^ C419 | ||
3 | max([x.val for x in bar]) | ||
| | ||
= help: Remove unnecessary list comprehension | ||
|
||
ℹ Unsafe fix | ||
1 1 | sum([x.val for x in bar]) | ||
2 |-min([x.val for x in bar]) | ||
2 |+min(x.val for x in bar) | ||
3 3 | max([x.val for x in bar]) | ||
4 4 | | ||
5 5 | # Ok | ||
|
||
C419_1.py:3:5: C419 [*] Unnecessary list comprehension | ||
| | ||
1 | sum([x.val for x in bar]) | ||
2 | min([x.val for x in bar]) | ||
3 | max([x.val for x in bar]) | ||
| ^^^^^^^^^^^^^^^^^^^^ C419 | ||
4 | | ||
5 | # Ok | ||
| | ||
= help: Remove unnecessary list comprehension | ||
|
||
ℹ Unsafe fix | ||
1 1 | sum([x.val for x in bar]) | ||
2 2 | min([x.val for x in bar]) | ||
3 |-max([x.val for x in bar]) | ||
3 |+max(x.val for x in bar) | ||
4 4 | | ||
5 5 | # Ok | ||
6 6 | sum(x.val for x in bar) |