Skip to content

Commit 29878c8

Browse files
committed
gh-138891: fix star-unpack in get_annotations
1 parent 537133d commit 29878c8

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
if self.__code__ is not None:
242242
return self.__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-
if arg.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
except SyntaxError:
254247
raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
255248
return self.__code__
@@ -987,7 +980,8 @@ def get_annotations(
987980
locals = {param.__name__: param for param in type_params} | locals
988981

989982
return_value = {
990-
key: value if not isinstance(value, str) else eval(value, globals, locals)
983+
key: value if not isinstance(value, str)
984+
else eval(_rewrite_star_unpack(value), globals, locals)
991985
for key, value in ann.items()
992986
}
993987
return return_value
@@ -1024,6 +1018,16 @@ def annotations_to_string(annotations):
10241018
}
10251019

10261020

1021+
def _rewrite_star_unpack(arg):
1022+
"""Rewrites the given argument annotation expression, when it is a star
1023+
unwrap e.g. `'*Ts'` to a valid expression.
1024+
"""
1025+
if arg.startswith("*"):
1026+
return f"({arg},)[0]" # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
1027+
else:
1028+
return arg
1029+
1030+
10271031
def _get_and_call_annotate(obj, format):
10281032
"""Get the __annotate__ function and call it.
10291033

Lib/test/test_annotationlib.py

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

788+
def test_stringized_annotations_with_star_unpack(self):
789+
def f(*args: *tuple[int, ...]): ...
790+
self.assertEqual(get_annotations(f, eval_str=True),
791+
{'args': (*tuple[int, ...],)[0]})
792+
793+
788794
def test_stringized_annotations_on_wrapper(self):
789795
isa = inspect_stringized_annotations
790796
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)