-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-comprehensions
] Handle builtins at top of file correctly fo…
…r `unnecessary-dict-comprehension-for-iterable` (`C420`) (#15837) Builtin bindings are given a range of `0..0`, which causes strange behavior when range checks are made at the top of the file. In this case, the logic of the rule demands that the value of the dict comprehension is not self-referential (i.e. it does not contain definitions for any of the variables used within it). This logic was confused by builtins which looked like they were defined "in the comprehension", if the comprehension appeared at the top of the file. Closes #15830
- Loading branch information
Showing
4 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C420_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,7 @@ | ||
{x: NotImplemented for x in "XY"} | ||
|
||
|
||
# Builtin bindings are placed at top of file, but should not count as | ||
# an "expression defined within the comprehension". So the above | ||
# should trigger C420 | ||
# See https://github.com/astral-sh/ruff/issues/15830 |
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
16 changes: 16 additions & 0 deletions
16
...ehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C420_C420_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,16 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs | ||
--- | ||
C420_1.py:1:1: C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead | ||
| | ||
1 | {x: NotImplemented for x in "XY"} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C420 | ||
| | ||
= help: Replace with `dict.fromkeys(iterable)`) | ||
|
||
ℹ Safe fix | ||
1 |-{x: NotImplemented for x in "XY"} | ||
1 |+dict.fromkeys("XY", NotImplemented) | ||
2 2 | | ||
3 3 | | ||
4 4 | # Builtin bindings are placed at top of file, but should not count as |