Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange interaction between typing.Protocol and unrelated isinstance() checks #105144

Closed
AlexWaygood opened this issue May 31, 2023 · 4 comments
Closed
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented May 31, 2023

On main, an isinstance() check against Sized works just the same as it does on Python 3.11:

>>> from typing import Sized
>>> isinstance(1, Sized)
False

However! If you first subclass Sized like this, TypeError is raised on that isinstance() check!

>>> 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

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 an isinstance() check before subclassing typing.Sized:

>>> import typing
>>> isinstance(1, typing.Sized)
False
>>> class Foo(typing.Sized, typing.Protocol): pass
...
>>> isinstance(1, typing.Sized)
False

Linked PRs

@AlexWaygood AlexWaygood added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir topic-typing 3.12 bugs and security fixes 3.13 bugs and security fixes labels May 31, 2023
@AlexWaygood
Copy link
Member Author

This bisects to b27fe67

@AlexWaygood AlexWaygood changed the title Strange interaction between typing._BaseGenericAlias and typing.Protocol Strange interaction between typing._BaseGenericAlias and typing.Protocol with isinstance() checks May 31, 2023
@AlexWaygood AlexWaygood changed the title Strange interaction between typing._BaseGenericAlias and typing.Protocol with isinstance() checks Strange interaction between typing.Protocol and unrelated isinstance() checks May 31, 2023
@AlexWaygood
Copy link
Member Author

Also reproduces if you use collections.abc.Sized rather than typing.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

@AlexWaygood AlexWaygood self-assigned this May 31, 2023
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue May 31, 2023
…asses

A more invasive solution for python#105144; perhaps better kept for 3.13 only.
AlexWaygood added a commit that referenced this issue May 31, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 31, 2023
…' to `_ProtocolMeta.__subclasscheck__` (pythonGH-105152)

(cherry picked from commit c05c31d)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@JelleZijlstra
Copy link
Member

With some debugging we found that this is due to code in _abc.c that iterates over all the __subclasses__ of the ABC:

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.

JelleZijlstra pushed a commit that referenced this issue May 31, 2023
…s' to `_ProtocolMeta.__subclasscheck__` (GH-105152) (#105160)

(cherry picked from commit c05c31d)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@AlexWaygood
Copy link
Member Author

This issue is fixed, and Jelle's decided not to pursue #105159, so I'll close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants