Skip to content

Commit 1110e8f

Browse files
authored
pythongh-141174: Improve annotationlib.call_annotate_function() test coverage (python#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
1 parent 19b5730 commit 1110e8f

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
@@ -1339,6 +1339,32 @@ def evaluate(format, exc=NotImplementedError):
13391339
"undefined",
13401340
)
13411341

1342+
def test_fake_global_evaluation(self):
1343+
# This will raise an AttributeError
1344+
def evaluate_union(format, exc=NotImplementedError):
1345+
if format == Format.VALUE_WITH_FAKE_GLOBALS:
1346+
# Return a ForwardRef
1347+
return builtins.undefined | list[int]
1348+
raise exc
1349+
1350+
self.assertEqual(
1351+
annotationlib.call_evaluate_function(evaluate_union, Format.FORWARDREF),
1352+
support.EqualToForwardRef("builtins.undefined | list[int]"),
1353+
)
1354+
1355+
# This will raise an AttributeError
1356+
def evaluate_intermediate(format, exc=NotImplementedError):
1357+
if format == Format.VALUE_WITH_FAKE_GLOBALS:
1358+
intermediate = builtins.undefined
1359+
# Return a literal
1360+
return intermediate is None
1361+
raise exc
1362+
1363+
self.assertIs(
1364+
annotationlib.call_evaluate_function(evaluate_intermediate, Format.FORWARDREF),
1365+
False,
1366+
)
1367+
13421368

13431369
class TestCallAnnotateFunction(unittest.TestCase):
13441370
# Tests for user defined annotate functions.
@@ -1480,6 +1506,23 @@ def annotate(format, /):
14801506
with self.assertRaises(NotImplementedError):
14811507
annotationlib.call_annotate_function(annotate, Format.STRING)
14821508

1509+
def test_unsupported_formats(self):
1510+
def annotate(format, /):
1511+
if format == Format.FORWARDREF:
1512+
return {"x": str}
1513+
else:
1514+
raise NotImplementedError(format)
1515+
1516+
with self.assertRaises(ValueError):
1517+
annotationlib.call_annotate_function(annotate, Format.VALUE_WITH_FAKE_GLOBALS)
1518+
1519+
with self.assertRaises(RuntimeError):
1520+
annotationlib.call_annotate_function(annotate, Format.VALUE)
1521+
1522+
with self.assertRaises(ValueError):
1523+
# Some non-Format value
1524+
annotationlib.call_annotate_function(annotate, 7)
1525+
14831526
def test_error_from_value_raised(self):
14841527
# Test that the error from format.VALUE is raised
14851528
# if all formats fail

0 commit comments

Comments
 (0)