Skip to content

Commit

Permalink
Format PatternMatchAs (#6652)
Browse files Browse the repository at this point in the history
## Summary

Add formatting for `PatternMatchAs`.

This closes #6641.

## Test Plan

Add tests for comments.
  • Loading branch information
lkh42t authored Aug 22, 2023
1 parent 42ff833 commit c34a342
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,35 @@ def foo():
pass


match pattern_comments:
case (
# 1
pattern # 2
# 3
as # 4
# 5
name # 6
# 7
):
pass


match pattern_comments:
case (
pattern
# 1
as # 2
# 3
name #4
# 5
):
pass


match x:
case (a as b) as c:
pass

match pattern_singleton:
case (
# leading 1
Expand Down
41 changes: 32 additions & 9 deletions crates/ruff_python_formatter/src/pattern/pattern_match_as.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::PatternMatchAs;
use ruff_python_ast::{Pattern, PatternMatchAs};

use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use crate::expression::parentheses::parenthesized;
use crate::prelude::*;
use crate::{FormatNodeRule, PyFormatter};

#[derive(Default)]
pub struct FormatPatternMatchAs;

impl FormatNodeRule<PatternMatchAs> for FormatPatternMatchAs {
fn fmt_fields(&self, item: &PatternMatchAs, f: &mut PyFormatter) -> FormatResult<()> {
write!(
f,
[not_yet_implemented_custom_text(
"x as NOT_YET_IMPLEMENTED_PatternMatchAs",
item
)]
)
let PatternMatchAs {
range: _,
pattern,
name,
} = item;

if let Some(name) = name {
if let Some(pattern) = pattern {
// Parenthesize nested `PatternMatchAs` like `(a as b) as c`.
if matches!(
pattern.as_ref(),
Pattern::MatchAs(PatternMatchAs {
pattern: Some(_),
..
})
) {
parenthesized("(", &pattern.format(), ")").fmt(f)?;
} else {
pattern.format().fmt(f)?;
}

write!(f, [space(), text("as"), space()])?;
}
name.format().fmt(f)
} else {
debug_assert!(pattern.is_none());
text("_").fmt(f)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ match x:
y = 0
# case black_test_patma_233
match x:
@@ -116,29 +116,29 @@
@@ -116,11 +116,11 @@
y = 0
# case black_test_patma_078
match x:
Expand All @@ -316,8 +316,7 @@ match x:
y = 2
# case black_test_patma_156
match x:
- case z:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
@@ -128,17 +128,17 @@
y = 0
# case black_test_patma_189
match w:
Expand Down Expand Up @@ -471,7 +470,7 @@ match x:
y = 2
# case black_test_patma_156
match x:
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case z:
y = 0
# case black_test_patma_189
match w:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,25 +149,23 @@ match bar1:
print(x, y)
@@ -15,40 +15,43 @@
@@ -15,7 +15,7 @@
case: int = re.match(something)
match re.match(case):
- case type("match", match):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
pass
- case match:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case match:
pass
@@ -23,32 +23,35 @@
def func(match: case, case: match) -> case:
match Something():
- case func(match, case):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
...
- case another:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case another:
...
Expand All @@ -191,8 +189,7 @@ match bar1:
- case [[5], (6)], [7],:
+ case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
pass
- case _:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case _:
pass
Expand All @@ -205,7 +202,7 @@ match bar1:
assert "map" == b
@@ -59,61 +62,51 @@
@@ -59,15 +62,10 @@
),
case,
):
Expand All @@ -222,15 +219,8 @@ match bar1:
+ case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
pass
- case case:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
pass
match match:
- case case:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
pass
case case:
@@ -80,40 +78,35 @@
match a, *b(), c:
Expand All @@ -253,7 +243,7 @@ match bar1:
match something:
- case 1 as a:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
+ case "NOT_YET_IMPLEMENTED_PatternMatchValue" as a:
pass
- case 2 as b, 3 as c:
Expand All @@ -269,8 +259,7 @@ match bar1:
- case Foo(aa=Callable() as aa, bb=int()):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
print(bar1.aa, bar1.bb)
- case _:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case _:
print("no match", "\n")
Expand Down Expand Up @@ -304,15 +293,15 @@ case: int = re.match(something)
match re.match(case):
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
pass
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case match:
pass
def func(match: case, case: match) -> case:
match Something():
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
...
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case another:
...
Expand All @@ -331,7 +320,7 @@ match (
pass
case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
pass
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case _:
pass
Expand All @@ -355,12 +344,12 @@ match match(
case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
pass
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case case:
pass
match match:
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case case:
pass
Expand All @@ -377,7 +366,7 @@ match something:
match something:
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case "NOT_YET_IMPLEMENTED_PatternMatchValue" as a:
pass
case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
Expand All @@ -390,7 +379,7 @@ match something:
match bar1:
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
print(bar1.aa, bar1.bb)
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case _:
print("no match", "\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,6 @@ with match() as match:
```diff
--- Black
+++ Ruff
@@ -24,9 +24,9 @@
]
match match:
- case case:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
match match:
- case case:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
pass
if all(version.is_python2() for version in target_versions):
@@ -46,7 +46,7 @@
def test_patma_139(self):
x = False
Expand Down Expand Up @@ -188,9 +176,9 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
]
match match:
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case case:
match match:
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case case:
pass
if all(version.is_python2() for version in target_versions):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ def where_is(point):
- case ["drop", *objects]:
+ case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
print("Dropping: ", *objects)
- case _:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case _:
print(f"Sorry, I couldn't understand {command!r}")
match command.split():
Expand Down Expand Up @@ -197,8 +196,7 @@ def where_is(point):
- case KeyPress():
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
pass # Ignore other keystrokes
- case other_event:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case other_event:
raise ValueError(f"Unrecognized event: {other_event}")
match event.get():
Expand All @@ -224,8 +222,7 @@ def where_is(point):
- case Point():
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
print("Somewhere else")
- case _:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case _:
print("Not a point")
```

Expand Down Expand Up @@ -269,7 +266,7 @@ match command.split():
print("Going:", direction)
case [NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]:
print("Dropping: ", *objects)
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case _:
print(f"Sorry, I couldn't understand {command!r}")
match command.split():
Expand Down Expand Up @@ -302,7 +299,7 @@ match event.get():
game.go_north()
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
pass # Ignore other keystrokes
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case other_event:
raise ValueError(f"Unrecognized event: {other_event}")
match event.get():
Expand All @@ -322,7 +319,7 @@ def where_is(point):
print(f"X={x}")
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
print("Somewhere else")
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case _:
print("Not a point")
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ match match(
- ):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
print(2)
- case a:
+ case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case a:
pass
-match(arg) # comment
Expand Down Expand Up @@ -127,7 +126,7 @@ match something:
print(1)
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
print(2)
case x as NOT_YET_IMPLEMENTED_PatternMatchAs:
case a:
pass
match(
Expand Down
Loading

0 comments on commit c34a342

Please sign in to comment.