Implement FormatPatternMatchValue (#6799)

## Summary

This is effectively #6608, but with additional tests.

We aren't properly handling parenthesized patterns, but that needs to be
dealt with separately as it's somewhat involved.

Closes #6555
This commit is contained in:
Charlie Marsh 2023-08-23 10:01:14 -04:00 committed by GitHub
parent 4bdd99f882
commit 71c25e4f9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 189 additions and 407 deletions

View file

@ -236,3 +236,30 @@ match foo:
"b", "b",
]: ]:
pass pass
match foo:
case 1:
y = 0
case (1):
y = 1
case (("a")):
y = 1
case ( # comment
1
):
y = 1
case (
# comment
1
):
y = 1
case (
1 # comment
):
y = 1
case (
1
# comment
):
y = 1

View file

@ -1,19 +1,14 @@
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::PatternMatchValue; use ruff_python_ast::PatternMatchValue;
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter}; use crate::prelude::*;
#[derive(Default)] #[derive(Default)]
pub struct FormatPatternMatchValue; pub struct FormatPatternMatchValue;
impl FormatNodeRule<PatternMatchValue> for FormatPatternMatchValue { impl FormatNodeRule<PatternMatchValue> for FormatPatternMatchValue {
fn fmt_fields(&self, item: &PatternMatchValue, f: &mut PyFormatter) -> FormatResult<()> { fn fmt_fields(&self, item: &PatternMatchValue, f: &mut PyFormatter) -> FormatResult<()> {
write!( // TODO(charlie): Avoid double parentheses for parenthesized top-level `PatternMatchValue`.
f, let PatternMatchValue { value, range: _ } = item;
[not_yet_implemented_custom_text( value.format().fmt(f)
"\"NOT_YET_IMPLEMENTED_PatternMatchValue\"",
item
)]
)
} }
} }

View file

@ -156,12 +156,7 @@ match x:
```diff ```diff
--- Black --- Black
+++ Ruff +++ Ruff
@@ -2,97 +2,108 @@ @@ -6,7 +6,7 @@
# case black_test_patma_098
match x:
- case -0j:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
y = 0 y = 0
# case black_test_patma_142 # case black_test_patma_142
match x: match x:
@ -170,11 +165,7 @@ match x:
y = 0 y = 0
# case black_test_patma_073 # case black_test_patma_073
match x: match x:
- case 0 if 0: @@ -16,23 +16,23 @@
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue" if 0:
y = 0
- case 0 if 1:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue" if 1:
y = 1 y = 1
# case black_test_patma_006 # case black_test_patma_006
match 3: match 3:
@ -204,15 +195,7 @@ match x:
y = 1 y = 1
case []: case []:
y = 2 y = 2
# case black_test_patma_107 @@ -46,7 +46,7 @@
match x:
- case 0.25 + 1.75j:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
y = 0
# case black_test_patma_097
match x:
- case -0j:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
y = 0 y = 0
# case black_test_patma_007 # case black_test_patma_007
match 4: match 4:
@ -221,8 +204,7 @@ match x:
x = True x = True
# case black_test_patma_154 # case black_test_patma_154
match x: match x:
- case 0 if x: @@ -54,15 +54,15 @@
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue" if x:
y = 0 y = 0
# case black_test_patma_134 # case black_test_patma_134
match x: match x:
@ -242,11 +224,7 @@ match x:
y = 0 y = 0
# case black_test_patma_063 # case black_test_patma_063
match x: match x:
- case 1: @@ -72,11 +72,11 @@
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
y = 0
- case 1:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
y = 1 y = 1
# case black_test_patma_248 # case black_test_patma_248
match x: match x:
@ -256,29 +234,11 @@ match x:
# case black_test_patma_019 # case black_test_patma_019
match (0, 1, 2): match (0, 1, 2):
- case [0, 1, *x, 2]: - case [0, 1, *x, 2]:
+ case [ + case [0, 1, *NOT_YET_IMPLEMENTED_PatternMatchStar, 2]:
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ *NOT_YET_IMPLEMENTED_PatternMatchStar,
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ ]:
y = 0 y = 0
# case black_test_patma_052 # case black_test_patma_052
match x: match x:
- case [0]: @@ -88,7 +88,7 @@
+ case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]:
y = 0
- case [1, 0] if (x := x[:0]):
+ case [
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ ] if (x := x[:0]):
y = 1
- case [1, 0]:
+ case [
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ ]:
y = 2 y = 2
# case black_test_patma_191 # case black_test_patma_191
match w: match w:
@ -287,42 +247,7 @@ match x:
z = 0 z = 0
# case black_test_patma_110 # case black_test_patma_110
match x: match x:
- case -0.25 - 1.75j: @@ -128,17 +128,17 @@
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
y = 0
# case black_test_patma_151
match (x,):
@@ -100,7 +111,7 @@
z = 0
# case black_test_patma_114
match x:
- case A.B.C.D:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
y = 0
# case black_test_patma_232
match x:
@@ -108,7 +119,7 @@
y = 0
# case black_test_patma_058
match x:
- case 0:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
y = 0
# case black_test_patma_233
match x:
@@ -118,9 +129,9 @@
match x:
case []:
y = 0
- case [""]:
+ case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]:
y = 1
- case "":
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
y = 2
# case black_test_patma_156
match x:
@@ -128,17 +139,17 @@
y = 0 y = 0
# case black_test_patma_189 # case black_test_patma_189
match w: match w:
@ -353,7 +278,7 @@ match x:
# case black_test_patma_098 # case black_test_patma_098
match x: match x:
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case -0j:
y = 0 y = 0
# case black_test_patma_142 # case black_test_patma_142
match x: match x:
@ -361,9 +286,9 @@ match x:
y = 0 y = 0
# case black_test_patma_073 # case black_test_patma_073
match x: match x:
case "NOT_YET_IMPLEMENTED_PatternMatchValue" if 0: case 0 if 0:
y = 0 y = 0
case "NOT_YET_IMPLEMENTED_PatternMatchValue" if 1: case 0 if 1:
y = 1 y = 1
# case black_test_patma_006 # case black_test_patma_006
match 3: match 3:
@ -389,11 +314,11 @@ match x:
y = 2 y = 2
# case black_test_patma_107 # case black_test_patma_107
match x: match x:
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case 0.25 + 1.75j:
y = 0 y = 0
# case black_test_patma_097 # case black_test_patma_097
match x: match x:
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case -0j:
y = 0 y = 0
# case black_test_patma_007 # case black_test_patma_007
match 4: match 4:
@ -401,7 +326,7 @@ match 4:
x = True x = True
# case black_test_patma_154 # case black_test_patma_154
match x: match x:
case "NOT_YET_IMPLEMENTED_PatternMatchValue" if x: case 0 if x:
y = 0 y = 0
# case black_test_patma_134 # case black_test_patma_134
match x: match x:
@ -417,9 +342,9 @@ match Seq():
y = 0 y = 0
# case black_test_patma_063 # case black_test_patma_063
match x: match x:
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case 1:
y = 0 y = 0
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case 1:
y = 1 y = 1
# case black_test_patma_248 # case black_test_patma_248
match x: match x:
@ -427,26 +352,15 @@ match x:
y = bar y = bar
# case black_test_patma_019 # case black_test_patma_019
match (0, 1, 2): match (0, 1, 2):
case [ case [0, 1, *NOT_YET_IMPLEMENTED_PatternMatchStar, 2]:
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
*NOT_YET_IMPLEMENTED_PatternMatchStar,
"NOT_YET_IMPLEMENTED_PatternMatchValue",
]:
y = 0 y = 0
# case black_test_patma_052 # case black_test_patma_052
match x: match x:
case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]: case [0]:
y = 0 y = 0
case [ case [1, 0] if (x := x[:0]):
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
] if (x := x[:0]):
y = 1 y = 1
case [ case [1, 0]:
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
]:
y = 2 y = 2
# case black_test_patma_191 # case black_test_patma_191
match w: match w:
@ -454,7 +368,7 @@ match w:
z = 0 z = 0
# case black_test_patma_110 # case black_test_patma_110
match x: match x:
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case -0.25 - 1.75j:
y = 0 y = 0
# case black_test_patma_151 # case black_test_patma_151
match (x,): match (x,):
@ -462,7 +376,7 @@ match (x,):
z = 0 z = 0
# case black_test_patma_114 # case black_test_patma_114
match x: match x:
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case A.B.C.D:
y = 0 y = 0
# case black_test_patma_232 # case black_test_patma_232
match x: match x:
@ -470,7 +384,7 @@ match x:
y = 0 y = 0
# case black_test_patma_058 # case black_test_patma_058
match x: match x:
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case 0:
y = 0 y = 0
# case black_test_patma_233 # case black_test_patma_233
match x: match x:
@ -480,9 +394,9 @@ match x:
match x: match x:
case []: case []:
y = 0 y = 0
case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]: case [""]:
y = 1 y = 1
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case "":
y = 2 y = 2
# case black_test_patma_156 # case black_test_patma_156
match x: match x:

