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

Commit62f44dd

Browse files
[3.14]gh-138891: fix star-unpack in get_annotations (GH-138951) (#140384)
gh-138891: fix star-unpack in get_annotations (GH-138951)(cherry picked from commitc6be6e4)Co-authored-by: Christoph Walcher <christoph-wa@gmx.de>
1 parent8e93f6e commit62f44dd

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

‎Lib/annotationlib.py‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,8 @@ def __forward_code__(self):
241241
ifself.__code__isnotNone:
242242
returnself.__code__
243243
arg=self.__forward_arg__
244-
# If we do `def f(*args: *Ts)`, then we'll have `arg = '*Ts'`.
245-
# Unfortunately, this isn't a valid expression on its own, so we
246-
# do the unpacking manually.
247-
ifarg.startswith("*"):
248-
arg_to_compile=f"({arg},)[0]"# E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
249-
else:
250-
arg_to_compile=arg
251244
try:
252-
self.__code__=compile(arg_to_compile,"<string>","eval")
245+
self.__code__=compile(_rewrite_star_unpack(arg),"<string>","eval")
253246
exceptSyntaxError:
254247
raiseSyntaxError(f"Forward reference must be an expression -- got{arg!r}")
255248
returnself.__code__
@@ -1025,7 +1018,8 @@ def get_annotations(
10251018
locals= {param.__name__:paramforparamintype_params}|locals
10261019

10271020
return_value= {
1028-
key:valueifnotisinstance(value,str)elseeval(value,globals,locals)
1021+
key:valueifnotisinstance(value,str)
1022+
elseeval(_rewrite_star_unpack(value),globals,locals)
10291023
forkey,valueinann.items()
10301024
}
10311025
returnreturn_value
@@ -1062,6 +1056,16 @@ def annotations_to_string(annotations):
10621056
}
10631057

10641058

1059+
def_rewrite_star_unpack(arg):
1060+
"""If the given argument annotation expression is a star unpack e.g. `'*Ts'`
1061+
rewrite it to a valid expression.
1062+
"""
1063+
ifarg.startswith("*"):
1064+
returnf"({arg},)[0]"# E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
1065+
else:
1066+
returnarg
1067+
1068+
10651069
def_get_and_call_annotate(obj,format):
10661070
"""Get the __annotate__ function and call it.
10671071

‎Lib/test/test_annotationlib.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,12 @@ def test_stringized_annotations_in_empty_module(self):
787787
self.assertEqual(get_annotations(isa2,eval_str=True), {})
788788
self.assertEqual(get_annotations(isa2,eval_str=False), {})
789789

790+
deftest_stringized_annotations_with_star_unpack(self):
791+
deff(*args:*tuple[int, ...]): ...
792+
self.assertEqual(get_annotations(f,eval_str=True),
793+
{'args': (*tuple[int, ...],)[0]})
794+
795+
790796
deftest_stringized_annotations_on_wrapper(self):
791797
isa=inspect_stringized_annotations
792798
wrapped=times_three(isa.function)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``SyntaxError`` when ``inspect.get_annotations(f, eval_str=True)`` is
2+
called on a function annotated with a:pep:`646` ``star_expression``

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp