Skip to content

Commit 63e4bc9

Browse files
committed
Add a test for propagating the error from a failed call to VALUE annotations if they fail.
1 parent b387bc3 commit 63e4bc9

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

Lib/test/test_annotationlib.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,25 @@ class RaisesAttributeError:
11861186
},
11871187
)
11881188

1189+
def test_raises_error_from_value(self):
1190+
# test that if VALUE is the only supported format, but raises an error
1191+
# that error is propagated from get_annotations
1192+
class DemoException(Exception): ...
1193+
1194+
def annotate(format, /):
1195+
if format == Format.VALUE:
1196+
raise DemoException()
1197+
else:
1198+
raise NotImplementedError(format)
1199+
1200+
def f(): ...
1201+
1202+
f.__annotate__ = annotate
1203+
1204+
for fmt in [Format.VALUE, Format.FORWARDREF, Format.STRING]:
1205+
with self.assertRaises(DemoException):
1206+
get_annotations(f, format=fmt)
1207+
11891208

11901209
class TestCallEvaluateFunction(unittest.TestCase):
11911210
def test_evaluation(self):
@@ -1344,7 +1363,23 @@ def annotate(format, /):
13441363
raise NotImplementedError(format)
13451364

13461365
with self.assertRaises(NotImplementedError):
1347-
_ = annotationlib.call_annotate_function(annotate, Format.STRING)
1366+
annotationlib.call_annotate_function(annotate, Format.STRING)
1367+
1368+
def test_error_from_value_raised(self):
1369+
# Test that the error from format.VALUE is raised
1370+
# if all formats fail
1371+
1372+
class DemoException(Exception): ...
1373+
1374+
def annotate(format, /):
1375+
if format == Format.VALUE:
1376+
raise DemoException()
1377+
else:
1378+
raise NotImplementedError(format)
1379+
1380+
for fmt in [Format.VALUE, Format.FORWARDREF, Format.STRING]:
1381+
with self.assertRaises(DemoException):
1382+
annotationlib.call_annotate_function(annotate, format=fmt)
13481383

13491384

13501385
class MetaclassTests(unittest.TestCase):

0 commit comments

Comments
 (0)