Introduce AST nodes for PatternMatchClass arguments (#6881)

## Summary

This PR introduces two new AST nodes to improve the representation of
`PatternMatchClass`. As a reminder, `PatternMatchClass` looks like this:

```python
case Point2D(0, 0, x=1, y=2):
  ...
```

Historically, this was represented as a vector of patterns (for the `0,
0` portion) and parallel vectors of keyword names (for `x` and `y`) and
values (for `1` and `2`). This introduces a bunch of challenges for the
formatter, but importantly, it's also really different from how we
represent similar nodes, like arguments (`func(0, 0, x=1, y=2)`) or
parameters (`def func(x, y)`).

So, firstly, we now use a single node (`PatternArguments`) for the
entire parenthesized region, making it much more consistent with our
other nodes. So, above, `PatternArguments` would be `(0, 0, x=1, y=2)`.

Secondly, we now have a `PatternKeyword` node for `x=1` and `y=2`. This
is much more similar to the how `Keyword` is represented within
`Arguments` for call expressions.

Closes https://github.com/astral-sh/ruff/issues/6866.

Closes https://github.com/astral-sh/ruff/issues/6880.
This commit is contained in:
Charlie Marsh 2023-08-26 10:45:44 -04:00 committed by GitHub
parent ed1b4122d0
commit 15b73bdb8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 25299 additions and 25824 deletions

View file

@ -65,15 +65,7 @@ match match(
```diff
--- Black
+++ Ruff
@@ -6,30 +6,35 @@
):
print(1)
case c(
- very_complex=True,
- perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1,
+ very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1
):
print(2)
@@ -13,20 +13,26 @@
case a:
pass
@ -103,10 +95,6 @@ match match(
re.match()
match match():
case case(
- arg, # comment
+ arg # comment
):
pass
```
## Ruff Output
@ -120,7 +108,8 @@ match something:
):
print(1)
case c(
very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1
very_complex=True,
perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1,
):
print(2)
case a:
@ -149,7 +138,7 @@ re.match(
re.match()
match match():
case case(
arg # comment
arg, # comment
):
pass
```

View file

@ -464,6 +464,15 @@ match pattern_match_class:
# e
):
pass
case A(
# a
b # b
= # c
2 # d
# e
):
pass
```
## Output
@ -903,10 +912,8 @@ match pattern_match_class:
):
...
case Point2D(
# end of line
0,
0,
case Point2D( # end of line
0, 0
):
...
@ -932,8 +939,7 @@ match pattern_match_class:
case Bar(0, a=None, b="hello"):
...
case FooBar(
# leading
case FooBar( # leading
# leading
# leading
# leading
@ -951,6 +957,15 @@ match pattern_match_class:
# e
):
pass
case A(
# a
b=# b
# c
2 # d
# e
):
pass
```