>>> from typing import Sized
>>> isinstance(1, Sized)
False
>>> from typing import Sized, Protocol
>>> class Foo(Sized, Protocol): pass
...
>>> isinstance(1, Sized)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1153, in __instancecheck__
return self.__subclasscheck__(type(obj))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1428, in __subclasscheck__
return issubclass(cls, self.__origin__)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen abc>", line 123, in __subclasscheck__
File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1793, in __subclasscheck__
return super().__subclasscheck__(other)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen abc>", line 123, in __subclasscheck__
File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1876, in _proto_hook
raise TypeError("Instance and class checks can only be used with"
TypeError: Instance and class checks can only be used with @runtime_checkable protocols
>>> import typing
>>> isinstance(1, typing.Sized)
False
>>> class Foo(typing.Sized, typing.Protocol): pass
...
>>> isinstance(1, typing.Sized)
False
On
main, anisinstance()check againstSizedworks just the same as it does on Python 3.11:However! If you first subclass
Sizedlike this,TypeErroris raised on thatisinstance()check!This was originally reported by @vnmabus in python/typing_extensions#207.
Note that (because of the
abc-module cache), this doesn't reproduce if you do anisinstance()check before subclassingtyping.Sized:Linked PRs
_ProtocolMeta.__subclasscheck__#105152_ProtocolMeta.__subclasscheck__(GH-105152) #105160