mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 10:49:50 +00:00
Handle pattern parentheses in FormatPattern
(#6800)
## Summary This PR fixes the duplicate-parenthesis problem that's visible in the tests from https://github.com/astral-sh/ruff/pull/6799. The issue is that we might have parentheses around the entire match-case pattern, like in `(1)` here: ```python match foo: case (1): y = 0 ``` In this case, the inner expression (`1`) will _think_ it's parenthesized, but we'll _also_ detect the parentheses at the case level -- so they get rendered by the case, then again by the expression. Instead, if we detect parentheses at the case level, we can force-off the parentheses for the pattern using a design similar to the way we handle parentheses on expressions. Closes https://github.com/astral-sh/ruff/issues/6753. ## Test Plan `cargo test`
This commit is contained in:
parent
281ce56dc1
commit
6f23469e00
15 changed files with 406 additions and 124 deletions
|
@ -263,6 +263,7 @@ match foo:
|
|||
):
|
||||
y = 1
|
||||
|
||||
|
||||
match foo:
|
||||
case [1, 2, *rest]:
|
||||
pass
|
||||
|
@ -299,3 +300,47 @@ match foo:
|
|||
_, 1, 2]:
|
||||
pass
|
||||
|
||||
|
||||
match foo:
|
||||
case (1):
|
||||
pass
|
||||
case ((1)):
|
||||
pass
|
||||
case [(1), 2]:
|
||||
pass
|
||||
case [( # comment
|
||||
1
|
||||
), 2]:
|
||||
pass
|
||||
case [ # outer
|
||||
( # inner
|
||||
1
|
||||
), 2]:
|
||||
pass
|
||||
case [
|
||||
( # outer
|
||||
[ # inner
|
||||
1,
|
||||
]
|
||||
)
|
||||
]:
|
||||
pass
|
||||
case [ # outer
|
||||
( # inner outer
|
||||
[ # inner
|
||||
1,
|
||||
]
|
||||
)
|
||||
]:
|
||||
pass
|
||||
case [ # outer
|
||||
# own line
|
||||
( # inner outer
|
||||
[ # inner
|
||||
1,
|
||||
]
|
||||
)
|
||||
]:
|
||||
pass
|
||||
case [(*rest), (a as b)]:
|
||||
pass
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue