Skip to content

Commit

Permalink
Implement FormatPatternMatchValue (#6799)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
charliermarsh authored Aug 23, 2023
1 parent 4bdd99f commit 71c25e4
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 405 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,30 @@ def 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
13 changes: 4 additions & 9 deletions crates/ruff_python_formatter/src/pattern/pattern_match_value.rs
Original file line number Diff line number Diff line change
@@ -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<PatternMatchValue> 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -353,17 +278,17 @@ 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:
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
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:
Expand All @@ -389,19 +314,19 @@ 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:
case NOT_YET_IMPLEMENTED_PatternMatchOf | (y):
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:
Expand All @@ -417,60 +342,49 @@ 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:
case {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}:
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:
case [x, y, *NOT_YET_IMPLEMENTED_PatternMatchStar]:
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,):
case [y]:
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:
case None:
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:
Expand All @@ -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:
Expand Down
Loading

0 comments on commit 71c25e4

Please sign in to comment.