gh-133960: Improve typing.evaluate_forward_ref (#133961)

As explained in #133960, this removes most of the behavior differences with ForwardRef.evaluate.
The remaining difference is about recursive evaluation of forwardrefs; this is practically useful
in cases where an annotation refers to a type alias that itself is string-valued.

This also improves several edge cases that were previously not handled optimally. For example,
the function now takes advantage of the partial evaluation behavior of ForwardRef.evaluate() to
evaluate more ForwardRefs in the FORWARDREF format.

This also fixes #133959 as a side effect, because the buggy behavior in #133959 derives from
evaluate_forward_ref().
This commit is contained in:
Jelle Zijlstra 2025-05-25 10:26:39 -07:00 committed by GitHub
parent b51b08a0a5
commit 57fef27cfc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 132 additions and 56 deletions

View file

@ -6859,12 +6859,10 @@ class GetTypeHintsTests(BaseTestCase):
self.assertEqual(hints, {'value': Final})
def test_top_level_class_var(self):
# https://bugs.python.org/issue45166
with self.assertRaisesRegex(
TypeError,
r'typing.ClassVar\[int\] is not valid as type argument',
):
get_type_hints(ann_module6)
# This is not meaningful but we don't raise for it.
# https://github.com/python/cpython/issues/133959
hints = get_type_hints(ann_module6)
self.assertEqual(hints, {'wrong': ClassVar[int]})
def test_get_type_hints_typeddict(self):
self.assertEqual(get_type_hints(TotalMovie), {'title': str, 'year': int})
@ -6967,6 +6965,11 @@ class GetTypeHintsTests(BaseTestCase):
self.assertEqual(get_type_hints(foo, globals(), locals()),
{'a': Callable[..., T]})
def test_special_forms_no_forward(self):
def f(x: ClassVar[int]):
pass
self.assertEqual(get_type_hints(f), {'x': ClassVar[int]})
def test_special_forms_forward(self):
class C:
@ -6982,8 +6985,9 @@ class GetTypeHintsTests(BaseTestCase):
self.assertEqual(get_type_hints(C, globals())['b'], Final[int])
self.assertEqual(get_type_hints(C, globals())['x'], ClassVar)
self.assertEqual(get_type_hints(C, globals())['y'], Final)
with self.assertRaises(TypeError):
get_type_hints(CF, globals()),
lfi = get_type_hints(CF, globals())['b']
self.assertIs(get_origin(lfi), list)
self.assertEqual(get_args(lfi), (Final[int],))
def test_union_forward_recursion(self):
ValueList = List['Value']
@ -7216,33 +7220,113 @@ class GetUtilitiesTestCase(TestCase):
class EvaluateForwardRefTests(BaseTestCase):
def test_evaluate_forward_ref(self):
int_ref = ForwardRef('int')
missing = ForwardRef('missing')
self.assertIs(typing.evaluate_forward_ref(int_ref), int)
self.assertIs(
typing.evaluate_forward_ref(int_ref, type_params=()),
int,
)
self.assertIs(
typing.evaluate_forward_ref(
int_ref, type_params=(), format=annotationlib.Format.FORWARDREF,
),
typing.evaluate_forward_ref(int_ref, format=annotationlib.Format.VALUE),
int,
)
self.assertIs(
typing.evaluate_forward_ref(
missing, type_params=(), format=annotationlib.Format.FORWARDREF,
int_ref, format=annotationlib.Format.FORWARDREF,
),
int,
)
self.assertEqual(
typing.evaluate_forward_ref(
int_ref, format=annotationlib.Format.STRING,
),
'int',
)
def test_evaluate_forward_ref_undefined(self):
missing = ForwardRef('missing')
with self.assertRaises(NameError):
typing.evaluate_forward_ref(missing)
self.assertIs(
typing.evaluate_forward_ref(
missing, format=annotationlib.Format.FORWARDREF,
),
missing,
)
self.assertEqual(
typing.evaluate_forward_ref(
int_ref, type_params=(), format=annotationlib.Format.STRING,
missing, format=annotationlib.Format.STRING,
),
'int',
"missing",
)
def test_evaluate_forward_ref_no_type_params(self):
ref = ForwardRef('int')
self.assertIs(typing.evaluate_forward_ref(ref), int)
def test_evaluate_forward_ref_nested(self):
ref = ForwardRef("int | list['str']")
self.assertEqual(
typing.evaluate_forward_ref(ref),
int | list[str],
)
self.assertEqual(
typing.evaluate_forward_ref(ref, format=annotationlib.Format.FORWARDREF),
int | list[str],
)
self.assertEqual(
typing.evaluate_forward_ref(ref, format=annotationlib.Format.STRING),
"int | list['str']",
)
why = ForwardRef('"\'str\'"')
self.assertIs(typing.evaluate_forward_ref(why), str)
def test_evaluate_forward_ref_none(self):
none_ref = ForwardRef('None')
self.assertIs(typing.evaluate_forward_ref(none_ref), None)
def test_globals(self):
A = "str"
ref = ForwardRef('list[A]')
with self.assertRaises(NameError):
typing.evaluate_forward_ref(ref)
self.assertEqual(
typing.evaluate_forward_ref(ref, globals={'A': A}),
list[str],
)
def test_owner(self):
ref = ForwardRef("A")
with self.assertRaises(NameError):
typing.evaluate_forward_ref(ref)
# We default to the globals of `owner`,
# so it no longer raises `NameError`
self.assertIs(
typing.evaluate_forward_ref(ref, owner=Loop), A
)
def test_inherited_owner(self):
# owner passed to evaluate_forward_ref
ref = ForwardRef("list['A']")
self.assertEqual(
typing.evaluate_forward_ref(ref, owner=Loop),
list[A],
)
# owner set on the ForwardRef
ref = ForwardRef("list['A']", owner=Loop)
self.assertEqual(
typing.evaluate_forward_ref(ref),
list[A],
)
def test_partial_evaluation(self):
ref = ForwardRef("list[A]")
with self.assertRaises(NameError):
typing.evaluate_forward_ref(ref)
self.assertEqual(
typing.evaluate_forward_ref(ref, format=annotationlib.Format.FORWARDREF),
list[EqualToForwardRef('A')],
)
class CollectionsAbcTests(BaseTestCase):