Avoid no-self-use for attrs-style validators (#13166)

## Summary

Closes https://github.com/astral-sh/ruff/issues/12568.
This commit is contained in:
Charlie Marsh 2024-08-30 12:39:05 -04:00 committed by GitHub
parent 34dafb67a2
commit a73bebcf15
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 51 additions and 2 deletions

View file

@ -1,4 +1,4 @@
use ruff_python_ast::{self as ast, Decorator};
use ruff_python_ast::{self as ast, Decorator, Expr};
use ruff_python_ast::helpers::map_callable;
use ruff_python_ast::name::{QualifiedName, UnqualifiedName};
@ -90,6 +90,27 @@ where
})
}
/// Returns `true` if a function definition is an `attrs`-like validator based on its decorators.
pub fn is_validator(decorator_list: &[Decorator], semantic: &SemanticModel) -> bool {
decorator_list.iter().any(|decorator| {
let Expr::Attribute(ast::ExprAttribute { value, attr, .. }) = &decorator.expression else {
return false;
};
if attr.as_str() != "validator" {
return false;
}
let Expr::Name(value) = value.as_ref() else {
return false;
};
semantic
.resolve_name(value)
.is_some_and(|id| semantic.binding(id).kind.is_assignment())
})
}
/// Returns `true` if a class is an `final`.
pub fn is_final(decorator_list: &[Decorator], semantic: &SemanticModel) -> bool {
decorator_list