gh-145241: specialize SyntaxError for single trailing-comma with item#145282
gh-145241: specialize SyntaxError for single trailing-comma with item#145282pablogsal wants to merge 1 commit intopython:mainfrom
Conversation
3433bc5 to
4994e57
Compare
There was a problem hiding this comment.
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).
| >>> 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 | ||
There was a problem hiding this comment.
Covers multiple items cases.
| >>> 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 | |
| | ['async'] 'with' expression ['as' star_target] ',' ':' { | ||
| RAISE_SYNTAX_ERROR("single 'with' item has a trailing comma") } |
There was a problem hiding this comment.
Covers multiple items case and moves the indicator to the trailing comma. This is a suggestion block, so need to regen pegen.
| | ['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") } |
with#145241