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
Since mypy now supports __slots__ checking, we can add __slots__ to stubs.
This will allow us to correctly raise Something is not in __slots__ when assigning extra attributes.
Technically it's not always ignored, it's "ignorable". Aka if the runtime has __slots__, stubtest won't currently enforce that the stub has __slots__. But if the stub does have __slots__, stubtest will make sure they match the runtime.
For your question 2, I think it's best to add protected attributes. That way, we can just have the slots definition match exactly between runtime and the stub, which is easier to test and review.
to correctly raise Something is not in slots when assigning extra attributes.
Can you clarify this? Type checkers complain about assigning non-existent attributes anyway, unless it's a subclass and then the __slots__ of the parent class doesn't matter anyway at runtime (unless the subclass defines its own __slots__, in that case the fields that are actually accessible are Parent.__slots__ + Child.__slots__).
So, let's say we have a definition of SomeStub type in typeshed, which does not have __slots__ defined:
classSomeStub: ... # defined in typeshedclassA(SomeStub): # user's code__slots__= ("b",)
def__init__(self) ->None:
# All three will pass, because `SomeStub` parent has `__dict__`:self.a=1self.b=2self._one=1
Compare it to the next case, where we have __slots__ defined:
classSomeStubWithSlots: # defined in typeshed__slots__= ("a",)
classA(SomeStubWithSlots): # user's code__slots__= ("b",)
def__init__(self) ->None:
self.a=1self.b=2self._one=1# E: Trying to assign name "_one" that is not in "__slots__" of type "ex.A"
I now see how it's useful. It is an improvement, but also not super important because new attributes are usually introduced in __init__, so the user's code would fail every time A is instantiated, not only in some obscure corner case. It's also somewhat rare to use __slots__ in user code.
Activity
AlexWaygood commentedon Oct 3, 2022
It's currently included in stubtest's list of always-ignored dunders inside classes: https://github.com/python/mypy/blob/dc5c299aa190949f2300b163ccc10257a779006d/mypy/stubtest.py#L1280-L1281.
hauntsaninja commentedon Oct 3, 2022
Technically it's not always ignored, it's "ignorable". Aka if the runtime has
__slots__, stubtest won't currently enforce that the stub has__slots__. But if the stub does have__slots__, stubtest will make sure they match the runtime.JelleZijlstra commentedon Oct 3, 2022
I agree that adding
__slots__is useful in stubs.For your question 2, I think it's best to add protected attributes. That way, we can just have the slots definition match exactly between runtime and the stub, which is easier to test and review.
sobolevn commentedon Oct 3, 2022
Ok, I will start with
stubtestPR! 👍Akuli commentedon Oct 3, 2022
Can you clarify this? Type checkers complain about assigning non-existent attributes anyway, unless it's a subclass and then the
__slots__of the parent class doesn't matter anyway at runtime (unless the subclass defines its own__slots__, in that case the fields that are actually accessible areParent.__slots__ + Child.__slots__).AlexWaygood commentedon Oct 3, 2022
As @Akuli says, I don't think adding
__slots__to the stub helps much with whether or not type checkers will validate attribute assignments.Where it might help is in code like this:
Adding
__slots__to the stub could plausibly help type checkers catch both of those exceptions.sobolevn commentedon Oct 4, 2022
Sorry, an example was needed 🙂
Mypy has a special feature to check attribute definitions to be compatible with
__slots__: https://github.com/python/mypy/blob/dc5c299aa190949f2300b163ccc10257a779006d/mypy/checker.py#L3196-L3225Tests can be found here: https://github.com/python/mypy/blob/master/test-data/unit/check-slots.test
So, let's say we have a definition of
SomeStubtype in typeshed, which does not have__slots__defined:Compare it to the next case, where we have
__slots__defined:Akuli commentedon Oct 4, 2022
I now see how it's useful. It is an improvement, but also not super important because new attributes are usually introduced in
__init__, so the user's code would fail every timeAis instantiated, not only in some obscure corner case. It's also somewhat rare to use__slots__in user code.sobolevn commentedon Oct 4, 2022
This is shocking to me :)
So many people ignore this awesome feature for some reason.
JelleZijlstra commentedon Aug 21, 2025
#14611 adds
__slots__to almost all of the standard library. I might do the third-party stubs soon too.donbarbos commentedon Jan 29, 2026
Seems that the discussion has reached a conclusion:
__slots__has been added, and PR #14619 (for third-party stubs) was the final step.