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
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)):
# 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()")