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
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 86 additions and 18 deletions

View file

@ -288,5 +288,13 @@ match x:
]:
pass
match a, b:
case [], []:
...
case [], _:
...
case _, []:
...
case _, _:
...

View file

@ -0,0 +1,8 @@
# 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 [[], _]:
...

View file

@ -79,9 +79,26 @@ pub(crate) enum SequenceType {
impl SequenceType {
pub(crate) fn from_pattern(pattern: &PatternMatchSequence, source: &str) -> SequenceType {
if source[pattern.range()].starts_with('[') {
let before_first_pattern = &source[TextRange::new(
pattern.start(),
pattern
.patterns
.first()
.map(Ranged::start)
.unwrap_or(pattern.end()),
)];
let after_last_patttern = &source[TextRange::new(
pattern.start(),
pattern
.patterns
.first()
.map(Ranged::end)
.unwrap_or(pattern.end()),
)];
if before_first_pattern.starts_with('[') && !after_last_patttern.ends_with(',') {
SequenceType::List
} else if source[pattern.range()].starts_with('(') {
} else if before_first_pattern.starts_with('(') {
// If the pattern is empty, it must be a parenthesized tuple with no members. (This
// branch exists to differentiate between a tuple with and without its own parentheses,
// but a tuple without its own parentheses must have at least one member.)

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 [[], _]:
...
```