diff --git a/python2/test_typing.py b/python2/test_typing.py index e95a10ffd..06791397b 100644 --- a/python2/test_typing.py +++ b/python2/test_typing.py @@ -480,7 +480,13 @@ def test_reversible(self): def test_protocol_instance_type_error(self): with self.assertRaises(TypeError): isinstance(0, typing.SupportsAbs) - + class C1(typing.SupportsInt): + def __int__(self): + return 42 + class C2(C1): + pass + c = C2() + self.assertIsInstance(c, C1) class GenericTests(BaseTestCase): diff --git a/python2/typing.py b/python2/typing.py index 993df27a8..eab198757 100644 --- a/python2/typing.py +++ b/python2/typing.py @@ -1462,6 +1462,8 @@ class _ProtocolMeta(GenericMeta): """ def __instancecheck__(self, obj): + if _Protocol not in self.__bases__: + return super(_ProtocolMeta, self).__instancecheck__(obj) raise TypeError("Protocols cannot be used with isinstance().") def __subclasscheck__(self, cls): diff --git a/src/test_typing.py b/src/test_typing.py index 12bbf164a..183ede310 100644 --- a/src/test_typing.py +++ b/src/test_typing.py @@ -509,6 +509,13 @@ def test_reversible(self): def test_protocol_instance_type_error(self): with self.assertRaises(TypeError): isinstance(0, typing.SupportsAbs) + class C1(typing.SupportsInt): + def __int__(self) -> int: + return 42 + class C2(C1): + pass + c = C2() + self.assertIsInstance(c, C1) class GenericTests(BaseTestCase): diff --git a/src/typing.py b/src/typing.py index fe22b2b5e..c7fe5ddb9 100644 --- a/src/typing.py +++ b/src/typing.py @@ -1503,6 +1503,8 @@ class _ProtocolMeta(GenericMeta): """ def __instancecheck__(self, obj): + if _Protocol not in self.__bases__: + return super().__instancecheck__(obj) raise TypeError("Protocols cannot be used with isinstance().") def __subclasscheck__(self, cls):