[flake8-comprehensions] Fix false positive for C420 with attribute, subscript, or slice assignment targets (#19513)

## Summary

Fixes #19511
This commit is contained in:
Dan Parizher 2025-08-08 15:02:30 -04:00 committed by GitHub
parent 0095ff4c1a
commit 0ec4801b0d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 63 additions and 0 deletions

View file

@ -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"))]

View file

@ -96,6 +96,12 @@ pub(crate) fn unnecessary_dict_comprehension_for_iterable(
return;
}
// Don't suggest `dict.fromkeys` if the target contains side-effecting expressions
// (attributes, subscripts, or slices).
if contains_side_effecting_sub_expression(&generator.target) {
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;
@ -217,3 +223,12 @@ fn fix_unnecessary_dict_comprehension(value: &Expr, generator: &Comprehension) -
node_index: ruff_python_ast::AtomicNodeIndex::dummy(),
})
}
fn contains_side_effecting_sub_expression(target: &Expr) -> bool {
any_over_expr(target, &|expr| {
matches!(
expr,
Expr::Attribute(_) | Expr::Subscript(_) | Expr::Slice(_)
)
})
}

View file

@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs
---