Skip to content

gh-145241: specialize SyntaxError for single trailing-comma with item#145282

Open
pablogsal wants to merge 1 commit intopython:mainfrom
pablogsal:gh-145241-with-trailing-comma-news
Open

gh-145241: specialize SyntaxError for single trailing-comma with item#145282
pablogsal wants to merge 1 commit intopython:mainfrom
pablogsal:gh-145241-with-trailing-comma-news

Conversation

@pablogsal
Copy link
Member

@pablogsal pablogsal commented Feb 26, 2026

@pablogsal pablogsal force-pushed the gh-145241-with-trailing-comma-news branch from 3433bc5 to 4994e57 Compare February 26, 2026 21:46
Copy link
Member

@johnslavik johnslavik left a comment

Choose a reason for hiding this comment

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

Thanks! I've researched this a bit more, and I think we can make this even better :-)
Sorry for the resulting back-and-forth and not enough research in the issue.

The syntax error happens every time the last with item (could be one or could be multiple overall) has a bare trailing comma and items aren't parenthesized.

We'd want to add cases with multiple items as well and adapt the error message accordingly. I've added suggestions ready for you to apply. Please feel free to pick better phrasing though.

PS Also suggested using RAISE_SYNTAX_ERROR_KNOWN_LOCATION so that the caret ^ points at the faulty trailing comma (right now the caret points at the colon).

Comment on lines +2064 to +2071
>>> 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
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

Comment on lines +1450 to +1451
| ['async'] 'with' expression ['as' star_target] ',' ':' {
RAISE_SYNTAX_ERROR("single 'with' item has a trailing comma") }
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") }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants