You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How should structural subtyping work for objects with slots but no variable declarations? #1367
Mypy does not see HasNothingButSlots as a valid subtype of of HasX, so complains about this code. Pyright, however, does see HasNothingButSlots as a valid subtype of HasX, so has no complaints.
Currently, the runtime agrees with mypy: calling isinstance(HasNothingButSlots(), HasX) evaluates to False (due to the fact that the x variable has not been initialised on the instance). If we implement any of the patches discussed in #1363, this will change, and the runtime will agree with pyright: isinstance(HasNothingButSlots(), HasX) will evaluate to True even if the x variable has not been initialised on the instance.
changed the title [-]How should structural subtyping work for objects with slots?[/-][+]How should structural subtyping work for objects with slots but no variable declarations?[/+]on Mar 9, 2023
Activity
[-]How should structural subtyping work for objects with slots?[/-][+]How should structural subtyping work for objects with slots but no variable declarations?[/+]TeamSpen210 commentedon Mar 9, 2023
Perhaps it would be reasonable for mypy to treat this as
x: Any, but also produce a "x needs a type definition" or "Any type inferred" warning?gvanrossum commentedon Mar 10, 2023
I tend to agree with pyright -- surely any realistic class with slots does initialize the slots in
__init__or some other method?AlexWaygood commentedon Mar 12, 2023
The points made here makes sense to me. Seems like the consensus is that pyright's behaviour is preferable — closing as resolved.