https://github.com/python/cpython/commit/f187da5b596f5ec16f060fc7a3113e149507c1fc
commit: f187da5b596f5ec16f060fc7a3113e149507c1fc
branch: 3.11
author: Miss Islington (bot) <[email protected]>
committer: sobolevn <[email protected]>
date: 2024-03-05T10:15:54Z
summary:

[3.11] gh-116325: Raise `SyntaxError` rather than `IndexError` on ForwardRef 
with empty string arg (GH-116341) (#116348)

gh-116325: Raise `SyntaxError` rather than `IndexError` on ForwardRef with 
empty string arg (GH-116341)
(cherry picked from commit a29998a06bf75264c3faaeeec4584a5f75b45a1f)

Co-authored-by: Nikita Sobolev <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-03-05-02-09-18.gh-issue-116325.FmlBYv.rst
M Lib/test/test_typing.py
M Lib/typing.py

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index b2e4c901b737ee..ab430e3f76d150 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -4953,6 +4953,12 @@ def foo(a: 'Node[T'):
         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]'):
diff --git a/Lib/typing.py b/Lib/typing.py
index 45c6fbde46af54..e3c9e6e7867210 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -870,7 +870,7 @@ def __init__(self, arg, is_argument=True, module=None, *, 
is_class=False):
         # 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
index 00000000000000..aec4ee79b59cf2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-03-05-02-09-18.gh-issue-116325.FmlBYv.rst
@@ -0,0 +1,2 @@
+:mod:`typing`: raise :exc:`SyntaxError` instead of :exc:`AttributeError`
+on forward references as empty strings.

_______________________________________________
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