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 `match` syntax error for unary addition in pattern · Issue #145239 · python/cpython

Specialize match syntax error for unary addition in pattern #145239

New issue
Copy link
New issue
Copy link
Closed
Closed
Specialize match syntax error for unary addition in pattern#145239
Copy link
Assignees
johnslavik
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:

# Unary addition isn't allowed but we parse it for better error recovery.
match subject:
    case +1:
        pass
    case 1 | +2 | -3:
        pass
    case [1, +2, -3]:
        pass
    case Foo(x=+1, y=-2):
        pass
    case {True: +1, False: -2}:
        pass

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

Current vs proposed:

❯ ./python.exe t.py
  File "/Users/bartosz.slawecki/Python/cpython/t.py", line 2
    case +1:
         ^
-SyntaxError: invalid syntax
+SyntaxError: cannot use unary addition in a literal pattern

The code doesn't look incorrect syntactically, which is why I think it's a good idea to add a more specific message (although it is not extremely unclear as-is assuming a pointer ^ is shown).

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-145239: Specialize match syntax error for unary addition in pattern #145658
  • gh-145239: Accept unary plus literal pattern #148566
  • gh-145239: Colorize case + in the REPL #150979

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
    removed their assignment
    on Feb 26, 2026
  4. johnslavik commented on Feb 26, 2026

    @johnslavik
    johnslavik
    on Feb 26, 2026
    MemberAuthor
    More actions

    cc @sobolevn @Eclips4 @pablogsal

  5. pablogsal commented on Feb 26, 2026

    @pablogsal
    pablogsal
    on Feb 26, 2026
    Member
    More actions

    CC @brandtbucher

  6. mvanhorn
    added a commit that references this issue on Mar 9, 2026

    pythongh-145239: Specialize match syntax error for unary addition in …

    3b7072e
  7. mvanhorn commented on Mar 9, 2026

    @mvanhorn
    mvanhorn
    on Mar 9, 2026
    Contributor
    More actions

    I've submitted a PR for this: #145658

    The approach adds an invalid_literal_pattern rule to the PEG grammar that catches '+' NUMBER and raises the error: "cannot use unary '+' in a literal pattern". This covers all the pattern contexts mentioned in the issue (top-level, alternatives, sequences, class patterns, and mapping patterns).

    This contribution was developed with AI assistance (Claude Code).

  8. encukou commented on Mar 10, 2026

    @encukou
    encukou
    on Mar 10, 2026
    Member
    More actions

    Should we accept + instead?
    ast.literal_eval allows it. Also, 3e+1 is a valid spelling for 3e1 (using a different mechanism of course).

    Personally, I'd love to be able to write:

    match direction:
        case 0, +1: go_up()
        case 0, -1: go_down()
        case -1, 0: go_left()
        case +1, 0: go_right()
    Reacted by Bartosz Sławecki and Kirill Podoprigora
  9. johnslavik commented on Mar 10, 2026

    @johnslavik
    johnslavik
    on Mar 10, 2026
    MemberAuthor
    More actions

    @encukou yeah, +1 (no pun intended). I'd love to see it supported and actually started implementing that locally. Do you think I should open a DPO? Is it minor enough to not need a PEP?

  10. encukou commented on Mar 10, 2026

    @encukou
    encukou
    on Mar 10, 2026
    Member
    More actions

    I'd wait a day or two in case a parser expert wants to veto the idea here :)

    Reacted by Bartosz Sławecki
  11. johnslavik commented on Mar 10, 2026

    @johnslavik
    johnslavik
    on Mar 10, 2026
    MemberAuthor
    More actions

    cc @pablogsal

  12. khalidsaidi commented on Mar 25, 2026

    @khalidsaidi
    khalidsaidi
    on Mar 25, 2026 · Hidden as spam
    show commentMore actions
  13. johnslavik
    self-assigned this
    on Mar 25, 2026
  14. johnslavik
    added 3 commits that reference this issue on Apr 13, 2026

    pythongh-145239: Accept unary plus in match literal patterns

    004646e

    pythongh-145239: Add tests for unary plus in match literal patterns

    b30bc97

    pythongh-145239: Add NEWS entry

    5bb1a93
  15. Eclips4
    added a commit that references this issue on Apr 23, 2026

    gh-145239: Accept unary plus literal pattern (#148566)

    448d7b9
  16. johnslavik
    closed this as completedon Apr 24, 2026
  17. ljfp
    added a commit that references this issue on Apr 25, 2026

    pythongh-145239: Accept unary plus literal pattern (python#148566)

    c1a69cf
  18. Eclips4
    added a commit that references this issue on Jun 10, 2026

    gh-145239: Colorize `case +` in the REPL (#150979)

    ff64d8d
  19. philthompson10
    added a commit that references this issue on Jun 17, 2026

    pythongh-145239: Colorize `case +` in the REPL (python#150979)

    513e89c
  20. stealth-rklopper
    added a commit that references this issue on Oct 19, 2026

    feat: accept a plus sign in literal patterns

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

Metadata

Metadata

Assignees

  • @johnslavik
    johnslavik

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.