Add formatting for StmtMatch (#6286)

## Summary

This PR adds support for `StmtMatch` with subs for `MatchCase`.

## Test Plan

Add a few additional test cases around `match` statement, comments, line
breaks.

resolves: #6298
This commit is contained in:
Dhruv Manilawala 2023-08-08 18:48:49 +05:30 committed by GitHub
parent 87984e9ac7
commit 001aa486df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 882 additions and 444 deletions

View file

@ -156,180 +156,192 @@ match x:
```diff
--- Black
+++ Ruff
@@ -1,144 +1,60 @@
# Cases sampled from Lib/test/test_patma.py
@@ -2,143 +2,143 @@
# case black_test_patma_098
-match x:
match x:
- case -0j:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_142
-match x:
match x:
- case bytes(z):
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_073
-match x:
match x:
- case 0 if 0:
- y = 0
+ case NOT_YET_IMPLEMENTED_Pattern if 0:
y = 0
- case 0 if 1:
- y = 1
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern if 1:
y = 1
# case black_test_patma_006
-match 3:
match 3:
- case 0 | 1 | 2 | 3:
- x = True
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
x = True
# case black_test_patma_049
-match x:
match x:
- case [0, 1] | [1, 0]:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_check_sequence_then_mapping
-match x:
match x:
- case [*_]:
- return "seq"
+ case NOT_YET_IMPLEMENTED_Pattern:
return "seq"
- case {}:
- return "map"
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
return "map"
# case black_test_patma_035
-match x:
match x:
- case {0: [1, 2, {}]}:
- y = 0
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
- case {0: [1, 2, {}] | True} | {1: [[]]} | {0: [1, 2, {}]} | [] | "X" | {}:
- y = 1
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 1
- case []:
- y = 2
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 2
# case black_test_patma_107
-match x:
match x:
- case 0.25 + 1.75j:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_097
-match x:
match x:
- case -0j:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_007
-match 4:
match 4:
- case 0 | 1 | 2 | 3:
- x = True
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
x = True
# case black_test_patma_154
-match x:
match x:
- case 0 if x:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern if x:
y = 0
# case black_test_patma_134
-match x:
match x:
- case {1: 0}:
- y = 0
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
- case {0: 0}:
- y = 1
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 1
- case {**z}:
- y = 2
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 2
# case black_test_patma_185
-match Seq():
match Seq():
- case [*_]:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_063
-match x:
match x:
- case 1:
- y = 0
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
- case 1:
- y = 1
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 1
# case black_test_patma_248
-match x:
match x:
- case {"foo": bar}:
- y = bar
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = bar
# case black_test_patma_019
-match (0, 1, 2):
match (0, 1, 2):
- case [0, 1, *x, 2]:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_052
-match x:
match x:
- case [0]:
- y = 0
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
- case [1, 0] if (x := x[:0]):
- y = 1
+ case NOT_YET_IMPLEMENTED_Pattern if x := x[:0]:
y = 1
- case [1, 0]:
- y = 2
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 2
# case black_test_patma_191
-match w:
match w:
- case [x, y, *_]:
- z = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
z = 0
# case black_test_patma_110
-match x:
match x:
- case -0.25 - 1.75j:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_151
-match (x,):
match (x,):
- case [y]:
- z = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
z = 0
# case black_test_patma_114
-match x:
match x:
- case A.B.C.D:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_232
-match x:
match x:
- case None:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_058
-match x:
match x:
- case 0:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_233
-match x:
match x:
- case False:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_078
-match x:
match x:
- case []:
- y = 0
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
- case [""]:
- y = 1
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 1
- case "":
- y = 2
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 2
# case black_test_patma_156
-match x:
match x:
- case z:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_189
-match w:
match w:
- case [x, y, *rest]:
- z = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
z = 0
# case black_test_patma_042
-match x:
match x:
- case (0 as z) | (1 as z) | (2 as z) if z == x % 2:
- y = 0
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern if z == x % 2:
y = 0
# case black_test_patma_034
-match x:
match x:
- case {0: [1, 2, {}]}:
- y = 0
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
- case {0: [1, 2, {}] | False} | {1: [[]]} | {0: [1, 2, {}]} | [] | "X" | {}:
- y = 1
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 1
- case []:
- y = 2
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 2
```
## Ruff Output
@ -338,63 +350,147 @@ match x:
# Cases sampled from Lib/test/test_patma.py
# case black_test_patma_098
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_142
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_073
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern if 0:
y = 0
case NOT_YET_IMPLEMENTED_Pattern if 1:
y = 1
# case black_test_patma_006
NOT_YET_IMPLEMENTED_StmtMatch
match 3:
case NOT_YET_IMPLEMENTED_Pattern:
x = True
# case black_test_patma_049
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_check_sequence_then_mapping
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
return "seq"
case NOT_YET_IMPLEMENTED_Pattern:
return "map"
# case black_test_patma_035
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
case NOT_YET_IMPLEMENTED_Pattern:
y = 1
case NOT_YET_IMPLEMENTED_Pattern:
y = 2
# case black_test_patma_107
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_097
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_007
NOT_YET_IMPLEMENTED_StmtMatch
match 4:
case NOT_YET_IMPLEMENTED_Pattern:
x = True
# case black_test_patma_154
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern if x:
y = 0
# case black_test_patma_134
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
case NOT_YET_IMPLEMENTED_Pattern:
y = 1
case NOT_YET_IMPLEMENTED_Pattern:
y = 2
# case black_test_patma_185
NOT_YET_IMPLEMENTED_StmtMatch
match Seq():
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_063
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
case NOT_YET_IMPLEMENTED_Pattern:
y = 1
# case black_test_patma_248
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = bar
# case black_test_patma_019
NOT_YET_IMPLEMENTED_StmtMatch
match (0, 1, 2):
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_052
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
case NOT_YET_IMPLEMENTED_Pattern if x := x[:0]:
y = 1
case NOT_YET_IMPLEMENTED_Pattern:
y = 2
# case black_test_patma_191
NOT_YET_IMPLEMENTED_StmtMatch
match w:
case NOT_YET_IMPLEMENTED_Pattern:
z = 0
# case black_test_patma_110
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_151
NOT_YET_IMPLEMENTED_StmtMatch
match (x,):
case NOT_YET_IMPLEMENTED_Pattern:
z = 0
# case black_test_patma_114
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_232
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_058
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_233
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_078
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
case NOT_YET_IMPLEMENTED_Pattern:
y = 1
case NOT_YET_IMPLEMENTED_Pattern:
y = 2
# case black_test_patma_156
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
# case black_test_patma_189
NOT_YET_IMPLEMENTED_StmtMatch
match w:
case NOT_YET_IMPLEMENTED_Pattern:
z = 0
# case black_test_patma_042
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern if z == x % 2:
y = 0
# case black_test_patma_034
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
case NOT_YET_IMPLEMENTED_Pattern:
y = 1
case NOT_YET_IMPLEMENTED_Pattern:
y = 2
```
## Black Output

View file

@ -131,139 +131,155 @@ match bar1:
```diff
--- Black
+++ Ruff
@@ -1,119 +1,43 @@
@@ -1,13 +1,13 @@
import match
-match something:
match something:
- case [a as b]:
- print(b)
+ case NOT_YET_IMPLEMENTED_Pattern:
print(b)
- case [a as b, c, d, e as f]:
- print(f)
+ case NOT_YET_IMPLEMENTED_Pattern:
print(f)
- case Point(a as b):
- print(b)
+ case NOT_YET_IMPLEMENTED_Pattern:
print(b)
- case Point(int() as x, int() as y):
- print(x, y)
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
print(x, y)
match = 1
@@ -15,40 +15,43 @@
case: int = re.match(something)
-match re.match(case):
match re.match(case):
- case type("match", match):
- pass
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
- case match:
- pass
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
def func(match: case, case: match) -> case:
- match Something():
match Something():
- case func(match, case):
- ...
+ case NOT_YET_IMPLEMENTED_Pattern:
...
- case another:
- ...
+ NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
...
-match maybe, multiple:
match maybe, multiple:
- case perhaps, 5:
- pass
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
- case perhaps, 6,:
- pass
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-match more := (than, one), indeed,:
- case _, (5, 6):
- pass
+match (
+ more := (than, one),
+ indeed,
+):
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
- case [[5], (6)], [7],:
- pass
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
- case _:
- pass
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-match a, *b, c:
match a, *b, c:
- case [*_]:
- assert "seq" == _
+ case NOT_YET_IMPLEMENTED_Pattern:
assert "seq" == _
- case {}:
- assert "map" == b
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
assert "map" == b
-match match(
- case,
- match(
- match, case, match, looooooooooooooooooooooooooooooooooooong, match, case, match
- ),
- case,
-):
@@ -59,61 +62,47 @@
),
case,
):
- case case(
- match=case,
- case=re.match(
- loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
- ),
- ):
- pass
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-
- case [a as match]:
- pass
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-
- case case:
- pass
-
-
-match match:
- case case:
- pass
-
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-match a, *b(), c:
match match:
- case case:
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
match a, *b(), c:
- case d, *f, g:
- pass
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-
-match something:
match something:
- case {
- "key": key as key_1,
- "password": PASS.ONE | PASS.TWO | PASS.THREE as password,
- }:
- pass
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
- case {"maybe": something(complicated as this) as that}:
- pass
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-match something:
match something:
- case 1 as a:
- pass
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-
- case 2 as b, 3 as c:
- pass
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-
- case 4 as d, (5 as e), (6 | 7 as g), *h:
- pass
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-match bar1:
match bar1:
- case Foo(aa=Callable() as aa, bb=int()):
- print(bar1.aa, bar1.bb)
+ case NOT_YET_IMPLEMENTED_Pattern:
print(bar1.aa, bar1.bb)
- case _:
- print("no match", "\n")
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
print("no match", "\n")
-match bar1:
match bar1:
- case Foo(
- normal=x, perhaps=[list, {"x": d, "y": 1.0}] as y, otherwise=something, q=t as u
- ):
- pass
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
```
## Ruff Output
@ -271,47 +287,112 @@ match bar1:
```py
import match
NOT_YET_IMPLEMENTED_StmtMatch
match something:
case NOT_YET_IMPLEMENTED_Pattern:
print(b)
case NOT_YET_IMPLEMENTED_Pattern:
print(f)
case NOT_YET_IMPLEMENTED_Pattern:
print(b)
case NOT_YET_IMPLEMENTED_Pattern:
print(x, y)
match = 1
case: int = re.match(something)
NOT_YET_IMPLEMENTED_StmtMatch
match re.match(case):
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
def func(match: case, case: match) -> case:
NOT_YET_IMPLEMENTED_StmtMatch
match Something():
case NOT_YET_IMPLEMENTED_Pattern:
...
case NOT_YET_IMPLEMENTED_Pattern:
...
NOT_YET_IMPLEMENTED_StmtMatch
match maybe, multiple:
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
NOT_YET_IMPLEMENTED_StmtMatch
match (
more := (than, one),
indeed,
):
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
NOT_YET_IMPLEMENTED_StmtMatch
match a, *b, c:
case NOT_YET_IMPLEMENTED_Pattern:
assert "seq" == _
case NOT_YET_IMPLEMENTED_Pattern:
assert "map" == b
NOT_YET_IMPLEMENTED_StmtMatch
match match(
case,
match(
match, case, match, looooooooooooooooooooooooooooooooooooong, match, case, match
),
case,
):
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
NOT_YET_IMPLEMENTED_StmtMatch
match match:
case NOT_YET_IMPLEMENTED_Pattern:
pass
NOT_YET_IMPLEMENTED_StmtMatch
match a, *b(), c:
case NOT_YET_IMPLEMENTED_Pattern:
pass
NOT_YET_IMPLEMENTED_StmtMatch
match something:
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
NOT_YET_IMPLEMENTED_StmtMatch
match something:
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
NOT_YET_IMPLEMENTED_StmtMatch
match bar1:
case NOT_YET_IMPLEMENTED_Pattern:
print(bar1.aa, bar1.bb)
case NOT_YET_IMPLEMENTED_Pattern:
print("no match", "\n")
NOT_YET_IMPLEMENTED_StmtMatch
match bar1:
case NOT_YET_IMPLEMENTED_Pattern:
pass
```
## Black Output

View file

@ -119,49 +119,44 @@ with match() as match:
```diff
--- Black
+++ Ruff
@@ -23,11 +23,7 @@
pygram.python_grammar,
@@ -24,9 +24,9 @@
]
- match match:
match match:
- case case:
- match match:
+ case NOT_YET_IMPLEMENTED_Pattern:
match match:
- case case:
- pass
+ NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
if all(version.is_python2() for version in target_versions):
# Python 2-only code, so try Python 2 grammars.
@@ -45,9 +41,7 @@
@@ -46,7 +46,7 @@
def test_patma_139(self):
x = False
- match x:
match x:
- case bool(z):
- y = 0
+ NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
self.assertIs(x, False)
self.assertEqual(y, 0)
self.assertIs(z, x)
@@ -72,16 +66,12 @@
def test_patma_155(self):
@@ -73,14 +73,14 @@
x = 0
y = None
- match x:
match x:
- case 1e1000:
- y = 0
+ NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
self.assertEqual(x, 0)
self.assertIs(y, None)
x = range(3)
- match x:
match x:
- case [y, case as x, z]:
- w = 0
+ NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
w = 0
# At least one of the above branches must have been taken, because every Python
# version has exactly one of the two 'ASYNC_*' flags
```
## Ruff Output
@ -192,7 +187,11 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
pygram.python_grammar,
]
NOT_YET_IMPLEMENTED_StmtMatch
match match:
case NOT_YET_IMPLEMENTED_Pattern:
match match:
case NOT_YET_IMPLEMENTED_Pattern:
pass
if all(version.is_python2() for version in target_versions):
# Python 2-only code, so try Python 2 grammars.
@ -210,7 +209,9 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
def test_patma_139(self):
x = False
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
self.assertIs(x, False)
self.assertEqual(y, 0)
self.assertIs(z, x)
@ -235,12 +236,16 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
def test_patma_155(self):
x = 0
y = None
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
self.assertEqual(x, 0)
self.assertIs(y, None)
x = range(3)
NOT_YET_IMPLEMENTED_StmtMatch
match x:
case NOT_YET_IMPLEMENTED_Pattern:
w = 0
# At least one of the above branches must have been taken, because every Python
# version has exactly one of the two 'ASYNC_*' flags

View file

@ -104,111 +104,129 @@ def where_is(point):
```diff
--- Black
+++ Ruff
@@ -1,92 +1,27 @@
@@ -1,92 +1,92 @@
# Cases sampled from PEP 636 examples
-match command.split():
match command.split():
- case [action, obj]:
- ... # interpret action, obj
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
... # interpret action, obj
-match command.split():
match command.split():
- case [action]:
- ... # interpret single-verb action
+ case NOT_YET_IMPLEMENTED_Pattern:
... # interpret single-verb action
- case [action, obj]:
- ... # interpret action, obj
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
... # interpret action, obj
-match command.split():
match command.split():
- case ["quit"]:
- print("Goodbye!")
- quit_game()
+ case NOT_YET_IMPLEMENTED_Pattern:
print("Goodbye!")
quit_game()
- case ["look"]:
- current_room.describe()
+ case NOT_YET_IMPLEMENTED_Pattern:
current_room.describe()
- case ["get", obj]:
- character.get(obj, current_room)
+ case NOT_YET_IMPLEMENTED_Pattern:
character.get(obj, current_room)
- case ["go", direction]:
- current_room = current_room.neighbor(direction)
- # The rest of your commands go here
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
current_room = current_room.neighbor(direction)
# The rest of your commands go here
-match command.split():
match command.split():
- case ["drop", *objects]:
- for obj in objects:
- character.drop(obj, current_room)
- # The rest of your commands go here
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
for obj in objects:
character.drop(obj, current_room)
# The rest of your commands go here
-match command.split():
match command.split():
- case ["quit"]:
- pass
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
- case ["go", direction]:
- print("Going:", direction)
+ case NOT_YET_IMPLEMENTED_Pattern:
print("Going:", direction)
- case ["drop", *objects]:
- print("Dropping: ", *objects)
+ case NOT_YET_IMPLEMENTED_Pattern:
print("Dropping: ", *objects)
- case _:
- print(f"Sorry, I couldn't understand {command!r}")
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
print(f"Sorry, I couldn't understand {command!r}")
-match command.split():
match command.split():
- case ["north"] | ["go", "north"]:
- current_room = current_room.neighbor("north")
+ case NOT_YET_IMPLEMENTED_Pattern:
current_room = current_room.neighbor("north")
- case ["get", obj] | ["pick", "up", obj] | ["pick", obj, "up"]:
- ... # Code for picking up the given object
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
... # Code for picking up the given object
-match command.split():
match command.split():
- case ["go", ("north" | "south" | "east" | "west")]:
- current_room = current_room.neighbor(...)
- # how do I know which direction to go?
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
current_room = current_room.neighbor(...)
# how do I know which direction to go?
-match command.split():
match command.split():
- case ["go", ("north" | "south" | "east" | "west") as direction]:
- current_room = current_room.neighbor(direction)
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
current_room = current_room.neighbor(direction)
-match command.split():
match command.split():
- case ["go", direction] if direction in current_room.exits:
- current_room = current_room.neighbor(direction)
+ case NOT_YET_IMPLEMENTED_Pattern if direction in current_room.exits:
current_room = current_room.neighbor(direction)
- case ["go", _]:
- print("Sorry, you can't go that way")
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
print("Sorry, you can't go that way")
-match event.get():
match event.get():
- case Click(position=(x, y)):
- handle_click_at(x, y)
+ case NOT_YET_IMPLEMENTED_Pattern:
handle_click_at(x, y)
- case KeyPress(key_name="Q") | Quit():
- game.quit()
+ case NOT_YET_IMPLEMENTED_Pattern:
game.quit()
- case KeyPress(key_name="up arrow"):
- game.go_north()
+ case NOT_YET_IMPLEMENTED_Pattern:
game.go_north()
- case KeyPress():
- pass # Ignore other keystrokes
+ case NOT_YET_IMPLEMENTED_Pattern:
pass # Ignore other keystrokes
- case other_event:
- raise ValueError(f"Unrecognized event: {other_event}")
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
raise ValueError(f"Unrecognized event: {other_event}")
-match event.get():
match event.get():
- case Click((x, y), button=Button.LEFT): # This is a left click
- handle_click_at(x, y)
+ case NOT_YET_IMPLEMENTED_Pattern:
handle_click_at(x, y)
- case Click():
- pass # ignore other clicks
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
pass # ignore other clicks
def where_is(point):
- match point:
match point:
- case Point(x=0, y=0):
- print("Origin")
+ case NOT_YET_IMPLEMENTED_Pattern:
print("Origin")
- case Point(x=0, y=y):
- print(f"Y={y}")
+ case NOT_YET_IMPLEMENTED_Pattern:
print(f"Y={y}")
- case Point(x=x, y=0):
- print(f"X={x}")
+ case NOT_YET_IMPLEMENTED_Pattern:
print(f"X={x}")
- case Point():
- print("Somewhere else")
+ case NOT_YET_IMPLEMENTED_Pattern:
print("Somewhere else")
- case _:
- print("Not a point")
+ NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
print("Not a point")
```
## Ruff Output
@ -216,31 +234,96 @@ def where_is(point):
```py
# Cases sampled from PEP 636 examples
NOT_YET_IMPLEMENTED_StmtMatch
match command.split():
case NOT_YET_IMPLEMENTED_Pattern:
... # interpret action, obj
NOT_YET_IMPLEMENTED_StmtMatch
match command.split():
case NOT_YET_IMPLEMENTED_Pattern:
... # interpret single-verb action
case NOT_YET_IMPLEMENTED_Pattern:
... # interpret action, obj
NOT_YET_IMPLEMENTED_StmtMatch
match command.split():
case NOT_YET_IMPLEMENTED_Pattern:
print("Goodbye!")
quit_game()
case NOT_YET_IMPLEMENTED_Pattern:
current_room.describe()
case NOT_YET_IMPLEMENTED_Pattern:
character.get(obj, current_room)
case NOT_YET_IMPLEMENTED_Pattern:
current_room = current_room.neighbor(direction)
# The rest of your commands go here
NOT_YET_IMPLEMENTED_StmtMatch
match command.split():
case NOT_YET_IMPLEMENTED_Pattern:
for obj in objects:
character.drop(obj, current_room)
# The rest of your commands go here
NOT_YET_IMPLEMENTED_StmtMatch
match command.split():
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
print("Going:", direction)
case NOT_YET_IMPLEMENTED_Pattern:
print("Dropping: ", *objects)
case NOT_YET_IMPLEMENTED_Pattern:
print(f"Sorry, I couldn't understand {command!r}")
NOT_YET_IMPLEMENTED_StmtMatch
match command.split():
case NOT_YET_IMPLEMENTED_Pattern:
current_room = current_room.neighbor("north")
case NOT_YET_IMPLEMENTED_Pattern:
... # Code for picking up the given object
NOT_YET_IMPLEMENTED_StmtMatch
match command.split():
case NOT_YET_IMPLEMENTED_Pattern:
current_room = current_room.neighbor(...)
# how do I know which direction to go?
NOT_YET_IMPLEMENTED_StmtMatch
match command.split():
case NOT_YET_IMPLEMENTED_Pattern:
current_room = current_room.neighbor(direction)
NOT_YET_IMPLEMENTED_StmtMatch
match command.split():
case NOT_YET_IMPLEMENTED_Pattern if direction in current_room.exits:
current_room = current_room.neighbor(direction)
case NOT_YET_IMPLEMENTED_Pattern:
print("Sorry, you can't go that way")
NOT_YET_IMPLEMENTED_StmtMatch
match event.get():
case NOT_YET_IMPLEMENTED_Pattern:
handle_click_at(x, y)
case NOT_YET_IMPLEMENTED_Pattern:
game.quit()
case NOT_YET_IMPLEMENTED_Pattern:
game.go_north()
case NOT_YET_IMPLEMENTED_Pattern:
pass # Ignore other keystrokes
case NOT_YET_IMPLEMENTED_Pattern:
raise ValueError(f"Unrecognized event: {other_event}")
NOT_YET_IMPLEMENTED_StmtMatch
match event.get():
case NOT_YET_IMPLEMENTED_Pattern:
handle_click_at(x, y)
case NOT_YET_IMPLEMENTED_Pattern:
pass # ignore other clicks
def where_is(point):
NOT_YET_IMPLEMENTED_StmtMatch
match point:
case NOT_YET_IMPLEMENTED_Pattern:
print("Origin")
case NOT_YET_IMPLEMENTED_Pattern:
print(f"Y={y}")
case NOT_YET_IMPLEMENTED_Pattern:
print(f"X={x}")
case NOT_YET_IMPLEMENTED_Pattern:
print("Somewhere else")
case NOT_YET_IMPLEMENTED_Pattern:
print("Not a point")
```
## Black Output

View file

@ -65,22 +65,25 @@ match match(
```diff
--- Black
+++ Ruff
@@ -1,35 +1,24 @@
-match something:
@@ -1,35 +1,34 @@
match something:
- case b():
- print(1 + 1)
+ case NOT_YET_IMPLEMENTED_Pattern:
print(1 + 1)
- case c(
- very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1
- ):
- print(1)
+ case NOT_YET_IMPLEMENTED_Pattern:
print(1)
- case c(
- very_complex=True,
- perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1,
- ):
- print(2)
+ case NOT_YET_IMPLEMENTED_Pattern:
print(2)
- case a:
- pass
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-match(arg) # comment
+match(
@ -106,18 +109,26 @@ match match(
+ something # fast
+)
re.match()
-match match():
match match():
- case case(
- arg, # comment
- ):
- pass
+NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
```
## Ruff Output
```py
NOT_YET_IMPLEMENTED_StmtMatch
match something:
case NOT_YET_IMPLEMENTED_Pattern:
print(1 + 1)
case NOT_YET_IMPLEMENTED_Pattern:
print(1)
case NOT_YET_IMPLEMENTED_Pattern:
print(2)
case NOT_YET_IMPLEMENTED_Pattern:
pass
match(
arg # comment
@ -140,7 +151,9 @@ re.match(
something # fast
)
re.match()
NOT_YET_IMPLEMENTED_StmtMatch
match match():
case NOT_YET_IMPLEMENTED_Pattern:
pass
```
## Black Output

View file

@ -31,28 +31,39 @@ def http_status(status):
```diff
--- Black
+++ Ruff
@@ -1,13 +1,2 @@
@@ -1,13 +1,10 @@
def http_status(status):
- match status:
match status:
- case 400:
- return "Bad request"
+ case NOT_YET_IMPLEMENTED_Pattern:
return "Bad request"
-
- case 401:
- return "Unauthorized"
+ case NOT_YET_IMPLEMENTED_Pattern:
return "Unauthorized"
-
- case 403:
- return "Forbidden"
+ case NOT_YET_IMPLEMENTED_Pattern:
return "Forbidden"
-
- case 404:
- return "Not found"
+ NOT_YET_IMPLEMENTED_StmtMatch
+ case NOT_YET_IMPLEMENTED_Pattern:
return "Not found"
```
## Ruff Output
```py
def http_status(status):
NOT_YET_IMPLEMENTED_StmtMatch
match status:
case NOT_YET_IMPLEMENTED_Pattern:
return "Bad request"
case NOT_YET_IMPLEMENTED_Pattern:
return "Unauthorized"
case NOT_YET_IMPLEMENTED_Pattern:
return "Forbidden"
case NOT_YET_IMPLEMENTED_Pattern:
return "Not found"
```
## Black Output

View file

@ -0,0 +1,126 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py
---
## Input
```py
# leading match comment
match foo: # dangling match comment
case "bar":
pass
# leading match comment
match ( # leading expr comment
# another leading expr comment
foo # trailing expr comment
# another trailing expr comment
): # dangling match comment
case "bar":
pass
# leading match comment
match ( # hello
foo # trailing expr comment
, # another
): # dangling match comment
case "bar":
pass
match [ # comment
first,
second,
third
]: # another comment
case ["a", "b", "c"]:
pass
match ( # comment
"a b c"
).split(): # another comment
case ["a", "b", "c"]:
pass
match ( # comment
# let's go
yield foo
): # another comment
case ["a", "b", "c"]:
pass
match aaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh: # comment
case "sshhhhhhhh":
pass
def foo():
match inside_func: # comment
case "bar":
pass
```
## Output
```py
# leading match comment
match foo: # dangling match comment
case NOT_YET_IMPLEMENTED_Pattern:
pass
# leading match comment
match (
# leading expr comment
# another leading expr comment
foo # trailing expr comment
# another trailing expr comment
): # dangling match comment
case NOT_YET_IMPLEMENTED_Pattern:
pass
# leading match comment
match ( # hello
foo, # trailing expr comment # another
): # dangling match comment
case NOT_YET_IMPLEMENTED_Pattern:
pass
match [first, second, third]: # comment # another comment
case NOT_YET_IMPLEMENTED_Pattern:
pass
match (
# comment
"a b c"
).split(): # another comment
case NOT_YET_IMPLEMENTED_Pattern:
pass
match (
# comment
# let's go
yield foo
): # another comment
case NOT_YET_IMPLEMENTED_Pattern:
pass
match aaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh: # comment
case NOT_YET_IMPLEMENTED_Pattern:
pass
def foo():
match inside_func: # comment
case NOT_YET_IMPLEMENTED_Pattern:
pass
```