Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
59 changes: 59 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2758,6 +2758,65 @@ def x(self): ...
with self.assertRaisesRegex(TypeError, only_classes_allowed):
issubclass(1, 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)

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
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)

# 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:
x = 1
Expand Down
6 changes: 4 additions & 2 deletions Lib/typing.py
Comment thread
AlexWaygood marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,8 @@ 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')
Expand Down Expand Up @@ -1862,7 +1864,7 @@ def meth(self) -> T:
...
"""
__slots__ = ()
_is_protocol = True
Comment thread
AlexWaygood marked this conversation as resolved.
_is_protocol = False
_is_runtime_protocol = False

def __init_subclass__(cls, *args, **kwargs):
Expand Down Expand Up @@ -1904,7 +1906,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
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.