Add formatting for MatchCase (#6360)

## Summary

This PR adds formatting support for `MatchCase` node with subs for the
`Pattern`
nodes.

## Test Plan

Added test cases for case node handling with comments, newlines.

resolves: #6299
This commit is contained in:
Dhruv Manilawala 2023-08-11 19:20:25 +05:30 committed by GitHub
parent 8b24238d19
commit c434bdd2bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 256 additions and 29 deletions

View file

@ -265,7 +265,7 @@ match x:
+ case NOT_YET_IMPLEMENTED_Pattern:
y = 0
- case [1, 0] if (x := x[:0]):
+ case NOT_YET_IMPLEMENTED_Pattern if x := x[:0]:
+ case NOT_YET_IMPLEMENTED_Pattern if (x := x[:0]):
y = 1
- case [1, 0]:
+ case NOT_YET_IMPLEMENTED_Pattern:
@ -431,7 +431,7 @@ match (0, 1, 2):
match x:
case NOT_YET_IMPLEMENTED_Pattern:
y = 0
case NOT_YET_IMPLEMENTED_Pattern if x := x[:0]:
case NOT_YET_IMPLEMENTED_Pattern if (x := x[:0]):
y = 1
case NOT_YET_IMPLEMENTED_Pattern:
y = 2

View file

@ -205,7 +205,7 @@ match bar1:
assert "map" == b
@@ -59,61 +62,47 @@
@@ -59,61 +62,51 @@
),
case,
):
@ -217,11 +217,11 @@ match bar1:
- ):
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-
- case [a as match]:
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-
- case case:
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
@ -255,11 +255,11 @@ match bar1:
- case 1 as a:
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-
- case 2 as b, 3 as c:
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-
- case 4 as d, (5 as e), (6 | 7 as g), *h:
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
@ -351,8 +351,10 @@ match match(
):
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
@ -377,8 +379,10 @@ match something:
match something:
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass

View file

@ -203,7 +203,7 @@ def where_is(point):
match event.get():
- case Click((x, y), button=Button.LEFT): # This is a left click
+ case NOT_YET_IMPLEMENTED_Pattern:
+ case NOT_YET_IMPLEMENTED_Pattern: # This is a left click
handle_click_at(x, y)
- case Click():
+ case NOT_YET_IMPLEMENTED_Pattern:
@ -306,7 +306,7 @@ match event.get():
raise ValueError(f"Unrecognized event: {other_event}")
match event.get():
case NOT_YET_IMPLEMENTED_Pattern:
case NOT_YET_IMPLEMENTED_Pattern: # This is a left click
handle_click_at(x, y)
case NOT_YET_IMPLEMENTED_Pattern:
pass # ignore other clicks

View file

@ -31,21 +31,21 @@ def http_status(status):
```diff
--- Black
+++ Ruff
@@ -1,13 +1,10 @@
@@ -1,13 +1,13 @@
def http_status(status):
match status:
- case 400:
+ case NOT_YET_IMPLEMENTED_Pattern:
return "Bad request"
-
- case 401:
+ case NOT_YET_IMPLEMENTED_Pattern:
return "Unauthorized"
-
- case 403:
+ case NOT_YET_IMPLEMENTED_Pattern:
return "Forbidden"
-
- case 404:
+ case NOT_YET_IMPLEMENTED_Pattern:
return "Not found"
@ -58,10 +58,13 @@ def http_status(status):
match status:
case NOT_YET_IMPLEMENTED_Pattern:
return "Bad request"
case NOT_YET_IMPLEMENTED_Pattern:
return "Unauthorized"
case NOT_YET_IMPLEMENTED_Pattern:
return "Forbidden"
case NOT_YET_IMPLEMENTED_Pattern:
return "Not found"
```

View file

@ -61,6 +61,59 @@ def foo():
match inside_func: # comment
case "bar":
pass
match newlines:
# case 1 leading comment
case "top level case comment with newlines": # case dangling comment
# pass leading comment
pass
# pass trailing comment
# case 2 leading comment
case "case comment with newlines" if foo == 2: # second
pass
case "one", "newline" if (foo := 1): # third
pass
case "two newlines":
pass
case "three newlines":
pass
case _:
pass
match long_lines:
case "this is a long line for if condition" if aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2: # comment
pass
case "this is a long line for if condition with parentheses" if (aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2): # comment
pass
case "named expressions aren't special" if foo := 1:
pass
case "named expressions aren't that special" if (foo := 1):
pass
case "but with already broken long lines" if (
aaaaaaahhhhhhhhhhh == 1 and
bbbbbbbbaaaaaahhhh == 2
): # another comment
pass
```
## Output
@ -124,6 +177,52 @@ def foo():
match inside_func: # comment
case NOT_YET_IMPLEMENTED_Pattern:
pass
match newlines:
# case 1 leading comment
case NOT_YET_IMPLEMENTED_Pattern: # case dangling comment
# pass leading comment
pass
# pass trailing comment
# case 2 leading comment
case NOT_YET_IMPLEMENTED_Pattern if foo == 2: # second
pass
case NOT_YET_IMPLEMENTED_Pattern if (foo := 1): # third
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
case NOT_YET_IMPLEMENTED_Pattern:
pass
match long_lines:
case NOT_YET_IMPLEMENTED_Pattern if aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2: # comment
pass
case NOT_YET_IMPLEMENTED_Pattern if (
aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2
): # comment
pass
case NOT_YET_IMPLEMENTED_Pattern if foo := 1:
pass
case NOT_YET_IMPLEMENTED_Pattern if (foo := 1):
pass
case NOT_YET_IMPLEMENTED_Pattern if (
aaaaaaahhhhhhhhhhh == 1 and bbbbbbbbaaaaaahhhh == 2
): # another comment
pass
```