Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,12 @@ 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]:
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.
58 changes: 56 additions & 2 deletions Parser/parser.c

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

Loading