mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
Use TypeChecker for detecting fastapi routes (#15093)
This commit is contained in:
parent
fd4bea52e5
commit
2fb6b320d8
5 changed files with 88 additions and 11 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue