Skip to content

Commit 4994e57

Browse files
committed
gh-145241: specialize SyntaxError for single trailing-comma with item
1 parent c2d3d6b commit 4994e57

File tree

4 files changed

+667
-612
lines changed

4 files changed

+667
-612
lines changed

Grammar/python.gram

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,8 @@ invalid_import_from_targets:
14471447
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
14481448

14491449
invalid_with_stmt:
1450+
| ['async'] 'with' expression ['as' star_target] ',' ':' {
1451+
RAISE_SYNTAX_ERROR("single 'with' item has a trailing comma") }
14501452
| ['async'] 'with' ','.(expression ['as' star_target])+ NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
14511453
| ['async'] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
14521454
invalid_with_stmt_indent:

Lib/test/test_syntax.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,14 @@
20612061
Traceback (most recent call last):
20622062
SyntaxError: trailing comma not allowed without surrounding parentheses
20632063
2064+
>>> with item,: pass
2065+
Traceback (most recent call last):
2066+
SyntaxError: single 'with' item has a trailing comma
2067+
2068+
>>> with item as x,: pass
2069+
Traceback (most recent call last):
2070+
SyntaxError: single 'with' item has a trailing comma
2071+
20642072
>>> import a from b
20652073
Traceback (most recent call last):
20662074
SyntaxError: Did you mean to use 'from ... import ...' instead?
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Specialized the parser error for a single unparenthesized ``with`` item followed
2+
by a trailing comma (for example, ``with item,:``), raising a clearer
3+
:exc:`SyntaxError` message. Patch by Pablo Galindo.

0 commit comments

Comments
 (0)