From 9234769e6612b30dba6318b01431c4c6064f16af Mon Sep 17 00:00:00 2001 From: Randolf Scholz Date: Thu, 16 Nov 2023 14:25:05 +0100 Subject: [PATCH] added fast check in _ProtocolMeta.__instancecheck__ --- Lib/typing.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/typing.py b/Lib/typing.py index 14845b36028ca1..911dcd096e7e7b 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1850,6 +1850,11 @@ def __instancecheck__(cls, instance): if super().__instancecheck__(instance): return True + # quick true negative check for protocols with many attributes. + if not hasattr(instance, '__getattr__'): + if not (cls.__protocol_attrs__ <= set(dir(instance))): + return False + getattr_static = _lazy_load_getattr_static() for attr in cls.__protocol_attrs__: try: