Skip to content
2 changes: 1 addition & 1 deletion Doc/reference/compound_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ A literal pattern corresponds to most
: | "None"
: | "True"
: | "False"
signed_number: ["-"] NUMBER
signed_number: ["+" | "-"] NUMBER

The rule ``strings`` and the token ``NUMBER`` are defined in the
:doc:`standard Python grammar <./grammar>`. Triple-quoted strings are
Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,10 @@ Other language changes
* Allow the *count* argument of :meth:`bytes.replace` to be a keyword.
(Contributed by Stan Ulbrych in :gh:`147856`.)

* Unary plus is now accepted in :keyword:`match` literal patterns, mirroring
the existing support for unary minus.
(Contributed by Bartosz Sławecki in :gh:`145239`.)


New modules
===========
Expand Down
3 changes: 3 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -554,17 +554,20 @@ complex_number[expr_ty]:

signed_number[expr_ty]:
| NUMBER
| '+' number=NUMBER { number }
| '-' number=NUMBER { _PyAST_UnaryOp(USub, number, EXTRA) }

signed_real_number[expr_ty]:
| real_number
| '+' real=real_number { real }
| '-' real=real_number { _PyAST_UnaryOp(USub, real, EXTRA) }

real_number[expr_ty]:
| real=NUMBER { _PyPegen_ensure_real(p, real) }

imaginary_number[expr_ty]:
| imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) }
| '+' imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since imaginary_number is always preceded by + or - already, this makes the following pass:

match 1:
    case 1++1j:
        pass

@johnslavik, do you want to fix this? Removing this case should do it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes; I'll be back home from a trip soon and I'll fix it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@encukou Let's revisit this at EuroPython sprints.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I tend to be pretty busy at sprints; could you ask here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I filed #154776 for this.


capture_pattern[pattern_ty]:
| target=pattern_capture_target { _PyAST_MatchAs(NULL, target->v.Name.id, EXTRA) }
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/.ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ extend-exclude = [
"test_lazy_import/__init__.py",
"test_lazy_import/data/*.py",
"test_lazy_import/data/**/*.py",
# Unary plus literal pattern is not yet supported by Ruff (GH-145239)
"test_patma.py",
]

[lint]
Expand Down
90 changes: 90 additions & 0 deletions Lib/test/test_patma.py
Original file line number Diff line number Diff line change
Expand Up @@ -2762,6 +2762,96 @@ def test_patma_255(self):
self.assertEqual(y, 1)
self.assertIs(z, x)

def test_patma_256(self):
x = 0
match x:
case +0:
y = 0
self.assertEqual(x, 0)
self.assertEqual(y, 0)

def test_patma_257(self):
x = 0
match x:
case +0.0:
y = 0
self.assertEqual(x, 0)
self.assertEqual(y, 0)

def test_patma_258(self):
x = 0
match x:
case +0j:
y = 0
self.assertEqual(x, 0)
self.assertEqual(y, 0)

def test_patma_259(self):
x = 0
match x:
case +0.0j:
y = 0
self.assertEqual(x, 0)
self.assertEqual(y, 0)

def test_patma_260(self):
x = 1
match x:
case +1:
y = 0
self.assertEqual(x, 1)
self.assertEqual(y, 0)

def test_patma_261(self):
x = 1.5
match x:
case +1.5:
y = 0
self.assertEqual(x, 1.5)
self.assertEqual(y, 0)

def test_patma_262(self):
x = 1j
match x:
case +1j:
y = 0
self.assertEqual(x, 1j)
self.assertEqual(y, 0)

def test_patma_263(self):
x = 1.5j
match x:
case +1.5j:
y = 0
self.assertEqual(x, 1.5j)
self.assertEqual(y, 0)

def test_patma_264(self):
x = 0.25 + 1.75j
match x:
case +0.25 + 1.75j:
y = 0
self.assertEqual(x, 0.25 + 1.75j)
self.assertEqual(y, 0)

def test_patma_265(self):
x = 0.25 - 1.75j
match x:
case 0.25 - +1.75j:
y = 0
self.assertEqual(x, 0.25 - 1.75j)
self.assertEqual(y, 0)

def test_patma_266(self):
x = 0
match x:
case +1e1000:
y = 0
case 0:
y = 1
self.assertEqual(x, 0)
self.assertEqual(y, 1)

def test_patma_runtime_checkable_protocol(self):
# Runtime-checkable protocol
from typing import Protocol, runtime_checkable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Unary plus is now accepted in :keyword:`match` literal patterns, mirroring
the existing support for unary minus.
Patch by Bartosz Sławecki.
87 changes: 84 additions & 3 deletions Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading