mirror of
https://github.com/python/cpython.git
synced 2025-10-13 10:23:28 +00:00
gh-74690: typing: Simplify and optimise _ProtocolMeta.__instancecheck__
(#103159)
This commit is contained in:
parent
264c00a1c5
commit
47753ecde2
1 changed files with 3 additions and 11 deletions
|
@ -2024,20 +2024,12 @@ class _ProtocolMeta(ABCMeta):
|
||||||
raise TypeError("Instance and class checks can only be used with"
|
raise TypeError("Instance and class checks can only be used with"
|
||||||
" @runtime_checkable protocols")
|
" @runtime_checkable protocols")
|
||||||
|
|
||||||
if not is_protocol_cls and issubclass(instance.__class__, cls):
|
if super().__instancecheck__(instance):
|
||||||
return True
|
|
||||||
|
|
||||||
protocol_attrs = _get_protocol_attrs(cls)
|
|
||||||
|
|
||||||
if (
|
|
||||||
_is_callable_members_only(cls, protocol_attrs)
|
|
||||||
and issubclass(instance.__class__, cls)
|
|
||||||
):
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if is_protocol_cls:
|
if is_protocol_cls:
|
||||||
getattr_static = _lazy_load_getattr_static()
|
getattr_static = _lazy_load_getattr_static()
|
||||||
for attr in protocol_attrs:
|
for attr in _get_protocol_attrs(cls):
|
||||||
try:
|
try:
|
||||||
val = getattr_static(instance, attr)
|
val = getattr_static(instance, attr)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
@ -2047,7 +2039,7 @@ class _ProtocolMeta(ABCMeta):
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return super().__instancecheck__(instance)
|
return False
|
||||||
|
|
||||||
|
|
||||||
class Protocol(Generic, metaclass=_ProtocolMeta):
|
class Protocol(Generic, metaclass=_ProtocolMeta):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue