Skip to content

Commit e8ac427

Browse files
[3.14] gh-143517: Fix an edge case in rewriting stringified starred annotations (GH-143518) (#143540)
gh-143517: Fix an edge case in rewriting stringified starred annotations (GH-143518) (cherry picked from commit 6c9f7b4) Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
1 parent 9ab5a24 commit e8ac427

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

Lib/annotationlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ def _rewrite_star_unpack(arg):
10961096
"""If the given argument annotation expression is a star unpack e.g. `'*Ts'`
10971097
rewrite it to a valid expression.
10981098
"""
1099-
if arg.startswith("*"):
1099+
if arg.lstrip().startswith("*"):
11001100
return f"({arg},)[0]" # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
11011101
else:
11021102
return arg

Lib/test/test_annotationlib.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,9 @@ def test_stringized_annotations_with_star_unpack(self):
885885
def f(*args: "*tuple[int, ...]"): ...
886886
self.assertEqual(get_annotations(f, eval_str=True),
887887
{'args': (*tuple[int, ...],)[0]})
888+
def f(*args: " *tuple[int, ...]"): ...
889+
self.assertEqual(get_annotations(f, eval_str=True),
890+
{'args': (*tuple[int, ...],)[0]})
888891

889892

890893
def test_stringized_annotations_on_wrapper(self):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:func:`annotationlib.get_annotations` no longer raises a :exc:`SyntaxError`
2+
when evaluating a stringified starred annotation that starts with one
3+
or more whitespace characters followed by a ``*``.
4+
Patch by Bartosz Sławecki.

0 commit comments

Comments
 (0)