]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-116325: Raise `SyntaxError` rather than `IndexError` on ForwardRef with empty...
authorNikita Sobolev <mail@sobolevn.me>
Tue, 5 Mar 2024 09:14:18 +0000 (12:14 +0300)
committerGitHub <noreply@github.com>
Tue, 5 Mar 2024 09:14:18 +0000 (09:14 +0000)
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2024-03-05-02-09-18.gh-issue-116325.FmlBYv.rst [new file with mode: 0644]

index dbbbe63e95ad466bb240cc1479fd0598828b1579..912384ab6bfe84a0afd8d12cfb608b36d082ef0c 100644 (file)
@@ -5867,6 +5867,12 @@ class ForwardRefTests(BaseTestCase):
         with self.assertRaises(SyntaxError):
             get_type_hints(foo)
 
+    def test_syntax_error_empty_string(self):
+        for form in [typing.List, typing.Set, typing.Type, typing.Deque]:
+            with self.subTest(form=form):
+                with self.assertRaises(SyntaxError):
+                    form['']
+
     def test_name_error(self):
 
         def foo(a: 'Noode[T]'):
index 2cb4a714ea3a859ed2d0af47b58cbeece74212c2..cca9525d632ea5f9d729034d46e19baa4901e942 100644 (file)
@@ -880,7 +880,7 @@ class ForwardRef(_Final, _root=True):
         # If we do `def f(*args: *Ts)`, then we'll have `arg = '*Ts'`.
         # Unfortunately, this isn't a valid expression on its own, so we
         # do the unpacking manually.
-        if arg[0] == '*':
+        if arg.startswith('*'):
             arg_to_compile = f'({arg},)[0]'  # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
         else:
             arg_to_compile = arg
diff --git a/Misc/NEWS.d/next/Library/2024-03-05-02-09-18.gh-issue-116325.FmlBYv.rst b/Misc/NEWS.d/next/Library/2024-03-05-02-09-18.gh-issue-116325.FmlBYv.rst
new file mode 100644 (file)
index 0000000..aec4ee7
--- /dev/null
@@ -0,0 +1,2 @@
+:mod:`typing`: raise :exc:`SyntaxError` instead of :exc:`AttributeError`
+on forward references as empty strings.