Skip to content

gh-145239: Specialize match syntax error for unary addition in pattern#145658

Open
mvanhorn wants to merge 2 commits intopython:mainfrom
mvanhorn:osc/145239-match-unary-add-syntax-error
Open

gh-145239: Specialize match syntax error for unary addition in pattern#145658
mvanhorn wants to merge 2 commits intopython:mainfrom
mvanhorn:osc/145239-match-unary-add-syntax-error

Conversation

@mvanhorn
Copy link

@mvanhorn mvanhorn commented Mar 9, 2026

Fixes #145239.

Adds a specialized syntax error message when unary + is used in match patterns. Instead of the generic "invalid syntax", users now see a clear message: "cannot use unary '+' in a literal pattern".

This applies to all pattern contexts: top-level, alternatives, sequences, class patterns, and mapping patterns.

Changes

  • Grammar/python.gram: Added invalid_literal_pattern rule that catches '+' NUMBER and raises the specialized error
  • Parser/parser.c: Regenerated from grammar
  • Lib/test/test_syntax.py: Added doctest-style tests for all pattern contexts
  • Lib/test/test_patma.py: Added unit tests in TestSyntaxErrors
  • Misc/NEWS.d: Added NEWS entry

Example

match subject:
    case +1:
        pass

Before:

SyntaxError: invalid syntax

After:

SyntaxError: cannot use unary '+' in a literal pattern

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

…pattern

Add a specialized syntax error message when unary '+' is used in match
patterns. Instead of the generic "invalid syntax", users now see:
"cannot use unary '+' in a literal pattern".

This applies to all pattern contexts: top-level, alternatives, sequences,
class patterns, and mapping patterns.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@python-cla-bot
Copy link

python-cla-bot bot commented Mar 9, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@Shrey-N
Copy link
Contributor

Shrey-N commented Mar 9, 2026

Also @mvanhorn, Just a heads up, if you accept the python.gram suggestions via the GitHub UI, remember to pull locally and regenerate Parser/parser.c before your next push, otherwise the CI will fail. :)

@mvanhorn
Copy link
Author

mvanhorn commented Mar 9, 2026

Thanks for the heads up!

@Shrey-N
Copy link
Contributor

Shrey-N commented Mar 9, 2026

No Problem, Happy to help let me know if you need help with anything else :)

Copy link
Member

@johnslavik johnslavik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall seems good.

Comment on lines +3233 to +3267
def test_unary_add_in_literal_pattern(self):
self.assert_syntax_error("""
match ...:
case +1:
pass
""")

def test_unary_add_in_or_pattern(self):
self.assert_syntax_error("""
match ...:
case 1 | +2 | -3:
pass
""")

def test_unary_add_in_sequence_pattern(self):
self.assert_syntax_error("""
match ...:
case [1, +2, -3]:
pass
""")

def test_unary_add_in_class_pattern(self):
self.assert_syntax_error("""
match ...:
case Foo(x=+1, y=-2):
pass
""")

def test_unary_add_in_mapping_pattern(self):
self.assert_syntax_error("""
match ...:
case {True: +1, False: -2}:
pass
""")

Copy link
Member

@johnslavik johnslavik Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Occam's razor: we already test this in test_syntax where we additionally validate the exception message, so I'd remove any changes you made to test_patma.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call - removed the test_patma changes in b07a507. The test_syntax coverage is sufficient.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Specialize match syntax error for unary addition in pattern

3 participants