View file

@ -152,7 +152,7 @@ match bar1:
pass pass
case match: case match:
pass pass
@@ -23,32 +23,47 @@ @@ -23,7 +23,7 @@
def func(match: case, case: match) -> case: def func(match: case, case: match) -> case:
match Something(): match Something():
@ -161,38 +161,29 @@ match bar1:
... ...
case another: case another:
... ...
@@ -32,23 +32,32 @@
match maybe, multiple: match maybe, multiple:
- case perhaps, 5: case perhaps, 5:
+ case perhaps, "NOT_YET_IMPLEMENTED_PatternMatchValue":
pass pass
- case perhaps, 6,: - case perhaps, 6,:
+ case ( + case (
+ perhaps, + perhaps,
+ "NOT_YET_IMPLEMENTED_PatternMatchValue", + 6,
+ ): + ):
pass pass
-match more := (than, one), indeed,: -match more := (than, one), indeed,:
- case _, (5, 6):
+match ( +match (
+ more := (than, one), + more := (than, one),
+ indeed, + indeed,
+): +):
+ case _, ( case _, (5, 6):
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ ):
pass pass
- case [[5], (6)], [7],: - case [[5], (6)], [7],:
+ case [ + case [
+ [ + [[5], (6)],
+ ["NOT_YET_IMPLEMENTED_PatternMatchValue"], + [7],
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ ],
+ ["NOT_YET_IMPLEMENTED_PatternMatchValue"],
+ ]: + ]:
pass pass
case _: case _:
@ -208,7 +199,7 @@ match bar1:
assert "map" == b assert "map" == b
@@ -59,12 +74,7 @@ @@ -59,12 +68,7 @@
), ),
case, case,
): ):
@ -222,7 +213,7 @@ match bar1:
pass pass
case [a as match]: case [a as match]:
@@ -80,40 +90,43 @@ @@ -80,17 +84,14 @@
match a, *b(), c: match a, *b(), c:
@ -243,22 +234,14 @@ match bar1:
pass pass
match something: @@ -101,19 +102,22 @@
- case 1 as a: case 2 as b, 3 as c:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue" as a:
pass
- case 2 as b, 3 as c:
+ case (
+ "NOT_YET_IMPLEMENTED_PatternMatchValue" as b,
+ "NOT_YET_IMPLEMENTED_PatternMatchValue" as c,
+ ):
pass pass
- case 4 as d, (5 as e), (6 | 7 as g), *h: - case 4 as d, (5 as e), (6 | 7 as g), *h:
+ case ( + case (
+ "NOT_YET_IMPLEMENTED_PatternMatchValue" as d, + 4 as d,
+ "NOT_YET_IMPLEMENTED_PatternMatchValue" as e, + 5 as e,
+ NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as g, + NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as g,
+ *NOT_YET_IMPLEMENTED_PatternMatchStar, + *NOT_YET_IMPLEMENTED_PatternMatchStar,
+ ): + ):
@ -316,11 +299,11 @@ def func(match: case, case: match) -> case:
match maybe, multiple: match maybe, multiple:
case perhaps, "NOT_YET_IMPLEMENTED_PatternMatchValue": case perhaps, 5:
pass pass
case ( case (
perhaps, perhaps,
"NOT_YET_IMPLEMENTED_PatternMatchValue", 6,
): ):
pass pass
@ -329,17 +312,11 @@ match (
more := (than, one), more := (than, one),
indeed, indeed,
): ):
case _, ( case _, (5, 6):
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
):
pass pass
case [ case [
[ [[5], (6)],
["NOT_YET_IMPLEMENTED_PatternMatchValue"], [7],
"NOT_YET_IMPLEMENTED_PatternMatchValue",
],
["NOT_YET_IMPLEMENTED_PatternMatchValue"],
]: ]:
pass pass
case _: case _:
@ -388,18 +365,15 @@ match something:
match something: match something:
case "NOT_YET_IMPLEMENTED_PatternMatchValue" as a: case 1 as a:
pass
case 2 as b, 3 as c:
pass pass
case ( case (
"NOT_YET_IMPLEMENTED_PatternMatchValue" as b, 4 as d,
"NOT_YET_IMPLEMENTED_PatternMatchValue" as c, 5 as e,
):
pass
case (
"NOT_YET_IMPLEMENTED_PatternMatchValue" as d,
"NOT_YET_IMPLEMENTED_PatternMatchValue" as e,
NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as g, NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as g,
*NOT_YET_IMPLEMENTED_PatternMatchStar, *NOT_YET_IMPLEMENTED_PatternMatchStar,
): ):

View file

@ -128,15 +128,6 @@ with match() as match:
y = 0 y = 0
self.assertIs(x, False) self.assertIs(x, False)
self.assertEqual(y, 0) self.assertEqual(y, 0)
@@ -73,7 +73,7 @@
x = 0
y = None
match x:
- case 1e1000:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
y = 0
self.assertEqual(x, 0)
self.assertIs(y, None)
``` ```
## Ruff Output ## Ruff Output
@ -217,7 +208,7 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
x = 0 x = 0
y = None y = None
match x: match x:
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case 1e1000:
y = 0 y = 0
self.assertEqual(x, 0) self.assertEqual(x, 0)
self.assertIs(y, None) self.assertIs(y, None)

View file

@ -104,47 +104,21 @@ def where_is(point):
```diff ```diff
--- Black --- Black
+++ Ruff +++ Ruff
@@ -11,82 +11,97 @@ @@ -23,7 +23,7 @@
... # interpret action, obj
match command.split():
- case ["quit"]:
+ case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]:
print("Goodbye!")
quit_game()
- case ["look"]:
+ case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]:
current_room.describe()
- case ["get", obj]:
+ case ["NOT_YET_IMPLEMENTED_PatternMatchValue", obj]:
character.get(obj, current_room)
- case ["go", direction]:
+ case ["NOT_YET_IMPLEMENTED_PatternMatchValue", direction]:
current_room = current_room.neighbor(direction)
# The rest of your commands go here # The rest of your commands go here
match command.split(): match command.split():
- case ["drop", *objects]: - case ["drop", *objects]:
+ case [ + case ["drop", *NOT_YET_IMPLEMENTED_PatternMatchStar]:
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ *NOT_YET_IMPLEMENTED_PatternMatchStar,
+ ]:
for obj in objects: for obj in objects:
character.drop(obj, current_room) character.drop(obj, current_room)
# The rest of your commands go here # The rest of your commands go here
@@ -33,24 +33,24 @@
match command.split():
- case ["quit"]:
+ case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]:
pass pass
- case ["go", direction]: case ["go", direction]:
+ case ["NOT_YET_IMPLEMENTED_PatternMatchValue", direction]:
print("Going:", direction) print("Going:", direction)
- case ["drop", *objects]: - case ["drop", *objects]:
+ case [ + case ["drop", *NOT_YET_IMPLEMENTED_PatternMatchStar]:
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ *NOT_YET_IMPLEMENTED_PatternMatchStar,
+ ]:
print("Dropping: ", *objects) print("Dropping: ", *objects)
case _: case _:
print(f"Sorry, I couldn't understand {command!r}") print(f"Sorry, I couldn't understand {command!r}")
@ -159,30 +133,17 @@ def where_is(point):
match command.split(): match command.split():
- case ["go", ("north" | "south" | "east" | "west")]: - case ["go", ("north" | "south" | "east" | "west")]:
+ case [ + case ["go", NOT_YET_IMPLEMENTED_PatternMatchOf | (y)]:
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ NOT_YET_IMPLEMENTED_PatternMatchOf | (y),
+ ]:
current_room = current_room.neighbor(...) current_room = current_room.neighbor(...)
# how do I know which direction to go? # how do I know which direction to go?
match command.split(): match command.split():
- case ["go", ("north" | "south" | "east" | "west") as direction]: - case ["go", ("north" | "south" | "east" | "west") as direction]:
+ case [ + case ["go", NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as direction]:
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as direction,
+ ]:
current_room = current_room.neighbor(direction) current_room = current_room.neighbor(direction)
match command.split(): match command.split():
- case ["go", direction] if direction in current_room.exits: @@ -60,33 +60,33 @@
+ case [
+ "NOT_YET_IMPLEMENTED_PatternMatchValue",
+ direction,
+ ] if direction in current_room.exits:
current_room = current_room.neighbor(direction)
- case ["go", _]:
+ case ["NOT_YET_IMPLEMENTED_PatternMatchValue", _]:
print("Sorry, you can't go that way") print("Sorry, you can't go that way")
match event.get(): match event.get():
@ -244,35 +205,29 @@ match command.split():
... # interpret action, obj ... # interpret action, obj
match command.split(): match command.split():
case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]: case ["quit"]:
print("Goodbye!") print("Goodbye!")
quit_game() quit_game()
case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]: case ["look"]:
current_room.describe() current_room.describe()
case ["NOT_YET_IMPLEMENTED_PatternMatchValue", obj]: case ["get", obj]:
character.get(obj, current_room) character.get(obj, current_room)
case ["NOT_YET_IMPLEMENTED_PatternMatchValue", direction]: case ["go", direction]:
current_room = current_room.neighbor(direction) current_room = current_room.neighbor(direction)
# The rest of your commands go here # The rest of your commands go here
match command.split(): match command.split():
case [ case ["drop", *NOT_YET_IMPLEMENTED_PatternMatchStar]:
"NOT_YET_IMPLEMENTED_PatternMatchValue",
*NOT_YET_IMPLEMENTED_PatternMatchStar,
]:
for obj in objects: for obj in objects:
character.drop(obj, current_room) character.drop(obj, current_room)
# The rest of your commands go here # The rest of your commands go here
match command.split(): match command.split():
case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]: case ["quit"]:
pass pass
case ["NOT_YET_IMPLEMENTED_PatternMatchValue", direction]: case ["go", direction]:
print("Going:", direction) print("Going:", direction)
case [ case ["drop", *NOT_YET_IMPLEMENTED_PatternMatchStar]:
"NOT_YET_IMPLEMENTED_PatternMatchValue",
*NOT_YET_IMPLEMENTED_PatternMatchStar,
]:
print("Dropping: ", *objects) print("Dropping: ", *objects)
case _: case _:
print(f"Sorry, I couldn't understand {command!r}") print(f"Sorry, I couldn't understand {command!r}")
@ -284,27 +239,18 @@ match command.split():
... # Code for picking up the given object ... # Code for picking up the given object
match command.split(): match command.split():
case [ case ["go", NOT_YET_IMPLEMENTED_PatternMatchOf | (y)]:
"NOT_YET_IMPLEMENTED_PatternMatchValue",
NOT_YET_IMPLEMENTED_PatternMatchOf | (y),
]:
current_room = current_room.neighbor(...) current_room = current_room.neighbor(...)
# how do I know which direction to go? # how do I know which direction to go?
match command.split(): match command.split():
case [ case ["go", NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as direction]:
"NOT_YET_IMPLEMENTED_PatternMatchValue",
NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as direction,
]:
current_room = current_room.neighbor(direction) current_room = current_room.neighbor(direction)
match command.split(): match command.split():
case [ case ["go", direction] if direction in current_room.exits:
"NOT_YET_IMPLEMENTED_PatternMatchValue",
direction,
] if direction in current_room.exits:
current_room = current_room.neighbor(direction) current_room = current_room.neighbor(direction)
case ["NOT_YET_IMPLEMENTED_PatternMatchValue", _]: case ["go", _]:
print("Sorry, you can't go that way") print("Sorry, you can't go that way")
match event.get(): match event.get():

