[Python-checkins] [3.9] Revert "[3.9] bpo-38908: Fix issue when non runtime_protocol does not raise TypeError (GH-26067)" (GH-26077)
miss-islington
webhook-mailer at python.org
Wed May 12 13:44:22 EDT 2021
https://github.com/python/cpython/commit/9b90ce68503f4861ce4e9ac9444d9a82b3d943a5
commit: 9b90ce68503f4861ce4e9ac9444d9a82b3d943a5
branch: 3.9
author: Ken Jin <28750310+Fidget-Spinner at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-05-12T10:44:14-07:00
summary:
[3.9] Revert "[3.9] bpo-38908: Fix issue when non runtime_protocol does not raise TypeError (GH-26067)" (GH-26077)
Reverts python/cpython#26075
Automerge-Triggered-By: GH:gvanrossum
files:
D Misc/NEWS.d/next/Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst
M Lib/test/test_typing.py
M Lib/typing.py
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index d91363eb37960..4bdb2a0fad6c7 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -1422,14 +1422,6 @@ class CustomProtocol(TestCase, Protocol):
class CustomContextManager(typing.ContextManager, Protocol):
pass
- def test_non_runtime_protocol_isinstance_check(self):
- class P(Protocol):
- x: int
-
- with self.assertRaisesRegex(TypeError, "@runtime_checkable"):
- isinstance(1, P)
-
-
class GenericTests(BaseTestCase):
def test_basics(self):
diff --git a/Lib/typing.py b/Lib/typing.py
index c3844cf3083e1..123fbc2c45010 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1079,14 +1079,14 @@ def _no_init(self, *args, **kwargs):
raise TypeError('Protocols cannot be instantiated')
-def _allow_reckless_class_checks(depth=3):
+def _allow_reckless_class_cheks():
"""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(depth).f_globals['__name__'] in ['abc', 'functools']
+ return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
except (AttributeError, ValueError): # For platforms without _getframe().
return True
@@ -1106,14 +1106,6 @@ 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)):
@@ -1176,12 +1168,12 @@ def _proto_hook(other):
# First, perform various sanity checks.
if not getattr(cls, '_is_runtime_protocol', False):
- if _allow_reckless_class_checks():
+ if _allow_reckless_class_cheks():
return NotImplemented
raise TypeError("Instance and class checks can only be used with"
" @runtime_checkable protocols")
if not _is_callable_members_only(cls):
- if _allow_reckless_class_checks():
+ if _allow_reckless_class_cheks():
return NotImplemented
raise TypeError("Protocols with non-method members"
" don't support issubclass()")
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
deleted file mode 100644
index b72936c205f67..0000000000000
--- a/Misc/NEWS.d/next/Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst
+++ /dev/null
@@ -1,5 +0,0 @@
-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.
More information about the Python-checkins
mailing list