Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,8 @@ invalid_import_from_targets:
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }

invalid_with_stmt:
| ['async'] 'with' expression ['as' star_target] ',' ':' {
RAISE_SYNTAX_ERROR("single 'with' item has a trailing comma") }
Comment on lines +1450 to +1451
Copy link
Member

@johnslavik johnslavik Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Covers multiple items case and moves the indicator to the trailing comma. This is a suggestion block, so need to regen pegen.

Suggested change
| ['async'] 'with' expression ['as' star_target] ',' ':' {
RAISE_SYNTAX_ERROR("single 'with' item has a trailing comma") }
| ['async'] 'with' ','.(expression ['as' star_target])+ trailing=',' ':' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(trailing, "the last 'with' item has a trailing comma") }

| ['async'] 'with' ','.(expression ['as' star_target])+ NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
| ['async'] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
invalid_with_stmt_indent:
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,14 @@
Traceback (most recent call last):
SyntaxError: trailing comma not allowed without surrounding parentheses
>>> with item,: pass
Traceback (most recent call last):
SyntaxError: single 'with' item has a trailing comma
>>> with item as x,: pass
Traceback (most recent call last):
SyntaxError: single 'with' item has a trailing comma
Comment on lines +2064 to +2071
Copy link
Member

@johnslavik johnslavik Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Covers multiple items cases.

Suggested change
>>> with item,: pass
Traceback (most recent call last):
SyntaxError: single 'with' item has a trailing comma
>>> with item as x,: pass
Traceback (most recent call last):
SyntaxError: single 'with' item has a trailing comma
>>> with item,: pass
Traceback (most recent call last):
SyntaxError: the last 'with' item has a trailing comma
>>> with item as x,: pass
Traceback (most recent call last):
SyntaxError: the last 'with' item has a trailing comma
>>> with item1, item2,: pass
Traceback (most recent call last):
SyntaxError: the last 'with' item has a trailing comma
>>> with item1 as x, item2,: pass
Traceback (most recent call last):
SyntaxError: the last 'with' item has a trailing comma
>>> with item1 as x, item2 as y,: pass
Traceback (most recent call last):
SyntaxError: the last 'with' item has a trailing comma
>>> with item1, item2 as y,: pass
Traceback (most recent call last):
SyntaxError: the last 'with' item has a trailing comma

>>> import a from b
Traceback (most recent call last):
SyntaxError: Did you mean to use 'from ... import ...' instead?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Specialized the parser error for a single unparenthesized ``with`` item followed
by a trailing comma (for example, ``with item,:``), raising a clearer
:exc:`SyntaxError` message. Patch by Pablo Galindo.
Loading
Loading