Skip to content

Commit

Permalink
[flake8-comprehensions] Handle builtins at top of file correctly fo…
Browse files Browse the repository at this point in the history
…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
dylwil3 authored Jan 30, 2025
1 parent 5c77898 commit 4f2aea8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
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
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod tests {
#[test_case(Rule::UnnecessaryComprehensionInCall, Path::new("C419.py"))]
#[test_case(Rule::UnnecessaryComprehensionInCall, Path::new("C419_2.py"))]
#[test_case(Rule::UnnecessaryDictComprehensionForIterable, Path::new("C420.py"))]
#[test_case(Rule::UnnecessaryDictComprehensionForIterable, Path::new("C420_1.py"))]
#[test_case(Rule::UnnecessaryDoubleCastOrProcess, Path::new("C414.py"))]
#[test_case(Rule::UnnecessaryGeneratorDict, Path::new("C402.py"))]
#[test_case(Rule::UnnecessaryGeneratorList, Path::new("C400.py"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ pub(crate) fn unnecessary_dict_comprehension_for_iterable(

let binding = checker.semantic().binding(id);

// Builtin bindings have a range of 0..0, and are never
// defined within the comprehension, so we abort before
// checking the range overlap below. Note this only matters
// if the comprehension appears at the top of the file!
if binding.kind.is_builtin() {
return false;
}

dict_comp.range().contains_range(binding.range())
});
if self_referential {
Expand Down
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

0 comments on commit 4f2aea8

Please sign in to comment.