-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[pylint
] Include name of base class in message for redefined-slots-in-subclass
(W0244
)
#15559
base: main
Are you sure you want to change the base?
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, thanks for following up quickly on this!
I just have one question about whether to use depth-first or breath-first traversal otherwise this looks good to go.
.filter(|&slot| contained_in_super_slots(class_def, semantic, slot)) | ||
.map(|slot| { | ||
Diagnostic::new( | ||
RedefinedSlotsInSubclass { | ||
name: slot.name.to_string(), | ||
}, | ||
slot.range(), | ||
) | ||
}) | ||
.filter_map(|slot| contained_in_super_slots(class_def, semantic, slot)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should change the function name as it's now returning a Diagnostic
and not a boolean. Maybe check_super_slots
or something similar?
redefined_slots_in_subclass.py:6:18: PLW0244 Redefined slot 'a' in subclass | ||
redefined_slots_in_subclass.py:6:18: PLW0244 Slot `a` redefined from base `Base` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "Slot a
redefined from base class Base
"
/// The traversal of the class hierarchy is breadth-first. | ||
pub fn iter_super_class<'stmt>( | ||
class_def: &'stmt ast::StmtClassDef, | ||
semantic: &'stmt SemanticModel, | ||
) -> impl Iterator<Item = &'stmt ast::StmtClassDef> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for doing breadth-first? I think the original implementation did depth-first traversal.
In the following situation:
the message for
W0244
now specifies thata
is overwriting a slot fromGrandparent
.To implement this, we introduce a helper function
iter_super_classes
which does a breadth-first traversal of the superclasses of a given class (as long as they are defined in the same file, due to the usual limitations of the semantic model).Note: Python does not allow conflicting slots definitions under multiple inheritance. Unless I'm misunderstanding something, I believe It follows that the subposet of superclasses of a given class that redefine a given slot is in fact totally ordered. There is therefore a unique nearest superclass whose slot is being overwritten. So, you know, in case anyone was super worried about that... you can just chill.
This is a followup to #9640 .