diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py index 85e02b23..d7e9dc44 100644 --- a/src/test_typing_extensions.py +++ b/src/test_typing_extensions.py @@ -1817,11 +1817,37 @@ class SubProto3(ExtensionsProto, TypingProto, typing.Protocol): class SubProto4(ExtensionsProto, TypingProto, Protocol): z: int + for proto in ( + ExtensionsProto, SubProto, SubProto2, SubProto3, SubProto4 + ): + with self.subTest(proto=proto.__name__): + self.assertTrue(is_protocol(proto)) + if Protocol is not typing.Protocol: + self.assertIsInstance(proto, typing_extensions._ProtocolMeta) + self.assertIsInstance(proto.__protocol_attrs__, set) + with self.assertRaisesRegex( + TypeError, "Protocols cannot be instantiated" + ): + proto() + # check these don't raise + runtime_checkable(proto) + typing.runtime_checkable(proto) + class Concrete(SubProto): pass class Concrete2(SubProto2): pass class Concrete3(SubProto3): pass class Concrete4(SubProto4): pass + for cls in Concrete, Concrete2, Concrete3, Concrete4: + with self.subTest(cls=cls.__name__): + self.assertFalse(is_protocol(cls)) + # Check that this doesn't raise: + self.assertIsInstance(cls(), cls) + with self.assertRaises(TypeError): + runtime_checkable(cls) + with self.assertRaises(TypeError): + typing.runtime_checkable(cls) + def test_no_instantiation(self): class P(Protocol): pass with self.assertRaises(TypeError):