-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[red-knot] Detect classes with incompatible __slots__
being used in multiple inheritance
#14931
Comments
Perhaps the case where this comes up most is with builtin/extension types, that aren't implemented in Python and thus don't use But typeshed does not explicitly apply |
Yeah, I think there's three distinct issues here:
I think these are all quite separate questions; the only one I was attempting to tackle in this issue was question (1). |
Some other problems to consider:
|
Those are great edge cases @InSyncWithFoo! I think for an initial implementation of this check we could easily defer them all however, and only emit the incompatible-slots error for two classes in a bases list if both classes have non-dynamic slots definitions that are definitely bound. We could then open follow-up issues to expand the check and detect issues in more cases. This would make the initial PR easy for us to review and would be in keeping with the principle that we should avoid aim to avoid false positives as much as possible (even if it causes false negatives in some situations) in our initial implementation of red-knot. |
This isn't necessarily a priority for red-knot, but it's something that we'll want to do at some point and it could be easily done now.
Two classes that both have nonempty
__slots__
cannot be used in multiple inheritance. We should detect this and emit a diagnostic for it.We'll probably want to check for this as part of this method here:
ruff/crates/red_knot_python_semantic/src/types/infer.rs
Lines 510 to 520 in 82faa9b
Notes on the semantics:
(1) A class with no
__slots__
can be used in multiple inheritance with a class that has__slots__
(2) Empty
__slots__
are fine in combination with multiple inheritance:(3) But even two classes with equal
__slots__
will fail in multiple inheritance if they are both non-empty:(5) And
__slots__
are inherited by subclasses:The text was updated successfully, but these errors were encountered: