You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Specialize match syntax error for unary addition in pattern #145239
❯ ./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
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).
@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?
Activity
johnslavik commentedon Feb 26, 2026
cc @sobolevn @Eclips4 @pablogsal
pablogsal commentedon Feb 26, 2026
CC @brandtbucher
pythongh-145239: Specialize match syntax error for unary addition in …
mvanhorn commentedon Mar 9, 2026
I've submitted a PR for this: #145658
The approach adds an
invalid_literal_patternrule to the PEG grammar that catches'+' NUMBERand 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).
encukou commentedon Mar 10, 2026
Should we accept
+instead?ast.literal_evalallows it. Also,3e+1is a valid spelling for3e1(using a different mechanism of course).Personally, I'd love to be able to write:
johnslavik commentedon Mar 10, 2026
@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?
encukou commentedon Mar 10, 2026
I'd wait a day or two in case a parser expert wants to veto the idea here :)
johnslavik commentedon Mar 10, 2026
cc @pablogsal
khalidsaidi commentedon Mar 25, 2026
pythongh-145239: Accept unary plus in match literal patterns
pythongh-145239: Add tests for unary plus in match literal patterns
pythongh-145239: Add NEWS entry
gh-145239: Accept unary plus literal pattern (#148566)
pythongh-145239: Accept unary plus literal pattern (python#148566)
gh-145239: Colorize `case +` in the REPL (#150979)
pythongh-145239: Colorize `case +` in the REPL (python#150979)