Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

dylwil3
Copy link
Collaborator

@dylwil3 dylwil3 commented Jan 17, 2025

In the following situation:

class Grandparent:
  __slots__ = "a"

class Parent(Grandparent): ...

class Child(Parent):
  __slots__ = "a"

the message for W0244 now specifies that a is overwriting a slot from Grandparent.

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 .

@dylwil3 dylwil3 added the diagnostics Related to reporting of diagnostics. label Jan 17, 2025
Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@dhruvmanila dhruvmanila left a 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.

Comment on lines -71 to +72
.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))
Copy link
Member

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`
Copy link
Member

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"

Comment on lines +78 to +82
/// 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> {
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
diagnostics Related to reporting of diagnostics.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants