Consider the following code:
fromtypingimportSizedisinstance(1, Sized)
This works well and returns False, as expected.
However, if I create a new protocol that subclass Sized:
fromtyping_extensionsimportProtocolclassMySized(Protocol, Sized):
passisinstance(1, Sized)
This now raises the following exception:
Details
TypeErrorTraceback (mostrecentcalllast)
<ipython-input-5-2516c5c4af5f>in<module>---->1isinstance(1, Sized)
.../lib/python3.8/typing.pyin__instancecheck__(self, obj)
764765def__instancecheck__(self, obj):
-->766returnself.__subclasscheck__(type(obj))
767768def__subclasscheck__(self, cls):
.../lib/python3.8/typing.pyin__subclasscheck__(self, cls)
769ifself._special:
770ifnotisinstance(cls, _GenericAlias):
-->771returnissubclass(cls, self.__origin__)
772ifcls._special:
773returnissubclass(cls.__origin__, self.__origin__)
.../lib/python3.8/abc.pyin__subclasscheck__(cls, subclass)
100def__subclasscheck__(cls, subclass):
101"""Override for issubclass(subclass, cls)."""-->102return_abc_subclasscheck(cls, subclass)
103104def_dump_registry(cls, file=None):
.../lib/python3.8/site-packages/typing_extensions.pyin__subclasscheck__(cls, other)
581"Protocols with non-method members don't support issubclass()"582 )
-->583returnsuper().__subclasscheck__(other)
584585def__instancecheck__(cls, instance):
.../lib/python3.8/abc.pyin__subclasscheck__(cls, subclass)
100def__subclasscheck__(cls, subclass):
101"""Override for issubclass(subclass, cls)."""-->102return_abc_subclasscheck(cls, subclass)
103104def_dump_registry(cls, file=None):
.../lib/python3.8/site-packages/typing_extensions.pyin_proto_hook(cls, other)
637if_allow_reckless_class_checks():
638returnNotImplemented-->639raiseTypeError("Instance and class checks can only be used with"640" @runtime_checkable protocols")
641TypeError: Instanceandclasscheckscanonlybeusedwith @runtime_checkableprotocolsThis non-local behavior did not happen in prior versions, and has caused problems in my code (GAA-UAM/scikit-fda#540).
Consider the following code:
This works well and returns
False, as expected.However, if I create a new protocol that subclass
Sized:This now raises the following exception:
Details
This non-local behavior did not happen in prior versions, and has caused problems in my code (GAA-UAM/scikit-fda#540).