mirror of
https://github.com/python/cpython.git
synced 2025-08-08 10:58:51 +00:00
[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 a29998a06b
)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
parent
8785eab342
commit
6264c4f4b2
3 changed files with 9 additions and 1 deletions
|
@ -5655,6 +5655,12 @@ class ForwardRefTests(BaseTestCase):
|
||||||
with self.assertRaises(SyntaxError):
|
with self.assertRaises(SyntaxError):
|
||||||
get_type_hints(foo)
|
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 test_name_error(self):
|
||||||
|
|
||||||
def foo(a: 'Noode[T]'):
|
def foo(a: 'Noode[T]'):
|
||||||
|
|
|
@ -886,7 +886,7 @@ class ForwardRef(_Final, _root=True):
|
||||||
# If we do `def f(*args: *Ts)`, then we'll have `arg = '*Ts'`.
|
# 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
|
# Unfortunately, this isn't a valid expression on its own, so we
|
||||||
# do the unpacking manually.
|
# 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]
|
arg_to_compile = f'({arg},)[0]' # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
|
||||||
else:
|
else:
|
||||||
arg_to_compile = arg
|
arg_to_compile = arg
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
:mod:`typing`: raise :exc:`SyntaxError` instead of :exc:`AttributeError`
|
||||||
|
on forward references as empty strings.
|
Loading…
Add table
Add a link
Reference in a new issue