Skip to content

Commit 4b9ed4e

Browse files
[3.14] gh-141174: Improve annotationlib.call_annotate_function() test coverage (GH-141176) (#141355)
gh-141174: Improve `annotationlib.call_annotate_function()` test coverage (GH-141176) * 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 (cherry picked from commit 1110e8f) Co-authored-by: dr-carlos <[email protected]>
1 parent 727cdcb commit 4b9ed4e

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Lib/test/test_annotationlib.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,32 @@ def evaluate(format, exc=NotImplementedError):
13151315
"undefined",
13161316
)
13171317

1318+
def test_fake_global_evaluation(self):
1319+
# This will raise an AttributeError
1320+
def evaluate_union(format, exc=NotImplementedError):
1321+
if format == Format.VALUE_WITH_FAKE_GLOBALS:
1322+
# Return a ForwardRef
1323+
return builtins.undefined | list[int]
1324+
raise exc
1325+
1326+
self.assertEqual(
1327+
annotationlib.call_evaluate_function(evaluate_union, Format.FORWARDREF),
1328+
support.EqualToForwardRef("builtins.undefined | list[int]"),
1329+
)
1330+
1331+
# This will raise an AttributeError
1332+
def evaluate_intermediate(format, exc=NotImplementedError):
1333+
if format == Format.VALUE_WITH_FAKE_GLOBALS:
1334+
intermediate = builtins.undefined
1335+
# Return a literal
1336+
return intermediate is None
1337+
raise exc
1338+
1339+
self.assertIs(
1340+
annotationlib.call_evaluate_function(evaluate_intermediate, Format.FORWARDREF),
1341+
False,
1342+
)
1343+
13181344

13191345
class TestCallAnnotateFunction(unittest.TestCase):
13201346
# Tests for user defined annotate functions.
@@ -1456,6 +1482,23 @@ def annotate(format, /):
14561482
with self.assertRaises(NotImplementedError):
14571483
annotationlib.call_annotate_function(annotate, Format.STRING)
14581484

1485+
def test_unsupported_formats(self):
1486+
def annotate(format, /):
1487+
if format == Format.FORWARDREF:
1488+
return {"x": str}
1489+
else:
1490+
raise NotImplementedError(format)
1491+
1492+
with self.assertRaises(ValueError):
1493+
annotationlib.call_annotate_function(annotate, Format.VALUE_WITH_FAKE_GLOBALS)
1494+
1495+
with self.assertRaises(RuntimeError):
1496+
annotationlib.call_annotate_function(annotate, Format.VALUE)
1497+
1498+
with self.assertRaises(ValueError):
1499+
# Some non-Format value
1500+
annotationlib.call_annotate_function(annotate, 7)
1501+
14591502
def test_error_from_value_raised(self):
14601503
# Test that the error from format.VALUE is raised
14611504
# if all formats fail

0 commit comments

Comments
 (0)