]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-141174: Improve `annotationlib.call_annotate_function()` test coverage (#141176)
authordr-carlos <77367421+dr-carlos@users.noreply.github.com>
Mon, 10 Nov 2025 14:54:50 +0000 (01:24 +1030)
committerGitHub <noreply@github.com>
Mon, 10 Nov 2025 14:54:50 +0000 (06:54 -0800)
* Test passing unsupported Format values to call_annotate_function()

* Test call_evaluate_function with fake globals that raise errors

* Fix typo and comparison in test_fake_global_evaluation

Lib/test/test_annotationlib.py

index d196801eedee577e55bda2e125d5626a4397a51b..0ae598b6839692713c4ec2ad3eded6b229d7f162 100644 (file)
@@ -1339,6 +1339,32 @@ class TestCallEvaluateFunction(unittest.TestCase):
             "undefined",
         )
 
+    def test_fake_global_evaluation(self):
+        # This will raise an AttributeError
+        def evaluate_union(format, exc=NotImplementedError):
+            if format == Format.VALUE_WITH_FAKE_GLOBALS:
+                # Return a ForwardRef
+                return builtins.undefined | list[int]
+            raise exc
+
+        self.assertEqual(
+            annotationlib.call_evaluate_function(evaluate_union, Format.FORWARDREF),
+            support.EqualToForwardRef("builtins.undefined | list[int]"),
+        )
+
+        # This will raise an AttributeError
+        def evaluate_intermediate(format, exc=NotImplementedError):
+            if format == Format.VALUE_WITH_FAKE_GLOBALS:
+                intermediate = builtins.undefined
+                # Return a literal
+                return intermediate is None
+            raise exc
+
+        self.assertIs(
+            annotationlib.call_evaluate_function(evaluate_intermediate, Format.FORWARDREF),
+            False,
+        )
+
 
 class TestCallAnnotateFunction(unittest.TestCase):
     # Tests for user defined annotate functions.
@@ -1480,6 +1506,23 @@ class TestCallAnnotateFunction(unittest.TestCase):
         with self.assertRaises(NotImplementedError):
             annotationlib.call_annotate_function(annotate, Format.STRING)
 
+    def test_unsupported_formats(self):
+        def annotate(format, /):
+            if format == Format.FORWARDREF:
+                return {"x": str}
+            else:
+                raise NotImplementedError(format)
+
+        with self.assertRaises(ValueError):
+            annotationlib.call_annotate_function(annotate, Format.VALUE_WITH_FAKE_GLOBALS)
+
+        with self.assertRaises(RuntimeError):
+            annotationlib.call_annotate_function(annotate, Format.VALUE)
+
+        with self.assertRaises(ValueError):
+            # Some non-Format value
+            annotationlib.call_annotate_function(annotate, 7)
+
     def test_error_from_value_raised(self):
         # Test that the error from format.VALUE is raised
         # if all formats fail