Preserve tuple parentheses in case patterns (#18147)

This commit is contained in:
Max Mynter
2025-05-22 07:52:21 +02:00
committed by GitHub
parent 01eeb2f0d6
commit bdf488462a
6 changed files with 86 additions and 18 deletions

View File

@@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/pattern_matching_trailing_comma.py
snapshot_kind: text
---
## Input
@@ -27,7 +26,7 @@ match more := (than, one), indeed,:
```diff
--- Black
+++ Ruff
@@ -8,13 +8,16 @@
@@ -8,7 +8,10 @@
pass
@@ -38,15 +37,7 @@ match more := (than, one), indeed,:
+):
case _, (5, 6):
pass
- case (
+ case [
[[5], (6)],
[7],
- ):
+ ]:
pass
case _:
pass
case (
```
## Ruff Output
@@ -68,10 +59,10 @@ match (
):
case _, (5, 6):
pass
case [
case (
[[5], (6)],
[7],
]:
):
pass
case _:
pass

View File

@@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern/pattern_maybe_parenthesize.py
snapshot_kind: text
---
## Input
```python
@@ -295,7 +294,15 @@ match x:
]:
pass
match a, b:
case [], []:
...
case [], _:
...
case _, []:
...
case _, _:
...
```
@@ -592,4 +599,14 @@ match x:
ccccccccccccccccccccccccccccccccc,
]:
pass
match a, b:
case [], []:
...
case [], _:
...
case _, []:
...
case _, _:
...
```

View File

@@ -0,0 +1,27 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern_match_regression_brackets.py
---
## Input
```python
# Ruff in some cases added brackets around tuples in some cases; deviating from Black.
# Ensure we don't revert already-applied formats with the fix.
# See https://github.com/astral-sh/ruff/pull/18147
match a, b:
case [[], []]:
...
case [[], _]:
...
```
## Output
```python
# Ruff in some cases added brackets around tuples in some cases; deviating from Black.
# Ensure we don't revert already-applied formats with the fix.
# See https://github.com/astral-sh/ruff/pull/18147
match a, b:
case [[], []]:
...
case [[], _]:
...
```