◐ Shell
clean mode source ↗

Specialize syntax error for trailing comma of last item in `with`

Feature or enhancement

Proposal:

Consider a specialized syntax error for these cases:

with item,: pass
with item as x,: pass

whereas this would be correct syntax:

with (item,): pass
with (item as x,): pass

This was taken from https://github.com/astral-sh/ruff/blob/6ff4da454c448f01eb0b71728f3941870c811bfc/crates/ruff_python_parser/resources/invalid/statements/with/unparenthesized_with_items.py. 2+ years in Ruff's parser.

I'm not sure what the better message could be. Maybe one of these:

❯ ./python.exe t.py
  File "/Users/bartosz.slawecki/Python/cpython/t.py", line 1
    with item,: pass
              ^
SyntaxError: single 'with' item has a trailing comma                 # this tells me what's wrong
SyntaxError: single 'with' item is followed by a comma               # this tells me about a violated rule
SyntaxError: this 'with' statement cannot end with a trailing comma  # this sets a rule
SyntaxError: single 'with' item cannot be followed by a comma        # this is not always true :-)

Only this specific case seems to require parentheses to pass.
For instance, I could also do:

Or maybe we could allow no parentheses at all? But that's probably much more work for an edge case, would it be worth it?

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs