8000 gh-119180: Make the FORWARDREF format work with more kinds of runtime… · faster-cpython/cpython@4e498d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e498d1

Browse files
pythongh-119180: Make the FORWARDREF format work with more kinds of runtime errors (python#133407)
1 parent 4c56563 commit 4e498d1

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Lib/annotationlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ def get_annotations(
868868
# For FORWARDREF, we use __annotations__ if it exists
869869
try:
870870
ann = _get_dunder_annotations(obj)
871-
except NameError:
871+
except Exception:
872872
pass
873873
else:
874874
if ann is not None:

Lib/test/test_annotationlib.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,21 @@ def f(
10531053
},
10541054
)
10551055

1056+
def test_partial_evaluation_error(self):
1057+
def f(x: range[1]):
1058+
pass
1059+
with self.assertRaisesRegex(
1060+
TypeError, "type 'range' is not subscriptable"
1061+
):
1062+
f.__annotations__
1063+
1064+
self.assertEqual(
1065+
get_annotations(f, format=Format.FORWARDREF),
1066+
{
1067+
"x": support.EqualToForwardRef("range[1]", owner=f),
1068+
},
1069+
)
1070+
10561071
def test_partial_evaluation_cell(self):
10571072
obj = object()
10581073

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Make :func:`annotationlib.get_annotations` succeed with the ``FORWARDREF``
2+
format if evaluating the annotations throws an exception other than
3+
:exc:`NameError` or :exc:`AttributeError`.

0 commit comments

Comments
 (0)
0