Skip to content

Commit

Permalink
Revert "[3.9] bpo-38908: Fix issue when non runtime_protocol does not…
Browse files Browse the repository at this point in the history
… raise TypeError (pythonGH-26067) (pythonGH-26075)"

This reverts commit 88136bb.
  • Loading branch information
Fidget-Spinner committed May 12, 2021
1 parent 88136bb commit 3ea1c0b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 25 deletions.
8 changes: 0 additions & 8 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
16 changes: 4 additions & 12 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)):
Expand Down Expand Up @@ -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()")
Expand Down

This file was deleted.

0 comments on commit 3ea1c0b

Please sign in to comment.