gh-138764: annotationlib: Make call_annotate_function fallback to using VALUE annotations if both the requested format and VALUE_WITH_FAKE_GLOBALS are not implemented (#138803)

This commit is contained in:
David Ellis 2025-10-21 16:57:43 +01:00 committed by GitHub
parent c788bfb80e
commit 95c257e2e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 209 additions and 0 deletions

View file

@ -695,6 +695,18 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
# possibly constants if the annotate function uses them directly). We then
# convert each of those into a string to get an approximation of the
# original source.
# Attempt to call with VALUE_WITH_FAKE_GLOBALS to check if it is implemented
# See: https://github.com/python/cpython/issues/138764
# Only fail on NotImplementedError
try:
annotate(Format.VALUE_WITH_FAKE_GLOBALS)
except NotImplementedError:
# Both STRING and VALUE_WITH_FAKE_GLOBALS are not implemented: fallback to VALUE
return annotations_to_string(annotate(Format.VALUE))
except Exception:
pass
globals = _StringifierDict({}, format=format)
is_class = isinstance(owner, type)
closure = _build_closure(
@ -753,6 +765,9 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
)
try:
result = func(Format.VALUE_WITH_FAKE_GLOBALS)
except NotImplementedError:
# FORWARDREF and VALUE_WITH_FAKE_GLOBALS not supported, fall back to VALUE
return annotate(Format.VALUE)
except Exception:
pass
else: