Skip to content

Specialize syntax error for trailing comma of last item in with #145241

Description

@johnslavik

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:

with a, b: pass

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

Activity

  1. self-assigned this
    on Feb 26, 2026
  2. changed the title [-]Specialize `match` syntax error for unary addition in pattern[/-] [+]Specialize `match` syntax error for unparenthesized comma-delimited 'with' item[/+] on Feb 26, 2026
  3. removed their assignment
    on Feb 26, 2026
  4. johnslavik commented on Feb 26, 2026

    @johnslavik
    MemberAuthor
  5. changed the title [-]Specialize `match` syntax error for unparenthesized comma-delimited 'with' item[/-] [+]Specialize syntax error for unparenthesized comma-delimited 'with' item[/+] on Feb 26, 2026
  6. terryjreedy commented on Feb 26, 2026

    @terryjreedy
    Member

    Verified standard message SyntaxError: invalid syntax on 3.15.0a6.

  7. added 2 commits that reference this issue on Feb 26, 2026
  8. changed the title [-]Specialize syntax error for unparenthesized comma-delimited 'with' item[/-] [+]Specialize syntax error for trailing comma of last item in `with`[/+] on Feb 26, 2026
  9. johnslavik commented on Feb 26, 2026

    @johnslavik
    MemberAuthor

    While reviewing the PR, I've learned that this can be triggered by more than one with item as well. See #145282 (review).

  10. added a commit that references this issue on Feb 28, 2026
  11. johnslavik commented on Feb 28, 2026

    @johnslavik
    MemberAuthor

    I'd command backports according to #142236 (comment). EDIT: Couldn't cleanly backport, I gave up.

  12. added a commit that references this issue on Feb 28, 2026
  13. added a commit that references this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions