Skip to content

Commit

Permalink
pythongh-74690: typing: Simplify and optimise `_ProtocolMeta.__instan…
Browse files Browse the repository at this point in the history
…cecheck__` (python#103159)
  • Loading branch information
AlexWaygood authored and warsaw committed Apr 11, 2023
1 parent 55c701e commit e14056b
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2024,20 +2024,12 @@ def __instancecheck__(cls, instance):
raise TypeError("Instance and class checks can only be used with"
" @runtime_checkable protocols")

if not is_protocol_cls and issubclass(instance.__class__, cls):
return True

protocol_attrs = _get_protocol_attrs(cls)

if (
_is_callable_members_only(cls, protocol_attrs)
and issubclass(instance.__class__, cls)
):
if super().__instancecheck__(instance):
return True

if is_protocol_cls:
getattr_static = _lazy_load_getattr_static()
for attr in protocol_attrs:
for attr in _get_protocol_attrs(cls):
try:
val = getattr_static(instance, attr)
except AttributeError:
Expand All @@ -2047,7 +2039,7 @@ def __instancecheck__(cls, instance):
else:
return True

return super().__instancecheck__(instance)
return False


class Protocol(Generic, metaclass=_ProtocolMeta):
Expand Down

0 comments on commit e14056b

Please sign in to comment.