Skip to content

Commit 09e8c38

Browse files
gh-145241: specialize SyntaxError for single trailing-comma with item (#145282)
Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
1 parent 2e2109d commit 09e8c38

File tree

4 files changed

+221
-154
lines changed

4 files changed

+221
-154
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])+ trailing=',' ':' {
1451+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(trailing, "the last '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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,30 @@
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: the last 'with' item has a trailing comma
2067+
2068+
>>> with item as x,: pass
2069+
Traceback (most recent call last):
2070+
SyntaxError: the last 'with' item has a trailing comma
2071+
2072+
>>> with item1, item2,: pass
2073+
Traceback (most recent call last):
2074+
SyntaxError: the last 'with' item has a trailing comma
2075+
2076+
>>> with item1 as x, item2,: pass
2077+
Traceback (most recent call last):
2078+
SyntaxError: the last 'with' item has a trailing comma
2079+
2080+
>>> with item1 as x, item2 as y,: pass
2081+
Traceback (most recent call last):
2082+
SyntaxError: the last 'with' item has a trailing comma
2083+
2084+
>>> with item1, item2 as y,: pass
2085+
Traceback (most recent call last):
2086+
SyntaxError: the last 'with' item has a trailing comma
2087+
20642088
>>> import a from b
20652089
Traceback (most recent call last):
20662090
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 when ``with`` items are followed
2+
by a trailing comma (for example, ``with item,:``), raising a clearer
3+
:exc:`SyntaxError` message. Patch by Pablo Galindo and Bartosz Sławecki.

0 commit comments

Comments
 (0)