Skip to content

Commit a29998a

Browse files
authored
gh-116325: Raise SyntaxError rather than IndexError on ForwardRef with empty string arg (#116341)
1 parent ffcc450 commit a29998a

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
@@ -5867,6 +5867,12 @@ def foo(a: 'Node[T'):
58675867
with self.assertRaises(SyntaxError):
58685868
get_type_hints(foo)
58695869

5870+
def test_syntax_error_empty_string(self):
5871+
for form in [typing.List, typing.Set, typing.Type, typing.Deque]:
5872+
with self.subTest(form=form):
5873+
with self.assertRaises(SyntaxError):
5874+
form['']
5875+
58705876
def test_name_error(self):
58715877

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

Lib/typing.py

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

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy