Skip to content

Commit 1b376b8

Browse files
[3.14] Revert "pythongh-137969: Fix evaluation of ref.evaluate(format=Format.FORWARDREF) objects (pythonGH-138075) (python#140929)" (pythonGH-140931)
Revert "[3.14] pythongh-137969: Fix evaluation of `ref.evaluate(format=Format.FORWARDREF)` objects (pythonGH-138075) (python#140929)" This reverts commit cdb6fe8.
1 parent fa9bb9a commit 1b376b8

File tree

3 files changed

+6
-19
lines changed

3 files changed

+6
-19
lines changed

Lib/annotationlib.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,19 @@ def evaluate(
159159
type_params = getattr(owner, "__type_params__", None)
160160

161161
# Type parameters exist in their own scope, which is logically
162-
# between the locals and the globals.
163-
type_param_scope = {}
162+
# between the locals and the globals. We simulate this by adding
163+
# them to the globals.
164164
if type_params is not None:
165+
globals = dict(globals)
165166
for param in type_params:
166-
type_param_scope[param.__name__] = param
167-
167+
globals[param.__name__] = param
168168
if self.__extra_names__:
169169
locals = {**locals, **self.__extra_names__}
170170

171171
arg = self.__forward_arg__
172172
if arg.isidentifier() and not keyword.iskeyword(arg):
173173
if arg in locals:
174174
return locals[arg]
175-
elif arg in type_param_scope:
176-
return type_param_scope[arg]
177175
elif arg in globals:
178176
return globals[arg]
179177
elif hasattr(builtins, arg):
@@ -185,15 +183,15 @@ def evaluate(
185183
else:
186184
code = self.__forward_code__
187185
try:
188-
return eval(code, globals=globals, locals={**type_param_scope, **locals})
186+
return eval(code, globals=globals, locals=locals)
189187
except Exception:
190188
if not is_forwardref_format:
191189
raise
192190

193191
# All variables, in scoping order, should be checked before
194192
# triggering __missing__ to create a _Stringifier.
195193
new_locals = _StringifierDict(
196-
{**builtins.__dict__, **globals, **type_param_scope, **locals},
194+
{**builtins.__dict__, **globals, **locals},
197195
globals=globals,
198196
owner=owner,
199197
is_class=self.__forward_is_class__,

Lib/test/test_annotationlib.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,15 +1911,6 @@ def test_fwdref_invalid_syntax(self):
19111911
with self.assertRaises(SyntaxError):
19121912
fr.evaluate()
19131913

1914-
def test_re_evaluate_generics(self):
1915-
global alias
1916-
class C:
1917-
x: alias[int]
1918-
1919-
evaluated = get_annotations(C, format=Format.FORWARDREF)["x"].evaluate(format=Format.FORWARDREF)
1920-
alias = list
1921-
self.assertEqual(evaluated.evaluate(), list[int])
1922-
19231914

19241915
class TestAnnotationLib(unittest.TestCase):
19251916
def test__all__(self):

Misc/NEWS.d/next/Library/2025-08-22-23-50-38.gh-issue-137969.Fkvis3.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)