Skip to content
Merged
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])+ 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
24 changes: 24 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,30 @@
Traceback (most recent call last):
SyntaxError: trailing comma not allowed without surrounding parentheses

>>> 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 when ``with`` items are followed
by a trailing comma (for example, ``with item,:``), raising a clearer
:exc:`SyntaxError` message. Patch by Pablo Galindo and Bartosz Sławecki.
Loading
Loading