Skip to content

Commit f187da5

Browse files
[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 a29998a) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
1 parent 7fa3318 commit f187da5

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

Lib/test/test_typing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4953,6 +4953,12 @@ def foo(a: 'Node[T'):
49534953
with self.assertRaises(SyntaxError):
49544954
get_type_hints(foo)
49554955

4956+
def test_syntax_error_empty_string(self):
4957+
for form in [typing.List, typing.Set, typing.Type, typing.Deque]:
4958+
with self.subTest(form=form):
4959+
with self.assertRaises(SyntaxError):
4960+
form['']
4961+
49564962
def test_name_error(self):
49574963

49584964
def foo(a: 'Noode[T]'):

Lib/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ def __init__(self, arg, is_argument=True, module=None, *, is_class=False):
870870
# If we do `def f(*args: *Ts)`, then we'll have `arg = '*Ts'`.
871871
# Unfortunately, this isn't a valid expression on its own, so we
872872
# do the unpacking manually.
873-
if arg[0] == '*':
873+
if arg.startswith('*'):
874874
arg_to_compile = f'({arg},)[0]' # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
875875
else:
876876
arg_to_compile = arg
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`typing`: raise :exc:`SyntaxError` instead of :exc:`AttributeError`
2+
on forward references as empty strings.

0 commit comments

Comments
 (0)