Iterate over lambdas in deferred type annotations (#9175)

Closes https://github.com/astral-sh/ruff/issues/9159.
This commit is contained in:
Charlie Marsh 2023-12-17 23:51:23 -05:00 committed by GitHub
parent c944d23053
commit 2643f74a5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 3 deletions

View File

@ -0,0 +1,4 @@
import re
from typing import Annotated
type X = Annotated[int, lambda: re.compile("x")]

View File

@ -2013,13 +2013,15 @@ pub(crate) fn check_ast(
// Iterate over the AST. // Iterate over the AST.
checker.visit_body(python_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_functions();
checker.visit_deferred_lambdas();
checker.visit_deferred_future_type_definitions();
checker.visit_deferred_type_param_definitions(); checker.visit_deferred_type_param_definitions();
checker.visit_deferred_future_type_definitions();
let allocator = typed_arena::Arena::new(); let allocator = typed_arena::Arena::new();
checker.visit_deferred_string_type_definitions(&allocator); checker.visit_deferred_string_type_definitions(&allocator);
checker.visit_deferred_lambdas();
checker.visit_exports(); checker.visit_exports();
// Check docstrings, bindings, and unresolved references. // Check docstrings, bindings, and unresolved references.

View File

@ -52,6 +52,7 @@ mod tests {
#[test_case(Rule::UnusedImport, Path::new("F401_17.py"))] #[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_18.py"))]
#[test_case(Rule::UnusedImport, Path::new("F401_19.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::ImportShadowedByLoopVar, Path::new("F402.py"))]
#[test_case(Rule::UndefinedLocalWithImportStar, Path::new("F403.py"))] #[test_case(Rule::UndefinedLocalWithImportStar, Path::new("F403.py"))]
#[test_case(Rule::LateFutureImport, Path::new("F404.py"))] #[test_case(Rule::LateFutureImport, Path::new("F404.py"))]

View File

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