mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Respect __str__
definitions from super classes (#9338)
Closes https://github.com/astral-sh/ruff/issues/9242.
This commit is contained in:
parent
cea2ec8dd0
commit
1f9353fed3
9 changed files with 111 additions and 49 deletions
|
@ -6,8 +6,8 @@ use ruff_python_ast::helpers::map_subscript;
|
|||
|
||||
use crate::{BindingId, SemanticModel};
|
||||
|
||||
/// Return `true` if any base class of a class definition matches a predicate.
|
||||
pub fn any_over_body(
|
||||
/// Return `true` if any base class matches a [`CallPath`] predicate.
|
||||
pub fn any_call_path(
|
||||
class_def: &ast::StmtClassDef,
|
||||
semantic: &SemanticModel,
|
||||
func: &dyn Fn(CallPath) -> bool,
|
||||
|
@ -55,3 +55,45 @@ pub fn any_over_body(
|
|||
|
||||
inner(class_def, semantic, func, &mut FxHashSet::default())
|
||||
}
|
||||
|
||||
/// Return `true` if any base class matches an [`ast::StmtClassDef`] predicate.
|
||||
pub fn any_super_class(
|
||||
class_def: &ast::StmtClassDef,
|
||||
semantic: &SemanticModel,
|
||||
func: &dyn Fn(&ast::StmtClassDef) -> bool,
|
||||
) -> bool {
|
||||
fn inner(
|
||||
class_def: &ast::StmtClassDef,
|
||||
semantic: &SemanticModel,
|
||||
func: &dyn Fn(&ast::StmtClassDef) -> bool,
|
||||
seen: &mut FxHashSet<BindingId>,
|
||||
) -> bool {
|
||||
// If the function itself matches the pattern, then this does too.
|
||||
if func(class_def) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Otherwise, check every base class.
|
||||
class_def.bases().iter().any(|expr| {
|
||||
// If the base class extends a class that matches the pattern, then this does too.
|
||||
if let Some(id) = semantic.lookup_attribute(map_subscript(expr)) {
|
||||
if seen.insert(id) {
|
||||
let binding = semantic.binding(id);
|
||||
if let Some(base_class) = binding
|
||||
.kind
|
||||
.as_class_definition()
|
||||
.map(|id| &semantic.scopes[*id])
|
||||
.and_then(|scope| scope.kind.as_class())
|
||||
{
|
||||
if inner(base_class, semantic, func, seen) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
})
|
||||
}
|
||||
|
||||
inner(class_def, semantic, func, &mut FxHashSet::default())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue