-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Format PatternMatchClass
#6860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Format PatternMatchClass
#6860
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,41 @@ | ||
use ruff_formatter::{write, Buffer, FormatResult}; | ||
use ruff_formatter::{write, FormatResult}; | ||
use ruff_python_ast::PatternMatchClass; | ||
|
||
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 FormatPatternMatchClass; | ||
|
||
impl FormatNodeRule<PatternMatchClass> for FormatPatternMatchClass { | ||
fn fmt_fields(&self, item: &PatternMatchClass, f: &mut PyFormatter) -> FormatResult<()> { | ||
write!( | ||
f, | ||
[not_yet_implemented_custom_text( | ||
"NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0)", | ||
item | ||
)] | ||
) | ||
let PatternMatchClass { | ||
range, | ||
cls, | ||
patterns, | ||
kwd_attrs, | ||
kwd_patterns, | ||
} = item; | ||
|
||
let items = format_with(|f| { | ||
let mut join = f.join_comma_separated(range.end()); | ||
|
||
if !patterns.is_empty() { | ||
join.nodes(patterns.iter()); | ||
} | ||
|
||
if !kwd_attrs.is_empty() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need the |
||
for (key, value) in kwd_attrs.iter().zip(kwd_patterns.iter()) { | ||
join.entry( | ||
key, | ||
&format_with(|f| write!(f, [key.format(), text("="), value.format()])), | ||
); | ||
} | ||
} | ||
join.finish() | ||
}); | ||
|
||
write!(f, [cls.format(), parenthesized("(", &items, ")")]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try adding an extra group around items There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, group(&items) should do the trick if I'm not mistaken There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing changed for this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment below. This is fine, assuming we're talking about the same mismatch. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,36 +131,6 @@ match bar1: | |
```diff | ||
--- Black | ||
+++ Ruff | ||
@@ -5,9 +5,9 @@ | ||
print(b) | ||
case [a as b, c, d, e as f]: | ||
print(f) | ||
- case Point(a as b): | ||
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): | ||
print(b) | ||
- case Point(int() as x, int() as y): | ||
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): | ||
print(x, y) | ||
|
||
|
||
@@ -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: | ||
pass | ||
@@ -23,7 +23,7 @@ | ||
|
||
def func(match: case, case: match) -> case: | ||
match Something(): | ||
- case func(match, case): | ||
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): | ||
... | ||
case another: | ||
... | ||
@@ -32,23 +32,32 @@ | ||
match maybe, multiple: | ||
case perhaps, 5: | ||
|
@@ -199,21 +169,7 @@ match bar1: | |
assert "map" == b | ||
|
||
|
||
@@ -59,12 +68,7 @@ | ||
), | ||
case, | ||
): | ||
- case case( | ||
- match=case, | ||
- case=re.match( | ||
- loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong | ||
- ), | ||
- ): | ||
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): | ||
pass | ||
|
||
case [a as match]: | ||
@@ -80,17 +84,14 @@ | ||
@@ -80,17 +89,14 @@ | ||
|
||
|
||
match a, *b(), c: | ||
|
@@ -234,7 +190,7 @@ match bar1: | |
pass | ||
|
||
|
||
@@ -101,19 +102,22 @@ | ||
@@ -101,7 +107,12 @@ | ||
case 2 as b, 3 as c: | ||
pass | ||
|
||
|
@@ -248,19 +204,16 @@ match bar1: | |
pass | ||
|
||
|
||
match bar1: | ||
- case Foo(aa=Callable() as aa, bb=int()): | ||
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): | ||
print(bar1.aa, bar1.bb) | ||
case _: | ||
print("no match", "\n") | ||
|
||
@@ -114,6 +125,9 @@ | ||
|
||
match bar1: | ||
- case Foo( | ||
case Foo( | ||
- normal=x, perhaps=[list, {"x": d, "y": 1.0}] as y, otherwise=something, q=t as u | ||
- ): | ||
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): | ||
+ normal=x, | ||
MichaReiser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
+ perhaps=[list, {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}] as y, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, the problem here is that We can ignore this. |
||
+ otherwise=something, | ||
+ q=t as u, | ||
): | ||
pass | ||
``` | ||
|
||
|
@@ -274,25 +227,25 @@ match something: | |
print(b) | ||
case [a as b, c, d, e as f]: | ||
print(f) | ||
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): | ||
case Point(a as b): | ||
print(b) | ||
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): | ||
case Point(int() as x, int() as y): | ||
print(x, y) | ||
|
||
|
||
match = 1 | ||
case: int = re.match(something) | ||
|
||
match re.match(case): | ||
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): | ||
case type("match", match): | ||
pass | ||
case match: | ||
pass | ||
|
||
|
||
def func(match: case, case: match) -> case: | ||
match Something(): | ||
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): | ||
case func(match, case): | ||
... | ||
case another: | ||
... | ||
|
@@ -337,7 +290,12 @@ match match( | |
), | ||
case, | ||
): | ||
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): | ||
case case( | ||
match=case, | ||
case=re.match( | ||
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong | ||
), | ||
): | ||
pass | ||
|
||
case [a as match]: | ||
|
@@ -381,14 +339,19 @@ match something: | |
|
||
|
||
match bar1: | ||
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): | ||
case Foo(aa=Callable() as aa, bb=int()): | ||
print(bar1.aa, bar1.bb) | ||
case _: | ||
print("no match", "\n") | ||
|
||
|
||
match bar1: | ||
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): | ||
case Foo( | ||
normal=x, | ||
perhaps=[list, {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}] as y, | ||
otherwise=something, | ||
q=t as u, | ||
): | ||
pass | ||
``` | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add something like
with the left hand side and the right hand side split of the assignment split by comment? I wanted to add an additional comment before the
b
but that's blocked by #6866