Skip to content
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)
Comment thread
AlexWaygood marked this conversation as resolved.
Outdated

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
Comment thread
AlexWaygood marked this conversation as 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
Comment thread
AlexWaygood marked this conversation as 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