[flake8-pyi] Also remove self and cls's annotation (PYI034) (#14801)

Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
This commit is contained in:
InSync 2024-12-09 21:59:12 +07:00 committed by GitHub
parent 0e9427255f
commit aa6b812a73
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 785 additions and 73 deletions

View file

@ -778,6 +778,40 @@ impl TypeChecker for PathlibPathChecker {
}
}
pub struct TypeVarLikeChecker;
impl TypeVarLikeChecker {
/// Returns `true` if an [`Expr`] is a `TypeVar`, `TypeVarTuple`, or `ParamSpec` call.
///
/// See also [`ruff_linter::rules::flake8_pyi::rules::simple_defaults::is_type_var_like_call`].
fn is_type_var_like_call(expr: &Expr, semantic: &SemanticModel) -> bool {
let Expr::Call(ast::ExprCall { func, .. }) = expr else {
return false;
};
let Some(qualified_name) = semantic.resolve_qualified_name(func) else {
return false;
};
matches!(
qualified_name.segments(),
[
"typing" | "typing_extensions",
"TypeVar" | "TypeVarTuple" | "ParamSpec"
]
)
}
}
impl TypeChecker for TypeVarLikeChecker {
fn match_annotation(_annotation: &Expr, _semantic: &SemanticModel) -> bool {
false
}
fn match_initializer(initializer: &Expr, semantic: &SemanticModel) -> bool {
Self::is_type_var_like_call(initializer, semantic)
}
}
/// Test whether the given binding can be considered a list.
///
/// For this, we check what value might be associated with it through it's initialization and
@ -867,6 +901,11 @@ pub fn is_pathlib_path(binding: &Binding, semantic: &SemanticModel) -> bool {
check_type::<PathlibPathChecker>(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)
}
/// Find the [`ParameterWithDefault`] corresponding to the given [`Binding`].
#[inline]
fn find_parameter<'a>(