View file

@ -1,90 +0,0 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/py_310/remove_newline_after_match.py
---
## Input
```py
def http_status(status):
match status:
case 400:
return "Bad request"
case 401:
return "Unauthorized"
case 403:
return "Forbidden"
case 404:
return "Not found"
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -1,13 +1,13 @@
def http_status(status):
match status:
- case 400:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
return "Bad request"
- case 401:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
return "Unauthorized"
- case 403:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
return "Forbidden"
- case 404:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue":
return "Not found"
```
## Ruff Output
```py
def http_status(status):
match status:
case "NOT_YET_IMPLEMENTED_PatternMatchValue":
return "Bad request"
case "NOT_YET_IMPLEMENTED_PatternMatchValue":
return "Unauthorized"
case "NOT_YET_IMPLEMENTED_PatternMatchValue":
return "Forbidden"
case "NOT_YET_IMPLEMENTED_PatternMatchValue":
return "Not found"
```
## Black Output
```py
def http_status(status):
match status:
case 400:
return "Bad request"
case 401:
return "Unauthorized"
case 403:
return "Forbidden"
case 404:
return "Not found"
```

View file

@ -85,9 +85,9 @@ def http_error(status):
match status : # fmt: skip match status : # fmt: skip
case 400 : # fmt: skip case 400 : # fmt: skip
return "Bad request" return "Bad request"
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case 404:
return "Not found" return "Not found"
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case 418:
return "I'm a teapot" return "I'm a teapot"
case _: case _:
return "Something's wrong with the internet" return "Something's wrong with the internet"
@ -97,9 +97,9 @@ def http_error(status):
match point: match point:
case (0, 0): # fmt: skip case (0, 0): # fmt: skip
print("Origin") print("Origin")
case ("NOT_YET_IMPLEMENTED_PatternMatchValue", y): case (0, y):
print(f"Y={y}") print(f"Y={y}")
case (x, "NOT_YET_IMPLEMENTED_PatternMatchValue"): case (x, 0):
print(f"X={x}") print(f"X={x}")
case (x, y): case (x, y):
print(f"X={x}, Y={y}") print(f"X={x}, Y={y}")
@ -151,7 +151,7 @@ match test_variable:
40 40
): # fmt: skip ): # fmt: skip
print("A warning has been received.") print("A warning has been received.")
case ("NOT_YET_IMPLEMENTED_PatternMatchValue", code, _): case ("error", code, _):
print(f"An error {code} occurred.") print(f"An error {code} occurred.")

