diff --git a/crates/ruff_linter/resources/test/fixtures/pyflakes/F821_24.py b/crates/ruff_linter/resources/test/fixtures/pyflakes/F821_24.py new file mode 100644 index 00000000000000..c6356289138ffb --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pyflakes/F821_24.py @@ -0,0 +1,8 @@ +"""Test for accessing class members within a generator.""" + + +class Class: + items = [] + + if len(replacements := {item[1] for item in items}) > 1: + pass diff --git a/crates/ruff_linter/src/checkers/ast/mod.rs b/crates/ruff_linter/src/checkers/ast/mod.rs index 2cc13787d731b6..d408e49078b518 100644 --- a/crates/ruff_linter/src/checkers/ast/mod.rs +++ b/crates/ruff_linter/src/checkers/ast/mod.rs @@ -1748,7 +1748,8 @@ impl<'a> Checker<'a> { if self .semantic .current_expressions() - .any(Expr::is_named_expr_expr) + .filter_map(Expr::as_named_expr_expr) + .any(|parent| parent.target.as_ref() == expr) { self.add_binding(id, expr.range(), BindingKind::NamedExprAssignment, flags); return; diff --git a/crates/ruff_linter/src/rules/pyflakes/mod.rs b/crates/ruff_linter/src/rules/pyflakes/mod.rs index 814dcb3d743ecb..721e7dceb72563 100644 --- a/crates/ruff_linter/src/rules/pyflakes/mod.rs +++ b/crates/ruff_linter/src/rules/pyflakes/mod.rs @@ -142,6 +142,7 @@ mod tests { #[test_case(Rule::UndefinedName, Path::new("F821_21.py"))] #[test_case(Rule::UndefinedName, Path::new("F821_22.ipynb"))] #[test_case(Rule::UndefinedName, Path::new("F821_23.py"))] + #[test_case(Rule::UndefinedName, Path::new("F821_24.py"))] #[test_case(Rule::UndefinedExport, Path::new("F822_0.py"))] #[test_case(Rule::UndefinedExport, Path::new("F822_1.py"))] #[test_case(Rule::UndefinedExport, Path::new("F822_2.py"))] diff --git a/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F821_F821_24.py.snap b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F821_F821_24.py.snap new file mode 100644 index 00000000000000..d0b409f39ee0ba --- /dev/null +++ b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F821_F821_24.py.snap @@ -0,0 +1,4 @@ +--- +source: crates/ruff_linter/src/rules/pyflakes/mod.rs +--- +