]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132493: lazy evaluation of annotations in `typing._proto_hook` (#132534)
authorFelix Scherz <felixwscherz@gmail.com>
Wed, 16 Apr 2025 15:20:35 +0000 (17:20 +0200)
committerGitHub <noreply@github.com>
Wed, 16 Apr 2025 15:20:35 +0000 (08:20 -0700)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2025-04-15-08-39-14.gh-issue-132493.V0gLkU.rst [new file with mode: 0644]

index 16c5a5204da102788bbbbb95edddbd96ccd98254..5b05ebe8702015a644d345f56d6881868efad2e3 100644 (file)
@@ -4554,6 +4554,33 @@ class ProtocolTests(BaseTestCase):
         )
         self.assertIs(type(exc.__cause__), CustomError)
 
+    def test_isinstance_with_deferred_evaluation_of_annotations(self):
+        @runtime_checkable
+        class P(Protocol):
+            def meth(self):
+                ...
+
+        class DeferredClass:
+            x: undefined
+
+        class DeferredClassImplementingP:
+            x: undefined | int
+
+            def __init__(self):
+                self.x = 0
+
+            def meth(self):
+                ...
+
+        # override meth with a non-method attribute to make it part of __annotations__ instead of __dict__
+        class SubProtocol(P, Protocol):
+            meth: undefined
+
+
+        self.assertIsSubclass(SubProtocol, P)
+        self.assertNotIsInstance(DeferredClass(), P)
+        self.assertIsInstance(DeferredClassImplementingP(), P)
+
     def test_deferred_evaluation_of_annotations(self):
         class DeferredProto(Protocol):
             x: DoesNotExist
index 36789624d2f57aad5ab2037d881e5f6a7bb0c746..245592b5678957becf0723036a3e6501c7723287 100644 (file)
@@ -2020,10 +2020,13 @@ def _proto_hook(cls, other):
                 break
 
             # ...or in annotations, if it is a sub-protocol.
-            annotations = getattr(base, '__annotations__', {})
-            if (isinstance(annotations, collections.abc.Mapping) and
-                    attr in annotations and
-                    issubclass(other, Generic) and getattr(other, '_is_protocol', False)):
+            if (
+                issubclass(other, Generic)
+                and getattr(other, "_is_protocol", False)
+                and attr in _lazy_annotationlib.get_annotations(
+                    base, format=_lazy_annotationlib.Format.FORWARDREF
+                )
+            ):
                 break
         else:
             return NotImplemented
diff --git a/Misc/NEWS.d/next/Library/2025-04-15-08-39-14.gh-issue-132493.V0gLkU.rst b/Misc/NEWS.d/next/Library/2025-04-15-08-39-14.gh-issue-132493.V0gLkU.rst
new file mode 100644 (file)
index 0000000..a7f7627
--- /dev/null
@@ -0,0 +1,4 @@
+:class:`typing.Protocol` now uses :func:`annotationlib.get_annotations` when
+checking whether or not an instance implements the protocol with
+:func:`isinstance`. This enables support for ``isinstance`` checks against
+classes with deferred annotations.