Skip to content

Commit 6264c4f

Browse files
[3.12] gh-116325: Raise SyntaxError rather than IndexError on ForwardRef with empty string arg (GH-116341) (#116347)
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 <[email protected]>
1 parent 8785eab commit 6264c4f

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
@@ -5655,6 +5655,12 @@ def foo(a: 'Node[T'):
56555655
with self.assertRaises(SyntaxError):
56565656
get_type_hints(foo)
56575657

5658+
def test_syntax_error_empty_string(self):
5659+
for form in [typing.List, typing.Set, typing.Type, typing.Deque]:
5660+
with self.subTest(form=form):
5661+
with self.assertRaises(SyntaxError):
5662+
form['']
5663+
56585664
def test_name_error(self):
56595665

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

Lib/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ def __init__(self, arg, is_argument=True, module=None, *, is_class=False):
886886
# If we do `def f(*args: *Ts)`, then we'll have `arg = '*Ts'`.
887887
# Unfortunately, this isn't a valid expression on its own, so we
888888
# do the unpacking manually.
889-
if arg[0] == '*':
889+
if arg.startswith('*'):
890890
arg_to_compile = f'({arg},)[0]' # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
891891
else:
892892
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)