]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-143517: Fix an edge case in rewriting stringified starred annotations (#143518)
authorBartosz Sławecki <bartosz@ilikepython.com>
Thu, 8 Jan 2026 03:41:27 +0000 (04:41 +0100)
committerGitHub <noreply@github.com>
Thu, 8 Jan 2026 03:41:27 +0000 (19:41 -0800)
Lib/annotationlib.py
Lib/test/test_annotationlib.py
Misc/NEWS.d/next/Library/2026-01-07-15-49-06.gh-issue-143517.FP5KgL.rst [new file with mode: 0644]

index a5788cdbfae3f5f7a0c0962605108796468362fc..4085cc6bef7954961d54b1c1b1c2b1277fe68c2a 100644 (file)
@@ -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
index 8208d0e9c94819719948674352c64b119b3d18d2..a8537871d294cfba81f7c6d5e9d28cf23a62faf7 100644 (file)
@@ -885,6 +885,9 @@ class TestGetAnnotations(unittest.TestCase):
         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 (file)
index 0000000..a9936b5
--- /dev/null
@@ -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.