From d35b5248eaa2b31ee7e4cefa90a667b0c507f891 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 24 Jul 2023 17:39:02 -0400 Subject: [PATCH] Tweak lambda rule to use annotations rather than shadowing (#6044) ## Summary This PR ensures that we can retain the current behavior even after we reorder the visitor a bit, by looking for annotated lambdas rather than "is the name bound to anything?", since if we visit the name before we run this rule, it'll _always_ be bound. (This check is already a bit flawed -- in truth, we should probably run this rule deferred so that we can reliably detect shadowing.) --- .../rules/pycodestyle/rules/lambda_assignment.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs b/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs index 9f87bb6073..33beb8b3b1 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs @@ -113,7 +113,11 @@ pub(crate) fn lambda_assignment( // See: https://github.com/astral-sh/ruff/issues/3046 // See: https://github.com/astral-sh/ruff/issues/5421 if (annotation.is_some() && checker.semantic().scope().kind.is_class()) - || checker.semantic().scope().has(id) + || checker + .semantic() + .scope() + .get_all(id) + .any(|binding_id| checker.semantic().binding(binding_id).kind.is_annotation()) { diagnostic.set_fix(Fix::manual(Edit::range_replacement(indented, stmt.range()))); } else { @@ -139,9 +143,9 @@ fn extract_types(annotation: &Expr, semantic: &SemanticModel) -> Option<(Vec Option<(Vec elts.clone(), Expr::Constant(ast::ExprConstant { value: Constant::Ellipsis, @@ -165,9 +169,9 @@ fn extract_types(annotation: &Expr, semantic: &SemanticModel) -> Option<(Vec