Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-119180: Make the FORWARDREF format work with more kinds of runtime errors #133407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ def get_annotations(
# For FORWARDREF, we use __annotations__ if it exists
try:
ann = _get_dunder_annotations(obj)
except NameError:
except Exception:
pass
else:
if ann is not None:
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,21 @@ def f(
},
)

def test_partial_evaluation_error(self):
def f(x: range[1]):
pass
with self.assertRaisesRegex(
TypeError, "type 'range' is not subscriptable"
):
f.__annotations__

self.assertEqual(
get_annotations(f, format=Format.FORWARDREF),
{
"x": support.EqualToForwardRef("range[1]", owner=f),
},
)

def test_partial_evaluation_cell(self):
obj = object()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Make :func:`annotationlib.get_annotations` succeed with the ``FORWARDREF``
format if evaluating the annotations throws an exception other than
:exc:`NameError` or :exc:`AttributeError`.
Loading