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
Prev Previous commit
Next Next commit
Fix longstanding bug and add many tests
  • Loading branch information
AlexWaygood committed Jun 2, 2023
commit 776bf2bdbed6c01cc48bdb34446a41cac78bacae
52 changes: 46 additions & 6 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2758,15 +2758,55 @@ def x(self): ...
with self.assertRaisesRegex(TypeError, only_classes_allowed):
issubclass(1, BadPG)

for proto in P, PG, BadP, BadPG:
def test_issubclass_and_isinstance_on_Protocol_itself(self):
class C:
def x(self): pass

self.assertNotIsSubclass(object, Protocol)
self.assertNotIsInstance(object(), Protocol)

self.assertNotIsSubclass(str, Protocol)
self.assertNotIsInstance('foo', Protocol)

self.assertNotIsSubclass(C, Protocol)
self.assertNotIsInstance(C(), Protocol)

T = TypeVar('T')

@runtime_checkable
class EmptyProtocol(Protocol): pass

@runtime_checkable
class SupportsStartsWith(Protocol):
def startswith(self, x: str) -> bool: ...

@runtime_checkable
class SupportsX(Protocol[T]):
def x(self): ...

for proto in EmptyProtocol, SupportsStartsWith, SupportsX:
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)
# gh-105237 / PR #105239:
# check that the presence of Protocol subclasses
# where `issubclass(X, <subclass>)` evaluates to True
# doesn't influence the result of `issubclass(X, Protocol)`

self.assertIsSubclass(object, EmptyProtocol)
self.assertIsInstance(object(), EmptyProtocol)
self.assertNotIsSubclass(object, Protocol)
self.assertNotIsInstance(object(), Protocol)

self.assertIsSubclass(str, SupportsStartsWith)
self.assertIsInstance('foo', SupportsStartsWith)
self.assertNotIsSubclass(str, Protocol)
self.assertNotIsInstance('foo', Protocol)

self.assertIsSubclass(C, SupportsX)
self.assertIsInstance(C(), SupportsX)
self.assertNotIsSubclass(C, Protocol)
self.assertNotIsInstance(C(), Protocol)

def test_protocols_issubclass_non_callable(self):
class C:
Expand Down
2 changes: 2 additions & 0 deletions Lib/typing.py
Comment thread
AlexWaygood marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,8 @@ def __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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix longstanding bug where ``issubclass(object, typing.Protocol)`` would
evaluate to ``True`` in some edge cases. Patch by Alex Waygood.