Skip to content

Commit fa5cc5f

Browse files
committed
gh-154775: Don't accept duplicate plus sign when matching complex number
1 parent c7b9a13 commit fa5cc5f

4 files changed

Lines changed: 31 additions & 29 deletions

File tree

Grammar/python.gram

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,6 @@ real_number[expr_ty]:
568568

569569
imaginary_number[expr_ty]:
570570
| imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) }
571-
| '+' imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) }
572571

573572
capture_pattern[pattern_ty]:
574573
| target=pattern_capture_target { _PyAST_MatchAs(NULL, target->v.Name.id, EXTRA) }

Lib/test/test_patma.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3329,6 +3329,34 @@ def test_mapping_pattern_duplicate_key_edge_case3(self):
33293329
pass
33303330
""")
33313331

3332+
def test_duplicate_sign_in_complex_1(self):
3333+
self.assert_syntax_error("""
3334+
match ...:
3335+
case 0 ++ 0j:
3336+
pass
3337+
""")
3338+
3339+
def test_duplicate_sign_in_complex_2(self):
3340+
self.assert_syntax_error("""
3341+
match ...:
3342+
case 0 -+ 0j:
3343+
pass
3344+
""")
3345+
3346+
def test_duplicate_sign_in_complex_3(self):
3347+
self.assert_syntax_error("""
3348+
match ...:
3349+
case 0 +- 0j:
3350+
pass
3351+
""")
3352+
3353+
def test_duplicate_sign_in_complex_4(self):
3354+
self.assert_syntax_error("""
3355+
match ...:
3356+
case 0 -- 0j:
3357+
pass
3358+
""")
3359+
33323360
class TestTypeErrors(unittest.TestCase):
33333361

33343362
def test_accepts_positional_subpatterns_0(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
When matching a complex literal in :keyword:`case` statements, an extraneous
2+
``+`` sign (for example, ``1++1j`` or ``1-+1j``) is no longer allowed.

Parser/parser.c

Lines changed: 1 addition & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)