Content-Length: 301037 | pFad | http://github.com/python/cpython/pull/105239/commits/ffb7f1ccddcb0dafb9221c615e17add7ba746243

BD gh-105237: Allow calling `issubclass(X, typing.Protocol)` again by AlexWaygood · Pull Request #105239 · python/cpython · GitHub
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

gh-105237: Allow calling issubclass(X, typing.Protocol) again #105239

Merged
merged 6 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Small micro-optimisation
  • Loading branch information
AlexWaygood committed Jun 2, 2023
commit ffb7f1ccddcb0dafb9221c615e17add7ba746243
9 changes: 9 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2771,6 +2771,15 @@ def x(self): pass
self.assertNotIsSubclass(C, Protocol)
self.assertNotIsInstance(C(), Protocol)

only_classes_allowed = r"issubclass\(\) arg 1 must be a class"

with self.assertRaisesRegex(TypeError, only_classes_allowed):
issubclass(1, Protocol)
with self.assertRaisesRegex(TypeError, only_classes_allowed):
issubclass('foo', Protocol)
with self.assertRaisesRegex(TypeError, only_classes_allowed):
issubclass(C(), Protocol)

T = TypeVar('T')

@runtime_checkable
Expand Down
4 changes: 2 additions & 2 deletions Lib/typing.py
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -1782,11 +1782,11 @@ def __init__(cls, *args, **kwargs):
)

def __subclasscheck__(cls, other):
if cls is Protocol:
return type.__subclasscheck__(cls, other)
if not isinstance(other, type):
# Same error message as for issubclass(1, int).
raise TypeError('issubclass() arg 1 must be a class')
if cls is Protocol:
return type.__subclasscheck__(cls, other)
if (
getattr(cls, '_is_protocol', False)
and not _allow_reckless_class_checks()
Expand Down








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/pull/105239/commits/ffb7f1ccddcb0dafb9221c615e17add7ba746243

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy