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

Cannot call issubclass() against typing.Protocol on Python 3.12 #105237

Closed
AlexWaygood opened this issue Jun 2, 2023 · 2 comments
Closed

Cannot call issubclass() against typing.Protocol on Python 3.12 #105237

AlexWaygood opened this issue Jun 2, 2023 · 2 comments
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes topic-typing type-bug An unexpected behavior, bug, or error

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented Jun 2, 2023

Bug report

On Python 3.11, you can do this:

>>> import typing
>>> issubclass(object, typing.Protocol)
False

But on Python 3.12 (following the backport of c05c31d), this raises:

>>> import typing
>>> issubclass(object, typing.Protocol)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1797, in __subclasscheck__
    raise TypeError(
TypeError: Instance and class checks can only be used with @runtime_checkable protocols

The new behaviour doesn't make sense. That TypeError should only be triggered if a user is calling issubclass() against a subclass of typing.Protocol. typing.Protocol itself should be exempted.

Linked PRs

@AlexWaygood AlexWaygood added type-bug An unexpected behavior, bug, or error topic-typing 3.12 bugs and security fixes 3.13 bugs and security fixes labels Jun 2, 2023
@AlexWaygood AlexWaygood self-assigned this Jun 2, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 5, 2023
AlexWaygood added a commit that referenced this issue Jun 5, 2023
GH-105239) (#105316)

gh-105237: Allow calling `issubclass(X, typing.Protocol)` again (GH-105239)
(cherry picked from commit cdfb201)

Co-authored-by: Alex Waygood <[email protected]>
@AlexWaygood
Copy link
Member Author

While working on fixing the bug, we discovered another bug, where issubclass(object, Protocol) can sometimes evaluate to True. Unlike the original bug, this one reproduces all the way back to Python 3.8. It's now fixed on main and 3.12, but it might be good to see if we can fix it on 3.11 as well.

@AlexWaygood
Copy link
Member Author

While working on fixing the bug, we discovered another bug, where issubclass(object, Protocol) can sometimes evaluate to True. Unlike the original bug, this one reproduces all the way back to Python 3.8. It's now fixed on main and 3.12, but it might be good to see if we can fix it on 3.11 as well.

Well, but on the other hand, I'm hesitant to touch 3.11 here, given that it's an edge-case bug, and it's hard to do a proper fix that's not somewhat invasive. So, I'll close this for now. (But if somebody else disagrees and wants to have a go at fixing it on 3.11, happy to reopen.)

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 topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant