Repository navigation
Strange interaction between typing.Protocol and unrelated isinstance() checks #105144
Copy link
Copy link
Closed
Labels
3.12only security fixesonly security fixes3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or errorstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory3.12only security fixesonly security fixes3.13only security fixesonly security fixes
on May 31, 2023 This bisects to b27fe67
- changed the title
[-]Strange interaction between `typing._BaseGenericAlias` and `typing.Protocol`[/-][+]Strange interaction between `typing._BaseGenericAlias` and `typing.Protocol` with `isinstance()` checks[/+]on May 31, 2023 - changed the title
[-]Strange interaction between `typing._BaseGenericAlias` and `typing.Protocol` with `isinstance()` checks[/-][+]Strange interaction between `typing.Protocol` and unrelated `isinstance()` checks[/+]on May 31, 2023 Also reproduces if you use
collections.abc.Sizedrather thantyping.Sized:>>> import collections.abc >>> import typing >>> class Foo(collections.abc.Sized, typing.Protocol): pass ... >>> isinstance(1, collections.abc.Sized) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\alexw\coding\cpython\Lib\abc.py", line 119, in __instancecheck__ return _abc_instancecheck(cls, instance) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\alexw\coding\cpython\Lib\abc.py", line 123, in __subclasscheck__ return _abc_subclasscheck(cls, subclass) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1796, in __subclasscheck__ return super().__subclasscheck__(other) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\alexw\coding\cpython\Lib\abc.py", line 123, in __subclasscheck__ return _abc_subclasscheck(cls, subclass) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1879, 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
- added a commit that references this issue
on May 31, 2023 With some debugging we found that this is due to code in _abc.c that iterates over all the
__subclasses__of the ABC:Line 768 in c05c31d
subclasses = PyObject_CallMethod(self, "__subclasses__", NULL); Alex's PR #105152 works around this problem by re-arranging the code in typing.py. In #105159 I propose a fix on the ABC side instead. We should consider merging that PR too to prevent similar problems with other ABC subclasses.
Reacted by sunmy2019 and Alex Waygood- added a commit that references this issue
on May 31, 2023 This issue is fixed, and Jelle's decided not to pursue #105159, so I'll close this
Metadata
Metadata
Assignees
Labels
3.12only security fixesonly security fixes3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
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