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] Implement redefined-slots-in-subclass (W0244) #9640

Merged

Conversation

tsugumi-sys
Copy link
Contributor

@tsugumi-sys tsugumi-sys commented Jan 25, 2024

Summary

Test Plan

cargo test

@tsugumi-sys tsugumi-sys marked this pull request as draft January 25, 2024 10:33
Copy link
Contributor

github-actions bot commented Jan 25, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@tsugumi-sys tsugumi-sys marked this pull request as ready for review January 25, 2024 11:05
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hy @tsugumi-sys. Thanks for working on this rule. It seems that the code currently fails to compile. Would you mind taking a look why that is?

@tsugumi-sys tsugumi-sys requested a review from MichaReiser March 7, 2024 06:11
@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Mar 7, 2024
@tsugumi-sys
Copy link
Contributor Author

@MichaReiser
I fixed the compile error :)

@AlexWaygood AlexWaygood dismissed MichaReiser’s stale review March 22, 2024 18:54

the code now compiles

@MichaReiser MichaReiser added the accepted Ready for implementation label Apr 5, 2024
@dhruvmanila dhruvmanila added the preview Related to preview mode features label Apr 29, 2024
@dhruvmanila dhruvmanila self-assigned this Oct 4, 2024
@dylwil3 dylwil3 force-pushed the impl-redefined-slots-in-subclass-W0244 branch from 35bb539 to 6f83375 Compare December 17, 2024 05:18
@dylwil3
Copy link
Collaborator

dylwil3 commented Dec 17, 2024

@tsugumi-sys I've got your branch up-to-date with main (with as few changes as possible). I also moved the proposed rule to preview.

Would you be interested in revisiting this PR and addressing dhruv's comments? I'd be happy to help if you run into any trouble. Let me know either way, and thanks for implementing this rule!

@tsugumi-sys
Copy link
Contributor Author

@dylwil3
I'm sorry for not getting around to this PR for so long.
I'd like to challenge this PR again :)

@dylwil3
Copy link
Collaborator

dylwil3 commented Dec 17, 2024

I'm sorry for not getting around to this PR for so long.

No need to apologize! Just wanted to check in, not scold 😄 Glad to hear you're interested in revisiting.

Feel free to ping me with any questions or when you're ready for a review, and thanks again!

@dylwil3 dylwil3 force-pushed the impl-redefined-slots-in-subclass-W0244 branch from ffad4b1 to d527a5b Compare January 16, 2025 20:04
@dylwil3 dylwil3 requested a review from dhruvmanila January 16, 2025 20:05
@dylwil3
Copy link
Collaborator

dylwil3 commented Jan 16, 2025

Hey @tsugumi-sys , since there weren't too many changes to make I went ahead and made them. Assuming @dhruvmanila agrees that this fixes the previous concerns, we can get this merged in. Thanks so much for your contribution!

@tsugumi-sys
Copy link
Contributor Author

@dylwil3
Hi, I'm so sorry but I'm extremely busy recently, and it would be helpful if you take over this PR :)

Comment on lines +120 to +124
// This function checks every super class
// but we want every _strict_ super class, hence:
if class_def.name == super_class.name {
return false;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR, this is interesting as it seems like a confusing behavior given the name of the function (any_super_class).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's confusing but I think technically correct since any class is a superclass of itself

>>> class Foo:...
...
>>> issubclass(Foo,Foo)
True

I like your idea of implementing iter_super_class. Then we can leave any_super_class as is, but use iter_super_class with a skip to get this behavior.

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.

Thank you! This looks good

@dylwil3
Copy link
Collaborator

dylwil3 commented Jan 17, 2025

@tsugumi-sys no worries at all! you did the bulk of the work, so not much left on my end. thank you!

@dylwil3 dylwil3 merged commit 5cdac25 into astral-sh:main Jan 17, 2025
21 checks passed
dcreager added a commit that referenced this pull request Jan 17, 2025
* main:
  [red-knot] Inline `SubclassOfType::as_instance_type_of_metaclass()` (#15556)
  [`flake8-comprehensions`] strip parentheses around generators in `unnecessary-generator-set` (`C401`) (#15553)
  [`pylint`] Implement `redefined-slots-in-subclass` (`W0244`) (#9640)
  [`flake8-bugbear`] Do not raise error if keyword argument is present and target-python version is less or equals than 3.9 (`B903`) (#15549)
  [red-knot] `type[T]` is disjoint from `type[S]` if the metaclass of `T` is disjoint from the metaclass of `S` (#15547)
  [red-knot] Pure instance variables declared in class body (#15515)
  Update snapshots of #15507 with new annotated snipetts rendering (#15546)
  [`pylint`] Do not report methods with only one `EM101`-compatible `raise` (`PLR6301`) (#15507)
  Fix unstable f-string formatting for expressions containing a trailing comma (#15545)
  Support `knot.toml` files in project discovery (#15505)
  Add support for configuring knot in `pyproject.toml` files (#15493)
  Fix bracket spacing for single-element tuples in f-string expressions (#15537)
  [`flake8-simplify`] Do not emit diagnostics for expressions inside string type annotations (`SIM222`, `SIM223`) (#15405)
  [`flake8-pytest-style`] Do not emit diagnostics for empty `for` loops (`PT012`, `PT031`) (#15542)
dylwil3 added a commit that referenced this pull request Jan 18, 2025
…-in-subclass` (`W0244`) (#15559)

In the following situation:

```python
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 .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants