Skip to content

Commit e14056b

Browse files
AlexWaygoodwarsaw
authored andcommitted
pythongh-74690: typing: Simplify and optimise _ProtocolMeta.__instancecheck__ (python#103159)
1 parent 55c701e commit e14056b

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

Lib/typing.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -2024,20 +2024,12 @@ def __instancecheck__(cls, instance):
20242024
raise TypeError("Instance and class checks can only be used with"
20252025
" @runtime_checkable protocols")
20262026

2027-
if not is_protocol_cls and issubclass(instance.__class__, cls):
2028-
return True
2029-
2030-
protocol_attrs = _get_protocol_attrs(cls)
2031-
2032-
if (
2033-
_is_callable_members_only(cls, protocol_attrs)
2034-
and issubclass(instance.__class__, cls)
2035-
):
2027+
if super().__instancecheck__(instance):
20362028
return True
20372029

20382030
if is_protocol_cls:
20392031
getattr_static = _lazy_load_getattr_static()
2040-
for attr in protocol_attrs:
2032+
for attr in _get_protocol_attrs(cls):
20412033
try:
20422034
val = getattr_static(instance, attr)
20432035
except AttributeError:
@@ -2047,7 +2039,7 @@ def __instancecheck__(cls, instance):
20472039
else:
20482040
return True
20492041

2050-
return super().__instancecheck__(instance)
2042+
return False
20512043

20522044

20532045
class Protocol(Generic, metaclass=_ProtocolMeta):

0 commit comments

Comments
 (0)