Use TypeChecker for detecting fastapi routes (#15093)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

This commit is contained in:
TomerBin 2024-12-21 16:45:28 +02:00 committed by GitHub
parent fd4bea52e5
commit 2fb6b320d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 88 additions and 11 deletions

View file

@ -786,6 +786,35 @@ impl TypeChecker for PathlibPathChecker {
}
}
pub struct FastApiRouteChecker;
impl FastApiRouteChecker {
fn is_fastapi_route_constructor(semantic: &SemanticModel, expr: &Expr) -> bool {
let Some(qualified_name) = semantic.resolve_qualified_name(expr) else {
return false;
};
matches!(
qualified_name.segments(),
["fastapi", "FastAPI" | "APIRouter"]
)
}
}
impl TypeChecker for FastApiRouteChecker {
fn match_annotation(annotation: &Expr, semantic: &SemanticModel) -> bool {
Self::is_fastapi_route_constructor(semantic, annotation)
}
fn match_initializer(initializer: &Expr, semantic: &SemanticModel) -> bool {
let Expr::Call(ast::ExprCall { func, .. }) = initializer else {
return false;
};
Self::is_fastapi_route_constructor(semantic, func)
}
}
pub struct TypeVarLikeChecker;
impl TypeVarLikeChecker {
@ -914,6 +943,10 @@ pub fn is_pathlib_path(binding: &Binding, semantic: &SemanticModel) -> bool {
check_type::<PathlibPathChecker>(binding, semantic)
}
pub fn is_fastapi_route(binding: &Binding, semantic: &SemanticModel) -> bool {
check_type::<FastApiRouteChecker>(binding, semantic)
}
/// Test whether the given binding is for an old-style `TypeVar`, `TypeVarTuple` or a `ParamSpec`.
pub fn is_type_var_like(binding: &Binding, semantic: &SemanticModel) -> bool {
check_type::<TypeVarLikeChecker>(binding, semantic)