Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6264c4f

Browse filesBrowse 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 <mail@sobolevn.me>
1 parent 8785eab commit 6264c4f
Copy full SHA for 6264c4f

File tree

Expand file treeCollapse file tree

3 files changed

+9
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+9
-1
lines changed

‎Lib/test/test_typing.py

Copy file name to clipboardExpand all lines: Lib/test/test_typing.py
+6Lines 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

Copy file name to clipboardExpand all lines: Lib/typing.py
+1-1Lines 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
+2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.