Skip to content

Commit 25ab548

Browse files
committed
[3.14] pythongh-137226: Fix get_type_hints() on generic TypedDict with stringified annotations (pythonGH-138953)
This issue appears specifically for TypedDicts because the TypedDict constructor code converts string annotations to ForwardRef objects, and those are not evaluated properly by the get_type_hints() stack because of other shenanigans with type parameters. This issue does not affect normal generic classes because their annotations are not pre-converted to ForwardRefs. The fix attempts to restore the pre- pythonGH-137227 behavior in the narrow scenario where the issue manifests. It mostly makes changes only in the paths accessible from get_type_hints(), ensuring that newer APIs (such as evaluate_forward_ref() and annotationlib) are not affected by get_type_hints()'s past odd choices. This PR does not fix issue pythonGH-138949, an older issue I discovered while playing around with this one; we'll need a separate and perhaps more invasive fix for that, but it should wait until after 3.14.0. (cherry picked from commit 6d6aba2) Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 284ab76 commit 25ab548

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

Lib/test/test_typing.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7132,6 +7132,19 @@ def add_right(self, node: 'Node[T]' = None):
71327132
right_hints = get_type_hints(t.add_right, globals(), locals())
71337133
self.assertEqual(right_hints['node'], Node[T])
71347134

7135+
def test_stringified_typeddict(self):
7136+
ns = run_code(
7137+
"""
7138+
from __future__ import annotations
7139+
from typing import TypedDict
7140+
class TD[UniqueT](TypedDict):
7141+
a: UniqueT
7142+
"""
7143+
)
7144+
TD = ns['TD']
7145+
self.assertEqual(TD.__annotations__, {'a': EqualToForwardRef('UniqueT', owner=TD, module=TD.__module__)})
7146+
self.assertEqual(get_type_hints(TD), {'a': TD.__type_params__[0]})
7147+
71357148

71367149
class GetUtilitiesTestCase(TestCase):
71377150
def test_get_origin(self):
@@ -8668,8 +8681,8 @@ def _make_td(future, class_name, annos, base, extra_names=None):
86688681
child = _make_td(
86698682
child_future, "Child", {"child": "int"}, "Base", {"Base": base}
86708683
)
8671-
base_anno = ForwardRef("int", module="builtins") if base_future else int
8672-
child_anno = ForwardRef("int", module="builtins") if child_future else int
8684+
base_anno = ForwardRef("int", module="builtins", owner=base) if base_future else int
8685+
child_anno = ForwardRef("int", module="builtins", owner=child) if child_future else int
86738686
self.assertEqual(base.__annotations__, {'base': base_anno})
86748687
self.assertEqual(
86758688
child.__annotations__, {'child': child_anno, 'base': base_anno}

Lib/typing.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,16 @@ def __getattr__(self, attr):
171171
_lazy_annotationlib = _LazyAnnotationLib()
172172

173173

174-
def _type_convert(arg, module=None, *, allow_special_forms=False):
174+
def _type_convert(arg, module=None, *, allow_special_forms=False, owner=None):
175175
"""For converting None to type(None), and strings to ForwardRef."""
176176
if arg is None:
177177
return type(None)
178178
if isinstance(arg, str):
179-
return _make_forward_ref(arg, module=module, is_class=allow_special_forms)
179+
return _make_forward_ref(arg, module=module, is_class=allow_special_forms, owner=owner)
180180
return arg
181181

182182

183-
def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=False):
183+
def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=False, owner=None):
184184
"""Check that the argument is a type, and return it (internal helper).
185185
186186
As a special case, accept None and return type(None) instead. Also wrap strings
@@ -198,7 +198,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
198198
if is_argument:
199199
invalid_generic_forms += (Final,)
200200

201-
arg = _type_convert(arg, module=module, allow_special_forms=allow_special_forms)
201+
arg = _type_convert(arg, module=module, allow_special_forms=allow_special_forms, owner=owner)
202202
if (isinstance(arg, _GenericAlias) and
203203
arg.__origin__ in invalid_generic_forms):
204204
raise TypeError(f"{arg} is not valid as type argument")
@@ -430,7 +430,7 @@ def __repr__(self):
430430

431431

432432
def _eval_type(t, globalns, localns, type_params=_sentinel, *, recursive_guard=frozenset(),
433-
format=None, owner=None, parent_fwdref=None):
433+
format=None, owner=None, parent_fwdref=None, prefer_fwd_module=False):
434434
"""Evaluate all forward references in the given type t.
435435
436436
For use of globalns and localns see the docstring for get_type_hints().
@@ -443,8 +443,20 @@ def _eval_type(t, globalns, localns, type_params=_sentinel, *, recursive_guard=f
443443
if isinstance(t, _lazy_annotationlib.ForwardRef):
444444
# If the forward_ref has __forward_module__ set, evaluate() infers the globals
445445
# from the module, and it will probably pick better than the globals we have here.
446-
if t.__forward_module__ is not None:
446+
# We do this only for calls from get_type_hints() (which opts in through the
447+
# prefer_fwd_module flag), so that the default behavior remains more straightforward.
448+
if prefer_fwd_module and t.__forward_module__ is not None:
447449
globalns = None
450+
# If there are type params on the owner, we need to add them back, because
451+
# annotationlib won't.
452+
if owner_type_params := getattr(owner, "__type_params__", None):
453+
globalns = getattr(
454+
sys.modules.get(t.__forward_module__, None), "__dict__", None
455+
)
456+
if globalns is not None:
457+
globalns = dict(globalns)
458+
for type_param in owner_type_params:
459+
globalns[type_param.__name__] = type_param
448460
return evaluate_forward_ref(t, globals=globalns, locals=localns,
449461
type_params=type_params, owner=owner,
450462
_recursive_guard=recursive_guard, format=format)
@@ -465,7 +477,7 @@ def _eval_type(t, globalns, localns, type_params=_sentinel, *, recursive_guard=f
465477
ev_args = tuple(
466478
_eval_type(
467479
a, globalns, localns, type_params, recursive_guard=recursive_guard,
468-
format=format, owner=owner,
480+
format=format, owner=owner, prefer_fwd_module=prefer_fwd_module,
469481
)
470482
for a in t.__args__
471483
)
@@ -2347,7 +2359,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
23472359
if isinstance(value, str):
23482360
value = _make_forward_ref(value, is_argument=False, is_class=True)
23492361
value = _eval_type(value, base_globals, base_locals, (),
2350-
format=format, owner=obj)
2362+
format=format, owner=obj, prefer_fwd_module=True)
23512363
if value is None:
23522364
value = type(None)
23532365
hints[name] = value
@@ -2392,7 +2404,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
23922404
is_argument=not isinstance(obj, types.ModuleType),
23932405
is_class=False,
23942406
)
2395-
value = _eval_type(value, globalns, localns, (), format=format, owner=obj)
2407+
value = _eval_type(value, globalns, localns, (), format=format, owner=obj, prefer_fwd_module=True)
23962408
if value is None:
23972409
value = type(None)
23982410
hints[name] = value
@@ -3128,7 +3140,7 @@ def __new__(cls, name, bases, ns, total=True):
31283140
own_annotations = {}
31293141
msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
31303142
own_checked_annotations = {
3131-
n: _type_check(tp, msg, module=tp_dict.__module__)
3143+
n: _type_check(tp, msg, owner=tp_dict, module=tp_dict.__module__)
31323144
for n, tp in own_annotations.items()
31333145
}
31343146
required_keys = set()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`typing.get_type_hints` calls on generic :class:`typing.TypedDict`
2+
classes defined with string annotations.

0 commit comments

Comments
 (0)