Format PatternMatchClass (#6860)

This commit is contained in:
Victor Hugo Gomes 2023-08-25 16:03:37 -03:00 committed by GitHub
parent 91880b8273
commit 91a780c771
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 458 additions and 536 deletions

View file

@ -156,15 +156,6 @@ match x:
```diff
--- Black
+++ Ruff
@@ -6,7 +6,7 @@
y = 0
# case black_test_patma_142
match x:
- case bytes(z):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
y = 0
# case black_test_patma_073
match x:
@@ -16,11 +16,11 @@
y = 1
# case black_test_patma_006
@ -226,7 +217,7 @@ match x:
y = 0
# case black_test_patma_142
match x:
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case bytes(z):
y = 0
# case black_test_patma_073
match x:

View file

@ -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,14 +32,23 @@
match maybe, multiple:
case perhaps, 5:
@ -188,21 +158,7 @@ match bar1:
pass
case _:
pass
@@ -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]:
@@ -87,10 +91,10 @@
@@ -87,7 +96,7 @@
match something:
case {
"key": key as key_1,
@ -210,12 +166,8 @@ match bar1:
+ "password": NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as password,
}:
pass
- case {"maybe": something(complicated as this) as that}:
+ case {"maybe": NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0) as that}:
pass
@@ -101,19 +105,17 @@
case {"maybe": something(complicated as this) as that}:
@@ -101,7 +110,7 @@
case 2 as b, 3 as c:
pass
@ -224,20 +176,6 @@ 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")
match bar1:
- 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):
pass
```
## Ruff Output
@ -250,9 +188,9 @@ 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)
@ -260,7 +198,7 @@ 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
@ -268,7 +206,7 @@ match re.match(case):
def func(match: case, case: match) -> case:
match Something():
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case func(match, case):
...
case another:
...
@ -313,7 +251,12 @@ match match(
),
case,
):
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case case(
match=case,
case=re.match(
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
),
):
pass
case [a as match]:
@ -339,7 +282,7 @@ match something:
"password": NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as password,
}:
pass
case {"maybe": NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0) as that}:
case {"maybe": something(complicated as this) as that}:
pass
@ -355,14 +298,16 @@ 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, {"x": d, "y": 1.0}] as y, otherwise=something, q=t as u
):
pass
```

View file

@ -1,357 +0,0 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/py_310/pattern_matching_generic.py
---
## Input
```py
re.match()
match = a
with match() as match:
match = f"{match}"
re.match()
match = a
with match() as match:
match = f"{match}"
def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
if not target_versions:
# No target_version specified, so try all grammars.
return [
# Python 3.7+
pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords,
# Python 3.0-3.6
pygram.python_grammar_no_print_statement_no_exec_statement,
# Python 2.7 with future print_function import
pygram.python_grammar_no_print_statement,
# Python 2.7
pygram.python_grammar,
]
match match:
case case:
match match:
case case:
pass
if all(version.is_python2() for version in target_versions):
# Python 2-only code, so try Python 2 grammars.
return [
# Python 2.7 with future print_function import
pygram.python_grammar_no_print_statement,
# Python 2.7
pygram.python_grammar,
]
re.match()
match = a
with match() as match:
match = f"{match}"
def test_patma_139(self):
x = False
match x:
case bool(z):
y = 0
self.assertIs(x, False)
self.assertEqual(y, 0)
self.assertIs(z, x)
# Python 3-compatible code, so only try Python 3 grammar.
grammars = []
if supports_feature(target_versions, Feature.PATTERN_MATCHING):
# Python 3.10+
grammars.append(pygram.python_grammar_soft_keywords)
# If we have to parse both, try to parse async as a keyword first
if not supports_feature(
target_versions, Feature.ASYNC_IDENTIFIERS
) and not supports_feature(target_versions, Feature.PATTERN_MATCHING):
# Python 3.7-3.9
grammars.append(
pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords
)
if not supports_feature(target_versions, Feature.ASYNC_KEYWORDS):
# Python 3.0-3.6
grammars.append(pygram.python_grammar_no_print_statement_no_exec_statement)
def test_patma_155(self):
x = 0
y = None
match x:
case 1e1000:
y = 0
self.assertEqual(x, 0)
self.assertIs(y, None)
x = range(3)
match x:
case [y, case as x, z]:
w = 0
# At least one of the above branches must have been taken, because every Python
# version has exactly one of the two 'ASYNC_*' flags
return grammars
def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node:
"""Given a string with source, return the lib2to3 Node."""
if not src_txt.endswith("\n"):
src_txt += "\n"
grammars = get_grammars(set(target_versions))
re.match()
match = a
with match() as match:
match = f"{match}"
re.match()
match = a
with match() as match:
match = f"{match}"
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -46,7 +46,7 @@
def test_patma_139(self):
x = False
match x:
- case bool(z):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
y = 0
self.assertIs(x, False)
self.assertEqual(y, 0)
```
## Ruff Output
```py
re.match()
match = a
with match() as match:
match = f"{match}"
re.match()
match = a
with match() as match:
match = f"{match}"
def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
if not target_versions:
# No target_version specified, so try all grammars.
return [
# Python 3.7+
pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords,
# Python 3.0-3.6
pygram.python_grammar_no_print_statement_no_exec_statement,
# Python 2.7 with future print_function import
pygram.python_grammar_no_print_statement,
# Python 2.7
pygram.python_grammar,
]
match match:
case case:
match match:
case case:
pass
if all(version.is_python2() for version in target_versions):
# Python 2-only code, so try Python 2 grammars.
return [
# Python 2.7 with future print_function import
pygram.python_grammar_no_print_statement,
# Python 2.7
pygram.python_grammar,
]
re.match()
match = a
with match() as match:
match = f"{match}"
def test_patma_139(self):
x = False
match x:
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
y = 0
self.assertIs(x, False)
self.assertEqual(y, 0)
self.assertIs(z, x)
# Python 3-compatible code, so only try Python 3 grammar.
grammars = []
if supports_feature(target_versions, Feature.PATTERN_MATCHING):
# Python 3.10+
grammars.append(pygram.python_grammar_soft_keywords)
# If we have to parse both, try to parse async as a keyword first
if not supports_feature(
target_versions, Feature.ASYNC_IDENTIFIERS
) and not supports_feature(target_versions, Feature.PATTERN_MATCHING):
# Python 3.7-3.9
grammars.append(
pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords
)
if not supports_feature(target_versions, Feature.ASYNC_KEYWORDS):
# Python 3.0-3.6
grammars.append(pygram.python_grammar_no_print_statement_no_exec_statement)
def test_patma_155(self):
x = 0
y = None
match x:
case 1e1000:
y = 0
self.assertEqual(x, 0)
self.assertIs(y, None)
x = range(3)
match x:
case [y, case as x, z]:
w = 0
# At least one of the above branches must have been taken, because every Python
# version has exactly one of the two 'ASYNC_*' flags
return grammars
def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node:
"""Given a string with source, return the lib2to3 Node."""
if not src_txt.endswith("\n"):
src_txt += "\n"
grammars = get_grammars(set(target_versions))
re.match()
match = a
with match() as match:
match = f"{match}"
re.match()
match = a
with match() as match:
match = f"{match}"
```
## Black Output
```py
re.match()
match = a
with match() as match:
match = f"{match}"
re.match()
match = a
with match() as match:
match = f"{match}"
def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
if not target_versions:
# No target_version specified, so try all grammars.
return [
# Python 3.7+
pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords,
# Python 3.0-3.6
pygram.python_grammar_no_print_statement_no_exec_statement,
# Python 2.7 with future print_function import
pygram.python_grammar_no_print_statement,
# Python 2.7
pygram.python_grammar,
]
match match:
case case:
match match:
case case:
pass
if all(version.is_python2() for version in target_versions):
# Python 2-only code, so try Python 2 grammars.
return [
# Python 2.7 with future print_function import
pygram.python_grammar_no_print_statement,
# Python 2.7
pygram.python_grammar,
]
re.match()
match = a
with match() as match:
match = f"{match}"
def test_patma_139(self):
x = False
match x:
case bool(z):
y = 0
self.assertIs(x, False)
self.assertEqual(y, 0)
self.assertIs(z, x)
# Python 3-compatible code, so only try Python 3 grammar.
grammars = []
if supports_feature(target_versions, Feature.PATTERN_MATCHING):
# Python 3.10+
grammars.append(pygram.python_grammar_soft_keywords)
# If we have to parse both, try to parse async as a keyword first
if not supports_feature(
target_versions, Feature.ASYNC_IDENTIFIERS
) and not supports_feature(target_versions, Feature.PATTERN_MATCHING):
# Python 3.7-3.9
grammars.append(
pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords
)
if not supports_feature(target_versions, Feature.ASYNC_KEYWORDS):
# Python 3.0-3.6
grammars.append(pygram.python_grammar_no_print_statement_no_exec_statement)
def test_patma_155(self):
x = 0
y = None
match x:
case 1e1000:
y = 0
self.assertEqual(x, 0)
self.assertIs(y, None)
x = range(3)
match x:
case [y, case as x, z]:
w = 0
# At least one of the above branches must have been taken, because every Python
# version has exactly one of the two 'ASYNC_*' flags
return grammars
def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node:
"""Given a string with source, return the lib2to3 Node."""
if not src_txt.endswith("\n"):
src_txt += "\n"
grammars = get_grammars(set(target_versions))
re.match()
match = a
with match() as match:
match = f"{match}"
re.match()
match = a
with match() as match:
match = f"{match}"
```

View file

@ -127,50 +127,15 @@ def where_is(point):
current_room = current_room.neighbor(direction)
match command.split():
@@ -60,33 +60,33 @@
print("Sorry, you can't go that way")
@@ -62,7 +62,7 @@
match event.get():
- case Click(position=(x, y)):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case Click(position=(x, y)):
handle_click_at(x, y)
- case KeyPress(key_name="Q") | Quit():
+ case NOT_YET_IMPLEMENTED_PatternMatchOf | (y):
game.quit()
- case KeyPress(key_name="up arrow"):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case KeyPress(key_name="up arrow"):
game.go_north()
- case KeyPress():
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
pass # Ignore other keystrokes
case other_event:
raise ValueError(f"Unrecognized event: {other_event}")
match event.get():
- case Click((x, y), button=Button.LEFT): # This is a left click
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): # This is a left click
handle_click_at(x, y)
- case Click():
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
pass # ignore other clicks
def where_is(point):
match point:
- case Point(x=0, y=0):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
print("Origin")
- case Point(x=0, y=y):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
print(f"Y={y}")
- case Point(x=x, y=0):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
print(f"X={x}")
- case Point():
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
print("Somewhere else")
case _:
print("Not a point")
```
## Ruff Output
@ -238,33 +203,33 @@ match command.split():
print("Sorry, you can't go that way")
match event.get():
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case Click(position=(x, y)):
handle_click_at(x, y)
case NOT_YET_IMPLEMENTED_PatternMatchOf | (y):
game.quit()
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case KeyPress(key_name="up arrow"):
game.go_north()
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case KeyPress():
pass # Ignore other keystrokes
case other_event:
raise ValueError(f"Unrecognized event: {other_event}")
match event.get():
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): # This is a left click
case Click((x, y), button=Button.LEFT): # This is a left click
handle_click_at(x, y)
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case Click():
pass # ignore other clicks
def where_is(point):
match point:
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case Point(x=0, y=0):
print("Origin")
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case Point(x=0, y=y):
print(f"Y={y}")
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case Point(x=x, y=0):
print(f"X={x}")
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case Point():
print("Somewhere else")
case _:
print("Not a point")

