GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Specialize syntax error for trailing comma of last item in `with` · Issue #145241 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Specialize syntax error for trailing comma of last item in with#145241
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-parsertype-featureA feature request or enhancementA feature request or enhancement

Description

@johnslavik
johnslavik
opened on Feb 26, 2026
Issue body actions

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

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

Activity

  1. johnslavik
    self-assigned this
    on Feb 26, 2026
  2. johnslavik
    added
    type-featureA feature request or enhancementA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    topic-parser
    on Feb 26, 2026
  3. johnslavik
    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
  4. johnslavik
    removed their assignment
    on Feb 26, 2026
  5. johnslavik commented on Feb 26, 2026

    @johnslavik
    johnslavik
    on Feb 26, 2026
    MemberAuthor
    More actions

    cc @sobolevn @Eclips4 @pablogsal

  6. terryjreedy
    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
  7. terryjreedy commented on Feb 26, 2026

    @terryjreedy
    terryjreedy
    on Feb 26, 2026
    Member
    More actions

    Verified standard message SyntaxError: invalid syntax on 3.15.0a6.

  8. pablogsal
    added 2 commits that reference this issue on Feb 26, 2026

    pythongh-145241: specialize SyntaxError for single trailing-comma wit…

    3433bc5

    pythongh-145241: specialize SyntaxError for single trailing-comma wit…

    4994e57
  9. johnslavik
    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
  10. johnslavik commented on Feb 26, 2026

    @johnslavik
    johnslavik
    on Feb 26, 2026
    MemberAuthor
    More actions

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

  11. pablogsal
    added a commit that references this issue on Feb 28, 2026

    gh-145241: specialize SyntaxError for single trailing-comma with item (…

    09e8c38
  12. johnslavik commented on Feb 28, 2026

    @johnslavik
    johnslavik
    on Feb 28, 2026
    MemberAuthor
    More actions

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

  13. johnslavik
    closed this as completedon Feb 28, 2026
  14. brijkapadia
    added a commit that references this issue on Feb 28, 2026

    pythongh-145241: specialize SyntaxError for single trailing-comma wit…

    0d13a6e
  15. ljfp
    added a commit that references this issue on Apr 25, 2026

    pythongh-145241: specialize SyntaxError for single trailing-comma wit…

    172eb81
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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-parsertype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.