From 71c25e4f9d138cbc0b3463ec4eadf911f15299a4 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 23 Aug 2023 10:01:14 -0400 Subject: [PATCH] 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 --- .../test/fixtures/ruff/statement/match.py | 27 ++++ .../src/pattern/pattern_match_value.rs | 13 +- ...y@py_310__pattern_matching_complex.py.snap | 136 ++++-------------- ...ty@py_310__pattern_matching_extras.py.snap | 74 ++++------ ...y@py_310__pattern_matching_generic.py.snap | 11 +- ...ty@py_310__pattern_matching_simple.py.snap | 94 +++--------- ...py_310__remove_newline_after_match.py.snap | 90 ------------ .../snapshots/format@fmt_skip__match.py.snap | 10 +- .../format@statement__ellipsis.pyi.snap | 6 +- .../snapshots/format@statement__match.py.snap | 135 ++++++++++------- 10 files changed, 189 insertions(+), 407 deletions(-) delete mode 100644 crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__remove_newline_after_match.py.snap diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py index af41e9ec64..7f4b6a956a 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py @@ -236,3 +236,30 @@ match foo: "b", ]: 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 diff --git a/crates/ruff_python_formatter/src/pattern/pattern_match_value.rs b/crates/ruff_python_formatter/src/pattern/pattern_match_value.rs index 8e824e8fe4..9dddc761f1 100644 --- a/crates/ruff_python_formatter/src/pattern/pattern_match_value.rs +++ b/crates/ruff_python_formatter/src/pattern/pattern_match_value.rs @@ -1,19 +1,14 @@ -use ruff_formatter::{write, Buffer, FormatResult}; use ruff_python_ast::PatternMatchValue; -use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter}; +use crate::prelude::*; #[derive(Default)] pub struct FormatPatternMatchValue; impl FormatNodeRule for FormatPatternMatchValue { fn fmt_fields(&self, item: &PatternMatchValue, f: &mut PyFormatter) -> FormatResult<()> { - write!( - f, - [not_yet_implemented_custom_text( - "\"NOT_YET_IMPLEMENTED_PatternMatchValue\"", - item - )] - ) + // TODO(charlie): Avoid double parentheses for parenthesized top-level `PatternMatchValue`. + let PatternMatchValue { value, range: _ } = item; + value.format().fmt(f) } } diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_complex.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_complex.py.snap index ac54cdd462..69fa19f8de 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_complex.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_complex.py.snap @@ -156,12 +156,7 @@ match x: ```diff --- Black +++ Ruff -@@ -2,97 +2,108 @@ - - # case black_test_patma_098 - match x: -- case -0j: -+ case "NOT_YET_IMPLEMENTED_PatternMatchValue": +@@ -6,7 +6,7 @@ y = 0 # case black_test_patma_142 match x: @@ -170,11 +165,7 @@ match x: y = 0 # case black_test_patma_073 match x: -- case 0 if 0: -+ case "NOT_YET_IMPLEMENTED_PatternMatchValue" if 0: - y = 0 -- case 0 if 1: -+ case "NOT_YET_IMPLEMENTED_PatternMatchValue" if 1: +@@ -16,23 +16,23 @@ y = 1 # case black_test_patma_006 match 3: @@ -204,15 +195,7 @@ match x: y = 1 case []: y = 2 - # case black_test_patma_107 - 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": +@@ -46,7 +46,7 @@ y = 0 # case black_test_patma_007 match 4: @@ -221,8 +204,7 @@ match x: x = True # case black_test_patma_154 match x: -- case 0 if x: -+ case "NOT_YET_IMPLEMENTED_PatternMatchValue" if x: +@@ -54,15 +54,15 @@ y = 0 # case black_test_patma_134 match x: @@ -242,11 +224,7 @@ match x: y = 0 # case black_test_patma_063 match x: -- case 1: -+ case "NOT_YET_IMPLEMENTED_PatternMatchValue": - y = 0 -- case 1: -+ case "NOT_YET_IMPLEMENTED_PatternMatchValue": +@@ -72,11 +72,11 @@ y = 1 # case black_test_patma_248 match x: @@ -256,29 +234,11 @@ match x: # case black_test_patma_019 match (0, 1, 2): - case [0, 1, *x, 2]: -+ case [ -+ "NOT_YET_IMPLEMENTED_PatternMatchValue", -+ "NOT_YET_IMPLEMENTED_PatternMatchValue", -+ *NOT_YET_IMPLEMENTED_PatternMatchStar, -+ "NOT_YET_IMPLEMENTED_PatternMatchValue", -+ ]: ++ case [0, 1, *NOT_YET_IMPLEMENTED_PatternMatchStar, 2]: y = 0 # case black_test_patma_052 match x: -- case [0]: -+ 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", -+ ]: +@@ -88,7 +88,7 @@ y = 2 # case black_test_patma_191 match w: @@ -287,42 +247,7 @@ match x: z = 0 # case black_test_patma_110 match x: -- case -0.25 - 1.75j: -+ 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 @@ +@@ -128,17 +128,17 @@ y = 0 # case black_test_patma_189 match w: @@ -353,7 +278,7 @@ match x: # case black_test_patma_098 match x: - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case -0j: y = 0 # case black_test_patma_142 match x: @@ -361,9 +286,9 @@ match x: y = 0 # case black_test_patma_073 match x: - case "NOT_YET_IMPLEMENTED_PatternMatchValue" if 0: + case 0 if 0: y = 0 - case "NOT_YET_IMPLEMENTED_PatternMatchValue" if 1: + case 0 if 1: y = 1 # case black_test_patma_006 match 3: @@ -389,11 +314,11 @@ match x: y = 2 # case black_test_patma_107 match x: - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case 0.25 + 1.75j: y = 0 # case black_test_patma_097 match x: - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case -0j: y = 0 # case black_test_patma_007 match 4: @@ -401,7 +326,7 @@ match 4: x = True # case black_test_patma_154 match x: - case "NOT_YET_IMPLEMENTED_PatternMatchValue" if x: + case 0 if x: y = 0 # case black_test_patma_134 match x: @@ -417,9 +342,9 @@ match Seq(): y = 0 # case black_test_patma_063 match x: - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case 1: y = 0 - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case 1: y = 1 # case black_test_patma_248 match x: @@ -427,26 +352,15 @@ match x: y = bar # case black_test_patma_019 match (0, 1, 2): - case [ - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - *NOT_YET_IMPLEMENTED_PatternMatchStar, - "NOT_YET_IMPLEMENTED_PatternMatchValue", - ]: + case [0, 1, *NOT_YET_IMPLEMENTED_PatternMatchStar, 2]: y = 0 # case black_test_patma_052 match x: - case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]: + case [0]: y = 0 - case [ - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - ] if (x := x[:0]): + case [1, 0] if (x := x[:0]): y = 1 - case [ - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - ]: + case [1, 0]: y = 2 # case black_test_patma_191 match w: @@ -454,7 +368,7 @@ match w: z = 0 # case black_test_patma_110 match x: - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case -0.25 - 1.75j: y = 0 # case black_test_patma_151 match (x,): @@ -462,7 +376,7 @@ match (x,): z = 0 # case black_test_patma_114 match x: - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case A.B.C.D: y = 0 # case black_test_patma_232 match x: @@ -470,7 +384,7 @@ match x: y = 0 # case black_test_patma_058 match x: - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case 0: y = 0 # case black_test_patma_233 match x: @@ -480,9 +394,9 @@ match x: match x: case []: y = 0 - case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]: + case [""]: y = 1 - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case "": y = 2 # case black_test_patma_156 match x: diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_extras.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_extras.py.snap index 15a9b8b33b..bbb9c5c824 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_extras.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_extras.py.snap @@ -152,7 +152,7 @@ match bar1: pass case match: pass -@@ -23,32 +23,47 @@ +@@ -23,7 +23,7 @@ def func(match: case, case: match) -> case: match Something(): @@ -161,38 +161,29 @@ match bar1: ... case another: ... - - +@@ -32,23 +32,32 @@ match maybe, multiple: -- case perhaps, 5: -+ case perhaps, "NOT_YET_IMPLEMENTED_PatternMatchValue": + case perhaps, 5: pass - case perhaps, 6,: + case ( + perhaps, -+ "NOT_YET_IMPLEMENTED_PatternMatchValue", ++ 6, + ): pass -match more := (than, one), indeed,: -- case _, (5, 6): +match ( + more := (than, one), + indeed, +): -+ case _, ( -+ "NOT_YET_IMPLEMENTED_PatternMatchValue", -+ "NOT_YET_IMPLEMENTED_PatternMatchValue", -+ ): + case _, (5, 6): pass - case [[5], (6)], [7],: + case [ -+ [ -+ ["NOT_YET_IMPLEMENTED_PatternMatchValue"], -+ "NOT_YET_IMPLEMENTED_PatternMatchValue", -+ ], -+ ["NOT_YET_IMPLEMENTED_PatternMatchValue"], ++ [[5], (6)], ++ [7], + ]: pass case _: @@ -208,7 +199,7 @@ match bar1: assert "map" == b -@@ -59,12 +74,7 @@ +@@ -59,12 +68,7 @@ ), case, ): @@ -222,7 +213,7 @@ match bar1: pass case [a as match]: -@@ -80,40 +90,43 @@ +@@ -80,17 +84,14 @@ match a, *b(), c: @@ -243,22 +234,14 @@ match bar1: pass - match something: -- case 1 as a: -+ 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, -+ ): +@@ -101,19 +102,22 @@ + case 2 as b, 3 as c: pass - case 4 as d, (5 as e), (6 | 7 as g), *h: + case ( -+ "NOT_YET_IMPLEMENTED_PatternMatchValue" as d, -+ "NOT_YET_IMPLEMENTED_PatternMatchValue" as e, ++ 4 as d, ++ 5 as e, + NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as g, + *NOT_YET_IMPLEMENTED_PatternMatchStar, + ): @@ -316,11 +299,11 @@ def func(match: case, case: match) -> case: match maybe, multiple: - case perhaps, "NOT_YET_IMPLEMENTED_PatternMatchValue": + case perhaps, 5: pass case ( perhaps, - "NOT_YET_IMPLEMENTED_PatternMatchValue", + 6, ): pass @@ -329,17 +312,11 @@ match ( more := (than, one), indeed, ): - case _, ( - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - ): + case _, (5, 6): pass case [ - [ - ["NOT_YET_IMPLEMENTED_PatternMatchValue"], - "NOT_YET_IMPLEMENTED_PatternMatchValue", - ], - ["NOT_YET_IMPLEMENTED_PatternMatchValue"], + [[5], (6)], + [7], ]: pass case _: @@ -388,18 +365,15 @@ match something: match something: - case "NOT_YET_IMPLEMENTED_PatternMatchValue" as a: + case 1 as a: + pass + + case 2 as b, 3 as c: pass case ( - "NOT_YET_IMPLEMENTED_PatternMatchValue" as b, - "NOT_YET_IMPLEMENTED_PatternMatchValue" as c, - ): - pass - - case ( - "NOT_YET_IMPLEMENTED_PatternMatchValue" as d, - "NOT_YET_IMPLEMENTED_PatternMatchValue" as e, + 4 as d, + 5 as e, NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as g, *NOT_YET_IMPLEMENTED_PatternMatchStar, ): diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_generic.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_generic.py.snap index a262921af9..74ef3ffc47 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_generic.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_generic.py.snap @@ -128,15 +128,6 @@ with match() as match: y = 0 self.assertIs(x, False) 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 @@ -217,7 +208,7 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]: x = 0 y = None match x: - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case 1e1000: y = 0 self.assertEqual(x, 0) self.assertIs(y, None) diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_simple.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_simple.py.snap index cb7b0acf4e..d370a42917 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_simple.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_simple.py.snap @@ -104,47 +104,21 @@ def where_is(point): ```diff --- Black +++ Ruff -@@ -11,82 +11,97 @@ - ... # 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) +@@ -23,7 +23,7 @@ # The rest of your commands go here match command.split(): - case ["drop", *objects]: -+ case [ -+ "NOT_YET_IMPLEMENTED_PatternMatchValue", -+ *NOT_YET_IMPLEMENTED_PatternMatchStar, -+ ]: ++ case ["drop", *NOT_YET_IMPLEMENTED_PatternMatchStar]: for obj in objects: character.drop(obj, current_room) # The rest of your commands go here - - match command.split(): -- case ["quit"]: -+ case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]: +@@ -33,24 +33,24 @@ pass -- case ["go", direction]: -+ case ["NOT_YET_IMPLEMENTED_PatternMatchValue", direction]: + case ["go", direction]: print("Going:", direction) - case ["drop", *objects]: -+ case [ -+ "NOT_YET_IMPLEMENTED_PatternMatchValue", -+ *NOT_YET_IMPLEMENTED_PatternMatchStar, -+ ]: ++ case ["drop", *NOT_YET_IMPLEMENTED_PatternMatchStar]: print("Dropping: ", *objects) case _: print(f"Sorry, I couldn't understand {command!r}") @@ -159,30 +133,17 @@ def where_is(point): match command.split(): - case ["go", ("north" | "south" | "east" | "west")]: -+ case [ -+ "NOT_YET_IMPLEMENTED_PatternMatchValue", -+ NOT_YET_IMPLEMENTED_PatternMatchOf | (y), -+ ]: ++ case ["go", NOT_YET_IMPLEMENTED_PatternMatchOf | (y)]: current_room = current_room.neighbor(...) # how do I know which direction to go? match command.split(): - case ["go", ("north" | "south" | "east" | "west") as direction]: -+ case [ -+ "NOT_YET_IMPLEMENTED_PatternMatchValue", -+ NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as direction, -+ ]: ++ case ["go", NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as direction]: current_room = current_room.neighbor(direction) match command.split(): -- case ["go", direction] if direction in current_room.exits: -+ 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", _]: +@@ -60,33 +60,33 @@ print("Sorry, you can't go that way") match event.get(): @@ -244,35 +205,29 @@ match command.split(): ... # interpret action, obj match command.split(): - case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]: + case ["quit"]: print("Goodbye!") quit_game() - case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]: + case ["look"]: current_room.describe() - case ["NOT_YET_IMPLEMENTED_PatternMatchValue", obj]: + case ["get", obj]: character.get(obj, current_room) - case ["NOT_YET_IMPLEMENTED_PatternMatchValue", direction]: + case ["go", direction]: current_room = current_room.neighbor(direction) # The rest of your commands go here match command.split(): - case [ - "NOT_YET_IMPLEMENTED_PatternMatchValue", - *NOT_YET_IMPLEMENTED_PatternMatchStar, - ]: + case ["drop", *NOT_YET_IMPLEMENTED_PatternMatchStar]: for obj in objects: character.drop(obj, current_room) # The rest of your commands go here match command.split(): - case ["NOT_YET_IMPLEMENTED_PatternMatchValue"]: + case ["quit"]: pass - case ["NOT_YET_IMPLEMENTED_PatternMatchValue", direction]: + case ["go", direction]: print("Going:", direction) - case [ - "NOT_YET_IMPLEMENTED_PatternMatchValue", - *NOT_YET_IMPLEMENTED_PatternMatchStar, - ]: + case ["drop", *NOT_YET_IMPLEMENTED_PatternMatchStar]: print("Dropping: ", *objects) case _: print(f"Sorry, I couldn't understand {command!r}") @@ -284,27 +239,18 @@ match command.split(): ... # Code for picking up the given object match command.split(): - case [ - "NOT_YET_IMPLEMENTED_PatternMatchValue", - NOT_YET_IMPLEMENTED_PatternMatchOf | (y), - ]: + case ["go", NOT_YET_IMPLEMENTED_PatternMatchOf | (y)]: current_room = current_room.neighbor(...) # how do I know which direction to go? match command.split(): - case [ - "NOT_YET_IMPLEMENTED_PatternMatchValue", - NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as direction, - ]: + case ["go", NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as direction]: current_room = current_room.neighbor(direction) match command.split(): - case [ - "NOT_YET_IMPLEMENTED_PatternMatchValue", - direction, - ] if direction in current_room.exits: + case ["go", direction] if direction in current_room.exits: current_room = current_room.neighbor(direction) - case ["NOT_YET_IMPLEMENTED_PatternMatchValue", _]: + case ["go", _]: print("Sorry, you can't go that way") match event.get(): diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__remove_newline_after_match.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__remove_newline_after_match.py.snap deleted file mode 100644 index 061ae70d6f..0000000000 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__remove_newline_after_match.py.snap +++ /dev/null @@ -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" -``` - - diff --git a/crates/ruff_python_formatter/tests/snapshots/format@fmt_skip__match.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@fmt_skip__match.py.snap index 977a4d3d44..32daa63f66 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@fmt_skip__match.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@fmt_skip__match.py.snap @@ -85,9 +85,9 @@ def http_error(status): match status : # fmt: skip case 400 : # fmt: skip return "Bad request" - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case 404: return "Not found" - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case 418: return "I'm a teapot" case _: return "Something's wrong with the internet" @@ -97,9 +97,9 @@ def http_error(status): match point: case (0, 0): # fmt: skip print("Origin") - case ("NOT_YET_IMPLEMENTED_PatternMatchValue", y): + case (0, y): print(f"Y={y}") - case (x, "NOT_YET_IMPLEMENTED_PatternMatchValue"): + case (x, 0): print(f"X={x}") case (x, y): print(f"X={x}, Y={y}") @@ -151,7 +151,7 @@ match test_variable: 40 ): # fmt: skip print("A warning has been received.") - case ("NOT_YET_IMPLEMENTED_PatternMatchValue", code, _): + case ("error", code, _): print(f"An error {code} occurred.") diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__ellipsis.pyi.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__ellipsis.pyi.snap index ebfdd97f3c..543c45b5c9 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__ellipsis.pyi.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__ellipsis.pyi.snap @@ -164,11 +164,11 @@ with True: with True: ... # comment match x: - case "NOT_YET_IMPLEMENTED_PatternMatchValue": ... - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case 1: ... + case 2: # comment ... - case "NOT_YET_IMPLEMENTED_PatternMatchValue": ... # comment + case 3: ... # comment try: ... except: ... diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap index 54d5e1be1c..8cca7405d3 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap @@ -242,13 +242,40 @@ match foo: "b", ]: 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 ```py # leading match comment match foo: # dangling match comment - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case "bar": pass @@ -258,7 +285,7 @@ match ( # leading expr comment foo # trailing expr comment # another trailing expr comment ): # dangling match comment - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case "bar": pass @@ -266,7 +293,7 @@ match ( # leading expr comment match ( # hello foo, # trailing expr comment # another ): # dangling match comment - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case "bar": pass @@ -275,21 +302,13 @@ match [ # comment second, third, ]: # another comment - case [ - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - ]: + case ["a", "b", "c"]: pass match ( # comment "a b c" ).split(): # another comment - case [ - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - ]: + case ["a", "b", "c"]: pass @@ -297,69 +316,62 @@ match ( # comment # let's go yield foo ): # another comment - case [ - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - ]: + case ["a", "b", "c"]: pass match aaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh: # comment - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case "sshhhhhhhh": pass def foo(): match inside_func: # comment - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case "bar": pass match newlines: # 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 # pass trailing comment # case 2 leading comment - case "NOT_YET_IMPLEMENTED_PatternMatchValue" if foo == 2: # second + case "case comment with newlines" if foo == 2: # second pass - case ( - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - ) if (foo := 1): # third + case "one", "newline" if (foo := 1): # third pass - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case "two newlines": pass - case "NOT_YET_IMPLEMENTED_PatternMatchValue": + case "three newlines": pass case _: pass 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 - case "NOT_YET_IMPLEMENTED_PatternMatchValue" if ( + case "this is a long line for if condition with parentheses" if ( aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2 ): # comment pass - case "NOT_YET_IMPLEMENTED_PatternMatchValue" if foo := 1: + case "named expressions aren't special" if foo := 1: pass - case "NOT_YET_IMPLEMENTED_PatternMatchValue" if (foo := 1): + case "named expressions aren't that special" if (foo := 1): pass - case "NOT_YET_IMPLEMENTED_PatternMatchValue" if ( + case "but with already broken long lines" if ( aaaaaaahhhhhhhhhhh == 1 and bbbbbbbbaaaaaahhhh == 2 ): # another comment pass @@ -465,32 +477,18 @@ match pattern_singleton: match foo: - case ( - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - ): + case "a", "b": pass case ( - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", + "a", + "b", ): pass - case ( - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - ): + case ("a", "b"): pass - case [ - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - ]: + case ["a", "b"]: pass - case ( - [ - "NOT_YET_IMPLEMENTED_PatternMatchValue", - "NOT_YET_IMPLEMENTED_PatternMatchValue", - ] - ): + case (["a", "b"]): pass @@ -500,13 +498,40 @@ match foo: # leading # leading # leading - "NOT_YET_IMPLEMENTED_PatternMatchValue", # trailing + "a", # trailing # trailing # trailing # trailing - "NOT_YET_IMPLEMENTED_PatternMatchValue", + "b", ]: 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 ```