]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-133037: Add test for shadowing __annotate__ (#133084)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Sat, 3 May 2025 02:42:49 +0000 (19:42 -0700)
committerGitHub <noreply@github.com>
Sat, 3 May 2025 02:42:49 +0000 (19:42 -0700)
Lib/test/test_type_annotations.py

index b72d3dbe5169744295ea99e09bb66670fe736609..2c886bb6d362fa89f06782ea440baed59060261e 100644 (file)
@@ -327,6 +327,25 @@ class AnnotateTests(unittest.TestCase):
         f.__annotations__ = {"z": 43}
         self.assertIs(f.__annotate__, None)
 
+    def test_user_defined_annotate(self):
+        class X:
+            a: int
+
+            def __annotate__(format):
+                return {"a": str}
+        self.assertEqual(X.__annotate__(annotationlib.Format.VALUE), {"a": str})
+        self.assertEqual(annotationlib.get_annotations(X), {"a": str})
+
+        mod = build_module(
+            """
+            a: int
+            def __annotate__(format):
+                return {"a": str}
+            """
+        )
+        self.assertEqual(mod.__annotate__(annotationlib.Format.VALUE), {"a": str})
+        self.assertEqual(annotationlib.get_annotations(mod), {"a": str})
+
 
 class DeferredEvaluationTests(unittest.TestCase):
     def test_function(self):