gh-145239: Specialize match syntax error for unary addition in pattern#145658
Open
mvanhorn wants to merge 2 commits intopython:mainfrom
Open
gh-145239: Specialize match syntax error for unary addition in pattern#145658mvanhorn wants to merge 2 commits intopython:mainfrom
mvanhorn wants to merge 2 commits intopython:mainfrom
Conversation
…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>
Contributor
|
Also @mvanhorn, Just a heads up, if you accept the |
Author
|
Thanks for the heads up! |
Contributor
|
No Problem, Happy to help let me know if you need help with anything else :) |
johnslavik
requested changes
Mar 9, 2026
Lib/test/test_patma.py
Outdated
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 | ||
| """) | ||
|
|
Member
There was a problem hiding this comment.
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.
Author
There was a problem hiding this comment.
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: Addedinvalid_literal_patternrule that catches'+' NUMBERand raises the specialized errorParser/parser.c: Regenerated from grammarLib/test/test_syntax.py: Added doctest-style tests for all pattern contextsLib/test/test_patma.py: Added unit tests inTestSyntaxErrorsMisc/NEWS.d: Added NEWS entryExample
Before:
After:
This contribution was developed with AI assistance (Claude Code).
matchsyntax error for unary addition in pattern #145239