Skip to content

Commit bdf4884

Browse files
authored
Preserve tuple parentheses in case patterns (#18147)
1 parent 01eeb2f commit bdf4884

File tree

6 files changed

+86
-18
lines changed

6 files changed

+86
-18
lines changed

crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern/pattern_maybe_parenthesize.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,5 +288,13 @@
288288
]:
289289
pass
290290

291-
291+
match a, b:
292+
case [], []:
293+
...
294+
case [], _:
295+
...
296+
case _, []:
297+
...
298+
case _, _:
299+
...
292300

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Ruff in some cases added brackets around tuples in some cases; deviating from Black.
2+
# Ensure we don't revert already-applied formats with the fix.
3+
# See https://github.com/astral-sh/ruff/pull/18147
4+
match a, b:
5+
case [[], []]:
6+
...
7+
case [[], _]:
8+
...

crates/ruff_python_formatter/src/pattern/pattern_match_sequence.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,26 @@ pub(crate) enum SequenceType {
7979

8080
impl SequenceType {
8181
pub(crate) fn from_pattern(pattern: &PatternMatchSequence, source: &str) -> SequenceType {
82-
if source[pattern.range()].starts_with('[') {
82+
let before_first_pattern = &source[TextRange::new(
83+
pattern.start(),
84+
pattern
85+
.patterns
86+
.first()
87+
.map(Ranged::start)
88+
.unwrap_or(pattern.end()),
89+
)];
90+
let after_last_patttern = &source[TextRange::new(
91+
pattern.start(),
92+
pattern
93+
.patterns
94+
.first()
95+
.map(Ranged::end)
96+
.unwrap_or(pattern.end()),
97+
)];
98+
99+
if before_first_pattern.starts_with('[') && !after_last_patttern.ends_with(',') {
83100
SequenceType::List
84-
} else if source[pattern.range()].starts_with('(') {
101+
} else if before_first_pattern.starts_with('(') {
85102
// If the pattern is empty, it must be a parenthesized tuple with no members. (This
86103
// branch exists to differentiate between a tuple with and without its own parentheses,
87104
// but a tuple without its own parentheses must have at least one member.)

crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__pattern_matching_trailing_comma.py.snap

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: crates/ruff_python_formatter/tests/fixtures.rs
33
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/pattern_matching_trailing_comma.py
4-
snapshot_kind: text
54
---
65
## Input
76

@@ -27,7 +26,7 @@ match more := (than, one), indeed,:
2726
```diff
2827
--- Black
2928
+++ Ruff
30-
@@ -8,13 +8,16 @@
29+
@@ -8,7 +8,10 @@
3130
pass
3231
3332
@@ -38,15 +37,7 @@ match more := (than, one), indeed,:
3837
+):
3938
case _, (5, 6):
4039
pass
41-
- case (
42-
+ case [
43-
[[5], (6)],
44-
[7],
45-
- ):
46-
+ ]:
47-
pass
48-
case _:
49-
pass
40+
case (
5041
```
5142

5243
## Ruff Output
@@ -68,10 +59,10 @@ match (
6859
):
6960
case _, (5, 6):
7061
pass
71-
case [
62+
case (
7263
[[5], (6)],
7364
[7],
74-
]:
65+
):
7566
pass
7667
case _:
7768
pass

crates/ruff_python_formatter/tests/snapshots/format@pattern__pattern_maybe_parenthesize.py.snap

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: crates/ruff_python_formatter/tests/fixtures.rs
33
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern/pattern_maybe_parenthesize.py
4-
snapshot_kind: text
54
---
65
## Input
76
```python
@@ -295,7 +294,15 @@ match x:
295294
]:
296295
pass
297296
298-
297+
match a, b:
298+
case [], []:
299+
...
300+
case [], _:
301+
...
302+
case _, []:
303+
...
304+
case _, _:
305+
...
299306
300307
```
301308

@@ -592,4 +599,14 @@ match x:
592599
ccccccccccccccccccccccccccccccccc,
593600
]:
594601
pass
602+
603+
match a, b:
604+
case [], []:
605+
...
606+
case [], _:
607+
...
608+
case _, []:
609+
...
610+
case _, _:
611+
...
595612
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
source: crates/ruff_python_formatter/tests/fixtures.rs
3+
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern_match_regression_brackets.py
4+
---
5+
## Input
6+
```python
7+
# Ruff in some cases added brackets around tuples in some cases; deviating from Black.
8+
# Ensure we don't revert already-applied formats with the fix.
9+
# See https://github.com/astral-sh/ruff/pull/18147
10+
match a, b:
11+
case [[], []]:
12+
...
13+
case [[], _]:
14+
...
15+
```
16+
17+
## Output
18+
```python
19+
# Ruff in some cases added brackets around tuples in some cases; deviating from Black.
20+
# Ensure we don't revert already-applied formats with the fix.
21+
# See https://github.com/astral-sh/ruff/pull/18147
22+
match a, b:
23+
case [[], []]:
24+
...
25+
case [[], _]:
26+
...
27+
```

0 commit comments

Comments
 (0)