https://github.com/python/cpython/commit/6c9f7b4406d507625ff414cddc549d4c630c59c5
commit: 6c9f7b4406d507625ff414cddc549d4c630c59c5
branch: main
author: Bartosz SÅ‚awecki <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-01-07T19:41:27-08:00
summary:

gh-143517: Fix an edge case in rewriting stringified starred annotations 
(#143518)

files:
A Misc/NEWS.d/next/Library/2026-01-07-15-49-06.gh-issue-143517.FP5KgL.rst
M Lib/annotationlib.py
M Lib/test/test_annotationlib.py

diff --git a/Lib/annotationlib.py b/Lib/annotationlib.py
index a5788cdbfae3f5..4085cc6bef7954 100644
--- a/Lib/annotationlib.py
+++ b/Lib/annotationlib.py
@@ -1096,7 +1096,7 @@ def _rewrite_star_unpack(arg):
     """If the given argument annotation expression is a star unpack e.g. 
`'*Ts'`
        rewrite it to a valid expression.
        """
-    if arg.startswith("*"):
+    if arg.lstrip().startswith("*"):
         return f"({arg},)[0]"  # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
     else:
         return arg
diff --git a/Lib/test/test_annotationlib.py b/Lib/test/test_annotationlib.py
index 8208d0e9c94819..a8537871d294cf 100644
--- a/Lib/test/test_annotationlib.py
+++ b/Lib/test/test_annotationlib.py
@@ -885,6 +885,9 @@ def test_stringized_annotations_with_star_unpack(self):
         def f(*args: "*tuple[int, ...]"): ...
         self.assertEqual(get_annotations(f, eval_str=True),
                          {'args': (*tuple[int, ...],)[0]})
+        def f(*args: " *tuple[int, ...]"): ...
+        self.assertEqual(get_annotations(f, eval_str=True),
+                         {'args': (*tuple[int, ...],)[0]})
 
 
     def test_stringized_annotations_on_wrapper(self):
diff --git 
a/Misc/NEWS.d/next/Library/2026-01-07-15-49-06.gh-issue-143517.FP5KgL.rst 
b/Misc/NEWS.d/next/Library/2026-01-07-15-49-06.gh-issue-143517.FP5KgL.rst
new file mode 100644
index 00000000000000..a9936b5d018692
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-01-07-15-49-06.gh-issue-143517.FP5KgL.rst
@@ -0,0 +1,4 @@
+:func:`annotationlib.get_annotations` no longer raises a :exc:`SyntaxError`
+when evaluating a stringified starred annotation that starts with one
+or more whitespace characters followed by a ``*``.
+Patch by Bartosz Sławecki.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to