From 53d5008f3a2751e59a1af186728fd6c3067ea24d Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Wed, 12 May 2021 16:51:28 +0300 Subject: [PATCH 1/4] Fix issue when non runtime_protocol does not raise TypeError --- Lib/test/test_typing.py | 11 +++++++++++ Lib/typing.py | 12 ++++++++++-- .../Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst | 2 ++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 47dc0b9358d7de..094fa62f3407ba 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1422,6 +1422,17 @@ class CustomProtocol(TestCase, Protocol): class CustomContextManager(typing.ContextManager, Protocol): pass + def test_non_runtime_protocol_isinstance_check(self): + class P(Protocol): + pass + + class A: + pass + + with self.assertRaises(TypeError): + isinstance(A(), P) + + class GenericTests(BaseTestCase): def test_basics(self): diff --git a/Lib/typing.py b/Lib/typing.py index a51452bfec357e..ca47a6f58453a4 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1343,14 +1343,14 @@ def _no_init(self, *args, **kwargs): raise TypeError('Protocols cannot be instantiated') -def _allow_reckless_class_checks(): +def _allow_reckless_class_checks(depth=3): """Allow instance and class checks for special stdlib modules. The abc and functools modules indiscriminately call isinstance() and issubclass() on the whole MRO of a user class, which may contain protocols. """ try: - return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools'] + return sys._getframe(depth).f_globals['__name__'] in ['abc', 'functools'] except (AttributeError, ValueError): # For platforms without _getframe(). return True @@ -1370,6 +1370,14 @@ class _ProtocolMeta(ABCMeta): def __instancecheck__(cls, instance): # We need this method for situations where attributes are # assigned in __init__. + if ( + getattr(cls, '_is_protocol', False) and + not getattr(cls, '_is_runtime_protocol', False) and + not _allow_reckless_class_checks(depth=2) + ): + raise TypeError("Instance and class checks can only be used with" + " @runtime_checkable protocols") + if ((not getattr(cls, '_is_protocol', False) or _is_callable_members_only(cls)) and issubclass(instance.__class__, cls)): diff --git a/Misc/NEWS.d/next/Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst b/Misc/NEWS.d/next/Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst new file mode 100644 index 00000000000000..d0b102ee197bd5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst @@ -0,0 +1,2 @@ +Fix issue when non runtime_protocol does not raise TypeError and return +False instead. Patch provided by Yurii Karabas. From 179cb403fd5bf6ebe73b5a4caf6606c9880c3d71 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Wed, 12 May 2021 17:29:12 +0300 Subject: [PATCH 2/4] Fix test --- Lib/test/test_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 094fa62f3407ba..b7dc683da612da 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1424,7 +1424,7 @@ class CustomContextManager(typing.ContextManager, Protocol): def test_non_runtime_protocol_isinstance_check(self): class P(Protocol): - pass + x: int class A: pass From 9c66ec36f1556833c04005156763d96a757e8c1e Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Wed, 12 May 2021 17:30:23 +0300 Subject: [PATCH 3/4] Update Lib/test/test_typing.py Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> --- Lib/test/test_typing.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index b7dc683da612da..a2a5d8f41019e5 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1426,11 +1426,8 @@ def test_non_runtime_protocol_isinstance_check(self): class P(Protocol): x: int - class A: - pass - - with self.assertRaises(TypeError): - isinstance(A(), P) + with self.assertRaisesRegex(TypeError, "@runtime_checkable"): + isinstance(1, P) class GenericTests(BaseTestCase): From d2baaba5076157db5f9eaa6126de310f0ddb40f6 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Wed, 12 May 2021 18:08:50 +0300 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> --- .../next/Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst b/Misc/NEWS.d/next/Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst index d0b102ee197bd5..b72936c205f67c 100644 --- a/Misc/NEWS.d/next/Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst +++ b/Misc/NEWS.d/next/Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst @@ -1,2 +1,5 @@ -Fix issue when non runtime_protocol does not raise TypeError and return -False instead. Patch provided by Yurii Karabas. +Fix issue where :mod:`typing` protocols without the ``@runtime_checkable`` +decorator did not raise a ``TypeError`` when used with ``issubclass`` and +``isinstance``. Now, subclassses of ``typing.Protocol`` will raise a +``TypeError`` when used with with those checks. +Patch provided by Yurii Karabas.