diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-03-05 10:08:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-05 10:08:27 (GMT) |
commit | 6264c4f4b2f99ba71678e1ff29347e6b3136f51c (patch) | |
tree | 7a84cace38ca81054b18dacd5cbdcd909a888a4a /Lib | |
parent | 8785eab34255dd783a94b1e0a8d388a27333578c (diff) | |
download | cpython-6264c4f4b2f99ba71678e1ff29347e6b3136f51c.zip cpython-6264c4f4b2f99ba71678e1ff29347e6b3136f51c.tar.gz cpython-6264c4f4b2f99ba71678e1ff29347e6b3136f51c.tar.bz2 |
[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 a29998a06bf75264c3faaeeec4584a5f75b45a1f)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_typing.py | 6 | ||||
-rw-r--r-- | Lib/typing.py | 2 |
2 files changed, 7 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index e0f7146..8ad26a7 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -5655,6 +5655,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]'): diff --git a/Lib/typing.py b/Lib/typing.py index 7581c16..1d8feed 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -886,7 +886,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 |