Skip to content

Commit 9b90ce6

Browse files
[3.9] Revert "[3.9] bpo-38908: Fix issue when non runtime_protocol does not raise TypeError (GH-26067)" (GH-26077)
Reverts #26075 Automerge-Triggered-By: GH:gvanrossum
1 parent 88136bb commit 9b90ce6

File tree

3 files changed

+4
-25
lines changed

3 files changed

+4
-25
lines changed

Lib/test/test_typing.py

-8
Original file line numberDiff line numberDiff line change
@@ -1422,14 +1422,6 @@ class CustomProtocol(TestCase, Protocol):
14221422
class CustomContextManager(typing.ContextManager, Protocol):
14231423
pass
14241424

1425-
def test_non_runtime_protocol_isinstance_check(self):
1426-
class P(Protocol):
1427-
x: int
1428-
1429-
with self.assertRaisesRegex(TypeError, "@runtime_checkable"):
1430-
isinstance(1, P)
1431-
1432-
14331425
class GenericTests(BaseTestCase):
14341426

14351427
def test_basics(self):

Lib/typing.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -1079,14 +1079,14 @@ def _no_init(self, *args, **kwargs):
10791079
raise TypeError('Protocols cannot be instantiated')
10801080

10811081

1082-
def _allow_reckless_class_checks(depth=3):
1082+
def _allow_reckless_class_cheks():
10831083
"""Allow instance and class checks for special stdlib modules.
10841084
10851085
The abc and functools modules indiscriminately call isinstance() and
10861086
issubclass() on the whole MRO of a user class, which may contain protocols.
10871087
"""
10881088
try:
1089-
return sys._getframe(depth).f_globals['__name__'] in ['abc', 'functools']
1089+
return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
10901090
except (AttributeError, ValueError): # For platforms without _getframe().
10911091
return True
10921092

@@ -1106,14 +1106,6 @@ class _ProtocolMeta(ABCMeta):
11061106
def __instancecheck__(cls, instance):
11071107
# We need this method for situations where attributes are
11081108
# assigned in __init__.
1109-
if (
1110-
getattr(cls, '_is_protocol', False) and
1111-
not getattr(cls, '_is_runtime_protocol', False) and
1112-
not _allow_reckless_class_checks(depth=2)
1113-
):
1114-
raise TypeError("Instance and class checks can only be used with"
1115-
" @runtime_checkable protocols")
1116-
11171109
if ((not getattr(cls, '_is_protocol', False) or
11181110
_is_callable_members_only(cls)) and
11191111
issubclass(instance.__class__, cls)):
@@ -1176,12 +1168,12 @@ def _proto_hook(other):
11761168

11771169
# First, perform various sanity checks.
11781170
if not getattr(cls, '_is_runtime_protocol', False):
1179-
if _allow_reckless_class_checks():
1171+
if _allow_reckless_class_cheks():
11801172
return NotImplemented
11811173
raise TypeError("Instance and class checks can only be used with"
11821174
" @runtime_checkable protocols")
11831175
if not _is_callable_members_only(cls):
1184-
if _allow_reckless_class_checks():
1176+
if _allow_reckless_class_cheks():
11851177
return NotImplemented
11861178
raise TypeError("Protocols with non-method members"
11871179
" don't support issubclass()")

Misc/NEWS.d/next/Library/2021-05-12-16-43-21.bpo-38908.nM2_rO.rst

-5
This file was deleted.

0 commit comments

Comments
 (0)