Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit63e01d6

Browse files
pythongh-137969: Fix evaluation ofref.evaluate(format=Format.FORWARDREF) objects (python#138075)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parente66f87c commit63e01d6

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

‎Lib/annotationlib.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,21 @@ 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. We simulate this by adding
163-
# them to the globals.
162+
# between the locals and the globals.
163+
type_param_scope= {}
164164
iftype_paramsisnotNone:
165-
globals=dict(globals)
166165
forparamintype_params:
167-
globals[param.__name__]=param
166+
type_param_scope[param.__name__]=param
167+
168168
ifself.__extra_names__:
169169
locals= {**locals,**self.__extra_names__}
170170

171171
arg=self.__forward_arg__
172172
ifarg.isidentifier()andnotkeyword.iskeyword(arg):
173173
ifarginlocals:
174174
returnlocals[arg]
175+
elifargintype_param_scope:
176+
returntype_param_scope[arg]
175177
elifarginglobals:
176178
returnglobals[arg]
177179
elifhasattr(builtins,arg):
@@ -183,15 +185,15 @@ def evaluate(
183185
else:
184186
code=self.__forward_code__
185187
try:
186-
returneval(code,globals=globals,locals=locals)
188+
returneval(code,globals=globals,locals={**type_param_scope,**locals})
187189
exceptException:
188190
ifnotis_forwardref_format:
189191
raise
190192

191193
# All variables, in scoping order, should be checked before
192194
# triggering __missing__ to create a _Stringifier.
193195
new_locals=_StringifierDict(
194-
{**builtins.__dict__,**globals,**locals},
196+
{**builtins.__dict__,**globals,**type_param_scope,**locals},
195197
globals=globals,
196198
owner=owner,
197199
is_class=self.__forward_is_class__,

‎Lib/test/test_annotationlib.py‎

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

1914+
deftest_re_evaluate_generics(self):
1915+
globalalias
1916+
classC:
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+
19141923

19151924
classTestAnnotationLib(unittest.TestCase):
19161925
deftest__all__(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix:meth:`annotationlib.ForwardRef.evaluate` returning:class:`annotationlib.ForwardRef`
2+
objects which do not update in new contexts.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp