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
14 changes: 12 additions & 2 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2758,6 +2758,16 @@ def x(self): ...
with self.assertRaisesRegex(TypeError, only_classes_allowed):
issubclass(1, BadPG)

for proto in P, PG, BadP, BadPG:
with self.subTest(proto=proto.__name__):
self.assertIsSubclass(proto, Protocol)

# TODO: the behaviour of some of these is questionable!
# For now, just check that these don't raise any Exceptions
issubclass(object, Protocol)
issubclass(str, Protocol)
issubclass(C, Protocol)
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved

def test_protocols_issubclass_non_callable(self):
class C:
x = 1
Expand Down Expand Up @@ -3505,8 +3515,8 @@ def meth(self):
self.assertTrue(P._is_protocol)
self.assertTrue(PR._is_protocol)
self.assertTrue(PG._is_protocol)
self.assertFalse(P._is_runtime_protocol)
self.assertTrue(PR._is_runtime_protocol)
self.assertFalse(getattr(P, "_is_runtime_protocol", False))
self.assertTrue(getattr(PR, "_is_runtime_protocol", False))
self.assertTrue(PG[int]._is_protocol)
self.assertEqual(typing._get_protocol_attrs(P), {'meth'})
self.assertEqual(typing._get_protocol_attrs(PR), {'x'})
Expand Down
4 changes: 1 addition & 3 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 @@ -1862,8 +1862,6 @@ def meth(self) -> T:
...
"""
__slots__ = ()
_is_protocol = True
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
_is_runtime_protocol = False

def __init_subclass__(cls, *args, **kwargs):
super().__init_subclass__(*args, **kwargs)
Expand Down Expand Up @@ -1904,7 +1902,7 @@ def _proto_hook(other):

# ... otherwise check consistency of bases, and prohibit instantiation.
for base in cls.__bases__:
if not (base in (object, Generic) or
if not (base in {object, Generic, Protocol} or
base.__module__ in _PROTO_ALLOWLIST and
base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
issubclass(base, Generic) and getattr(base, '_is_protocol', False)):
Expand Down