mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-105237: Allow calling issubclass(X, typing.Protocol)
again (#105239)
This commit is contained in:
parent
69d1245685
commit
cdfb201bfa
3 changed files with 65 additions and 0 deletions
|
@ -2758,6 +2758,65 @@ class ProtocolTests(BaseTestCase):
|
|||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue