Skip to content

Commit fd1f853

Browse files
authored
Add a few more tests for interactions between typing.Protocol and typing_extensions.Protocol (#244)
1 parent af89916 commit fd1f853

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/test_typing_extensions.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,11 +1817,37 @@ class SubProto3(ExtensionsProto, TypingProto, typing.Protocol):
18171817
class SubProto4(ExtensionsProto, TypingProto, Protocol):
18181818
z: int
18191819

1820+
for proto in (
1821+
ExtensionsProto, SubProto, SubProto2, SubProto3, SubProto4
1822+
):
1823+
with self.subTest(proto=proto.__name__):
1824+
self.assertTrue(is_protocol(proto))
1825+
if Protocol is not typing.Protocol:
1826+
self.assertIsInstance(proto, typing_extensions._ProtocolMeta)
1827+
self.assertIsInstance(proto.__protocol_attrs__, set)
1828+
with self.assertRaisesRegex(
1829+
TypeError, "Protocols cannot be instantiated"
1830+
):
1831+
proto()
1832+
# check these don't raise
1833+
runtime_checkable(proto)
1834+
typing.runtime_checkable(proto)
1835+
18201836
class Concrete(SubProto): pass
18211837
class Concrete2(SubProto2): pass
18221838
class Concrete3(SubProto3): pass
18231839
class Concrete4(SubProto4): pass
18241840

1841+
for cls in Concrete, Concrete2, Concrete3, Concrete4:
1842+
with self.subTest(cls=cls.__name__):
1843+
self.assertFalse(is_protocol(cls))
1844+
# Check that this doesn't raise:
1845+
self.assertIsInstance(cls(), cls)
1846+
with self.assertRaises(TypeError):
1847+
runtime_checkable(cls)
1848+
with self.assertRaises(TypeError):
1849+
typing.runtime_checkable(cls)
1850+
18251851
def test_no_instantiation(self):
18261852
class P(Protocol): pass
18271853
with self.assertRaises(TypeError):

0 commit comments

Comments
 (0)