View file

@ -65,21 +65,14 @@ match match(
```diff
--- Black
+++ Ruff
@@ -1,35 +1,34 @@
match something:
- case b():
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
print(1 + 1)
- case c(
- very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1
- ):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
@@ -6,30 +6,35 @@
):
print(1)
- case c(
case c(
- very_complex=True,
- perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1,
- ):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
+ very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1
):
print(2)
case a:
pass
@ -109,10 +102,10 @@ match match(
+)
re.match()
match match():
- case case(
case case(
- arg, # comment
- ):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
+ arg # comment
):
pass
```
@ -120,11 +113,15 @@ match match(
```py
match something:
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case b():
print(1 + 1)
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case c(
very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1
):
print(1)
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case c(
very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1
):
print(2)
case a:
pass
@ -151,7 +148,9 @@ re.match(
)
re.match()
match match():
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case case(
arg # comment
):
pass
```

View file

@ -116,11 +116,11 @@ def location(point):
match point:
case Point(x=0, y =0 ) : # fmt: skip
print("Origin is the point's location.")
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case Point(x=0, y=y):
print(f"Y={y} and the point is on the y-axis.")
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case Point(x=x, y=0):
print(f"X={x} and the point is on the x-axis.")
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case Point():
print("The point is located somewhere else on the plane.")
case _:
print("Not a point")
@ -133,12 +133,9 @@ match points:
Point(0, 0)
]: # fmt: skip
print("The origin is the only point in the list.")
case [NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0)]:
case [Point(x, y)]:
print(f"A single point {x}, {y} is in the list.")
case [
NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0),
NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0),
]:
case [Point(0, y1), Point(0, y2)]:
print(f"Two points on the Y axis at {y1}, {y2} are in the list.")
case _:
print("Something else is found in the list.")
@ -158,7 +155,7 @@ match test_variable:
match point:
case Point(x, y) if x == y: # fmt: skip
print(f"The point is located on the diagonal Y=X at {x}.")
case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
case Point(x, y):
print(f"Point is not on the diagonal.")
```

View file

@ -270,6 +270,7 @@ match foo:
y = 1
match foo:
case [1, 2, *rest]:
pass
@ -405,6 +406,64 @@ match foo:
b,
}:
pass
match pattern_match_class:
case Point2D(
# own line
):
...
case (
Point2D
# own line
()
):
...
case Point2D( # end of line line
):
...
case Point2D( # end of line
0, 0
):
...
case Point2D(0, 0):
...
case Point2D(
( # end of line
# own line
0
), 0):
...
case Point3D(x=0, y=0, z=000000000000000000000000000000000000000000000000000000000000000000000000000000000):
...
case Bar(0, a=None, b="hello"):
...
case FooBar(# leading
# leading
# leading
# leading
0 # trailing
# trailing
# trailing
# trailing
):
...
case A(
b # b
= # c
2 # d
# e
):
pass
```
## Output
@ -825,6 +884,73 @@ match foo:
**b,
}:
pass
match pattern_match_class:
case Point2D(
# own line
):
...
case (
Point2D
# own line
()
):
...
case Point2D( # end of line line
):
...
case Point2D(
# end of line
0,
0,
):
...
case Point2D(0, 0):
...
case Point2D(
( # end of line
# own line
0
),
0,
):
...
case Point3D(
x=0,
y=0,
z=000000000000000000000000000000000000000000000000000000000000000000000000000000000,
):
...
case Bar(0, a=None, b="hello"):
...
case FooBar(
# leading
# leading
# leading
# leading
0 # trailing
# trailing
# trailing
# trailing
):
...
case A(
b=# b
# c
2 # d
# e
):
pass
```