Skip to content

Commit

Permalink
Iterate over lambdas in deferred type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Dec 18, 2023
1 parent c944d23 commit 7918bd9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import re
from typing import Annotated

type X = Annotated[int, lambda: re.compile("x")]
10 changes: 7 additions & 3 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ where
range: _,
},
) => {
println!("Lambda: {:?}", lambda);
// Visit the default arguments, but avoid the body, which will be deferred.
if let Some(parameters) = parameters {
for ParameterWithDefault {
Expand Down Expand Up @@ -1205,6 +1206,7 @@ where
self.visit_expr(expr);
}
for expr in iter {
println!("Visiting non-type definition: {:?}", expr);
self.visit_non_type_definition(expr);
}
self.visit_expr_context(ctx);
Expand Down Expand Up @@ -2013,13 +2015,15 @@ pub(crate) fn check_ast(
// Iterate over the AST.
checker.visit_body(python_ast);

// Visit any deferred syntax nodes.
// Visit any deferred syntax nodes. Take care to visit in order, such that we avoid adding
// new deferred nodes after visiting nodes of that kind. For example, visiting a deferred
// function can add a deferred lambda, but the opposite is not true.
checker.visit_deferred_functions();
checker.visit_deferred_lambdas();
checker.visit_deferred_future_type_definitions();
checker.visit_deferred_type_param_definitions();
checker.visit_deferred_future_type_definitions();
let allocator = typed_arena::Arena::new();
checker.visit_deferred_string_type_definitions(&allocator);
checker.visit_deferred_lambdas();
checker.visit_exports();

// Check docstrings, bindings, and unresolved references.
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/pyflakes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ mod tests {
#[test_case(Rule::UnusedImport, Path::new("F401_17.py"))]
#[test_case(Rule::UnusedImport, Path::new("F401_18.py"))]
#[test_case(Rule::UnusedImport, Path::new("F401_19.py"))]
#[test_case(Rule::UnusedImport, Path::new("F401_20.py"))]
#[test_case(Rule::ImportShadowedByLoopVar, Path::new("F402.py"))]
#[test_case(Rule::UndefinedLocalWithImportStar, Path::new("F403.py"))]
#[test_case(Rule::LateFutureImport, Path::new("F404.py"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---

0 comments on commit 7918bd9

Please sign in to comment.