Skip to content

Commit 879abeb

Browse files
committed
pythongh-114053: Fix another edge case involving get_type_hints, PEP 695 and PEP 563
1 parent 38a25e9 commit 879abeb

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

Lib/test/test_typing.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4866,11 +4866,7 @@ def test_pep695_generic_with_future_annotations(self):
48664866
self.assertIs(hints_for_A["z"].__args__[0], A_type_params[2])
48674867

48684868
hints_for_B = get_type_hints(ann_module695.B)
4869-
self.assertEqual(hints_for_B.keys(), {"x", "y", "z"})
4870-
self.assertEqual(
4871-
set(hints_for_B.values()) ^ set(ann_module695.B.__type_params__),
4872-
set()
4873-
)
4869+
self.assertEqual(hints_for_B, {"x": int, "y": str, "z": bytes})
48744870

48754871
hints_for_generic_function = get_type_hints(ann_module695.generic_function)
48764872
func_t_params = ann_module695.generic_function.__type_params__

Lib/typing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,11 +1064,11 @@ def _evaluate(self, globalns, localns, type_params=_sentinel, *, recursive_guard
10641064
# "Inject" type parameters into the local namespace
10651065
# (unless they are shadowed by assignments *in* the local namespace),
10661066
# as a way of emulating annotation scopes when calling `eval()`
1067-
locals_to_pass = {param.__name__: param for param in type_params} | localns
1067+
globals_to_pass = {param.__name__: param for param in type_params} | globalns
10681068
else:
1069-
locals_to_pass = localns
1069+
globals_to_pass = globalns
10701070
type_ = _type_check(
1071-
eval(self.__forward_code__, globalns, locals_to_pass),
1071+
eval(self.__forward_code__, globals_to_pass, localns),
10721072
"Forward references must evaluate to types.",
10731073
is_argument=self.__forward_is_argument__,
10741074
allow_special_forms=self.__forward_is_class__,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix edge-case bug where :func:`typing.get_type_hints` would produce
2+
incorrect results if type parameters in a class scope were overridden by
3+
assignments in a class scope and ``from __future__ import annotations``
4+
semantics were enabled. Patch by Alex Waygood.

0 commit comments

Comments
 (0)