mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 22:54:42 +00:00
Preserve tuple parentheses in case patterns (#18147)
This commit is contained in:
parent
01eeb2f0d6
commit
bdf488462a
6 changed files with 86 additions and 18 deletions
|
@ -288,5 +288,13 @@ match x:
|
||||||
]:
|
]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
match a, b:
|
||||||
|
case [], []:
|
||||||
|
...
|
||||||
|
case [], _:
|
||||||
|
...
|
||||||
|
case _, []:
|
||||||
|
...
|
||||||
|
case _, _:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
|
@ -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 [[], _]:
|
||||||
|
...
|
|
@ -79,9 +79,26 @@ pub(crate) enum SequenceType {
|
||||||
|
|
||||||
impl SequenceType {
|
impl SequenceType {
|
||||||
pub(crate) fn from_pattern(pattern: &PatternMatchSequence, source: &str) -> 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
|
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
|
// 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,
|
// 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.)
|
// but a tuple without its own parentheses must have at least one member.)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/pattern_matching_trailing_comma.py
|
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/pattern_matching_trailing_comma.py
|
||||||
snapshot_kind: text
|
|
||||||
---
|
---
|
||||||
## Input
|
## Input
|
||||||
|
|
||||||
|
@ -27,7 +26,7 @@ match more := (than, one), indeed,:
|
||||||
```diff
|
```diff
|
||||||
--- Black
|
--- Black
|
||||||
+++ Ruff
|
+++ Ruff
|
||||||
@@ -8,13 +8,16 @@
|
@@ -8,7 +8,10 @@
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,15 +37,7 @@ match more := (than, one), indeed,:
|
||||||
+):
|
+):
|
||||||
case _, (5, 6):
|
case _, (5, 6):
|
||||||
pass
|
pass
|
||||||
- case (
|
case (
|
||||||
+ case [
|
|
||||||
[[5], (6)],
|
|
||||||
[7],
|
|
||||||
- ):
|
|
||||||
+ ]:
|
|
||||||
pass
|
|
||||||
case _:
|
|
||||||
pass
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Ruff Output
|
## Ruff Output
|
||||||
|
@ -68,10 +59,10 @@ match (
|
||||||
):
|
):
|
||||||
case _, (5, 6):
|
case _, (5, 6):
|
||||||
pass
|
pass
|
||||||
case [
|
case (
|
||||||
[[5], (6)],
|
[[5], (6)],
|
||||||
[7],
|
[7],
|
||||||
]:
|
):
|
||||||
pass
|
pass
|
||||||
case _:
|
case _:
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern/pattern_maybe_parenthesize.py
|
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern/pattern_maybe_parenthesize.py
|
||||||
snapshot_kind: text
|
|
||||||
---
|
---
|
||||||
## Input
|
## Input
|
||||||
```python
|
```python
|
||||||
|
@ -295,7 +294,15 @@ match x:
|
||||||
]:
|
]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
match a, b:
|
||||||
|
case [], []:
|
||||||
|
...
|
||||||
|
case [], _:
|
||||||
|
...
|
||||||
|
case _, []:
|
||||||
|
...
|
||||||
|
case _, _:
|
||||||
|
...
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -592,4 +599,14 @@ match x:
|
||||||
ccccccccccccccccccccccccccccccccc,
|
ccccccccccccccccccccccccccccccccc,
|
||||||
]:
|
]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
match a, b:
|
||||||
|
case [], []:
|
||||||
|
...
|
||||||
|
case [], _:
|
||||||
|
...
|
||||||
|
case _, []:
|
||||||
|
...
|
||||||
|
case _, _:
|
||||||
|
...
|
||||||
```
|
```
|
||||||
|
|
|
@ -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 [[], _]:
|
||||||
|
...
|
||||||
|
```
|
Loading…
Add table
Add a link
Reference in a new issue