View file

@ -164,11 +164,11 @@ with True:
with True: ... # comment with True: ... # comment
match x: match x:
case "NOT_YET_IMPLEMENTED_PatternMatchValue": ... case 1: ...
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case 2:
# comment # comment
... ...
case "NOT_YET_IMPLEMENTED_PatternMatchValue": ... # comment case 3: ... # comment
try: ... try: ...
except: ... except: ...

View file

@ -242,13 +242,40 @@ match foo:
"b", "b",
]: ]:
pass pass
match foo:
case 1:
y = 0
case (1):
y = 1
case (("a")):
y = 1
case ( # comment
1
):
y = 1
case (
# comment
1
):
y = 1
case (
1 # comment
):
y = 1
case (
1
# comment
):
y = 1
``` ```
## Output ## Output
```py ```py
# leading match comment # leading match comment
match foo: # dangling match comment match foo: # dangling match comment
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case "bar":
pass pass
@ -258,7 +285,7 @@ match ( # leading expr comment
foo # trailing expr comment foo # trailing expr comment
# another trailing expr comment # another trailing expr comment
): # dangling match comment ): # dangling match comment
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case "bar":
pass pass
@ -266,7 +293,7 @@ match ( # leading expr comment
match ( # hello match ( # hello
foo, # trailing expr comment # another foo, # trailing expr comment # another
): # dangling match comment ): # dangling match comment
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case "bar":
pass pass
@ -275,21 +302,13 @@ match [ # comment
second, second,
third, third,
]: # another comment ]: # another comment
case [ case ["a", "b", "c"]:
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
]:
pass pass
match ( # comment match ( # comment
"a b c" "a b c"
).split(): # another comment ).split(): # another comment
case [ case ["a", "b", "c"]:
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
]:
pass pass
@ -297,69 +316,62 @@ match ( # comment
# let's go # let's go
yield foo yield foo
): # another comment ): # another comment
case [ case ["a", "b", "c"]:
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
]:
pass pass
match aaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh: # comment match aaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh: # comment
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case "sshhhhhhhh":
pass pass
def foo(): def foo():
match inside_func: # comment match inside_func: # comment
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case "bar":
pass pass
match newlines: match newlines:
# case 1 leading comment # case 1 leading comment
case "NOT_YET_IMPLEMENTED_PatternMatchValue": # case dangling comment case "top level case comment with newlines": # case dangling comment
# pass leading comment # pass leading comment
pass pass
# pass trailing comment # pass trailing comment
# case 2 leading comment # case 2 leading comment
case "NOT_YET_IMPLEMENTED_PatternMatchValue" if foo == 2: # second case "case comment with newlines" if foo == 2: # second
pass pass
case ( case "one", "newline" if (foo := 1): # third
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
) if (foo := 1): # third
pass pass
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case "two newlines":
pass pass
case "NOT_YET_IMPLEMENTED_PatternMatchValue": case "three newlines":
pass pass
case _: case _:
pass pass
match long_lines: match long_lines:
case "NOT_YET_IMPLEMENTED_PatternMatchValue" if aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2: # comment case "this is a long line for if condition" if aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2: # comment
pass pass
case "NOT_YET_IMPLEMENTED_PatternMatchValue" if ( case "this is a long line for if condition with parentheses" if (
aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2 aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2
): # comment ): # comment
pass pass
case "NOT_YET_IMPLEMENTED_PatternMatchValue" if foo := 1: case "named expressions aren't special" if foo := 1:
pass pass
case "NOT_YET_IMPLEMENTED_PatternMatchValue" if (foo := 1): case "named expressions aren't that special" if (foo := 1):
pass pass
case "NOT_YET_IMPLEMENTED_PatternMatchValue" if ( case "but with already broken long lines" if (
aaaaaaahhhhhhhhhhh == 1 and bbbbbbbbaaaaaahhhh == 2 aaaaaaahhhhhhhhhhh == 1 and bbbbbbbbaaaaaahhhh == 2
): # another comment ): # another comment
pass pass
@ -465,32 +477,18 @@ match pattern_singleton:
match foo: match foo:
case ( case "a", "b":
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
):
pass pass
case ( case (
"NOT_YET_IMPLEMENTED_PatternMatchValue", "a",
"NOT_YET_IMPLEMENTED_PatternMatchValue", "b",
): ):
pass pass
case ( case ("a", "b"):
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
):
pass pass
case [ case ["a", "b"]:
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
]:
pass pass
case ( case (["a", "b"]):
[
"NOT_YET_IMPLEMENTED_PatternMatchValue",
"NOT_YET_IMPLEMENTED_PatternMatchValue",
]
):
pass pass
@ -500,13 +498,40 @@ match foo:
# leading # leading
# leading # leading
# leading # leading
"NOT_YET_IMPLEMENTED_PatternMatchValue", # trailing "a", # trailing
# trailing # trailing
# trailing # trailing
# trailing # trailing
"NOT_YET_IMPLEMENTED_PatternMatchValue", "b",
]: ]:
pass pass
match foo:
case 1:
y = 0
case ((1)):
y = 1
case (("a")):
y = 1
case ( # comment
(1)
):
y = 1
case (
# comment
(1)
):
y = 1
case (
(1) # comment
):
y = 1
case (
(1)
# comment
):
y = 1
``` ```