Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class C: a = None
dict.fromkeys("abc") # OK
print(C.a)

x = [None]
dict.fromkeys("abc") # OK
print(x)

class C(list):
def __getitem__(self, index, /):
item = super().__getitem__(index)
if isinstance(index, slice): item = tuple(item)
return item
x = C()
dict.fromkeys("abc") # OK
print(x)

# C420: side-effecting assignment targets
# See https://github.com/astral-sh/ruff/issues/19511
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 @@ -25,6 +25,7 @@ mod tests {
#[test_case(Rule::UnnecessaryDictComprehensionForIterable, Path::new("C420.py"))]
#[test_case(Rule::UnnecessaryDictComprehensionForIterable, Path::new("C420_1.py"))]
#[test_case(Rule::UnnecessaryDictComprehensionForIterable, Path::new("C420_2.py"))]
#[test_case(Rule::UnnecessaryDictComprehensionForIterable, Path::new("C420_3.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 @@ -96,6 +96,14 @@ pub(crate) fn unnecessary_dict_comprehension_for_iterable(
return;
}

// Don't suggest `dict.fromkeys` if the target is an attribute, subscript, or slice (side-effecting assignment).
match &generator.target {
Expr::Attribute(_) | Expr::Subscript(_) | Expr::Slice(_) => {
return;
}
_ => {}
}

// Don't suggest `dict.fromkeys` if the value is not a constant or constant-like.
if !is_constant_like(dict_comp.value.as_ref()) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs
---

Loading