From bdf488462a5a5c0d4e104eff4e299829019b657d Mon Sep 17 00:00:00 2001 From: Max Mynter <32773644+maxmynter@users.noreply.github.com> Date: Thu, 22 May 2025 07:52:21 +0200 Subject: [PATCH] Preserve tuple parentheses in case patterns (#18147) --- .../pattern/pattern_maybe_parenthesize.py | 10 ++++++- .../ruff/pattern_match_regression_brackets.py | 8 ++++++ .../src/pattern/pattern_match_sequence.rs | 21 +++++++++++++-- ...s__pattern_matching_trailing_comma.py.snap | 17 +++--------- ...attern__pattern_maybe_parenthesize.py.snap | 21 +++++++++++++-- ...@pattern_match_regression_brackets.py.snap | 27 +++++++++++++++++++ 6 files changed, 86 insertions(+), 18 deletions(-) create mode 100644 crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern_match_regression_brackets.py create mode 100644 crates/ruff_python_formatter/tests/snapshots/format@pattern_match_regression_brackets.py.snap diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern/pattern_maybe_parenthesize.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern/pattern_maybe_parenthesize.py index b898fad81b..fe1a4a3050 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern/pattern_maybe_parenthesize.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern/pattern_maybe_parenthesize.py @@ -288,5 +288,13 @@ match x: ]: pass - +match a, b: + case [], []: + ... + case [], _: + ... + case _, []: + ... + case _, _: + ... diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern_match_regression_brackets.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern_match_regression_brackets.py new file mode 100644 index 0000000000..5f94a64eb2 --- /dev/null +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/pattern_match_regression_brackets.py @@ -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 [[], _]: + ... diff --git a/crates/ruff_python_formatter/src/pattern/pattern_match_sequence.rs b/crates/ruff_python_formatter/src/pattern/pattern_match_sequence.rs index 55ecdda922..24f07f050d 100644 --- a/crates/ruff_python_formatter/src/pattern/pattern_match_sequence.rs +++ b/crates/ruff_python_formatter/src/pattern/pattern_match_sequence.rs @@ -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.) diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__pattern_matching_trailing_comma.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__pattern_matching_trailing_comma.py.snap index fa13412b56..79f1cf71b4 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__pattern_matching_trailing_comma.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__pattern_matching_trailing_comma.py.snap @@ -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 diff --git a/crates/ruff_python_formatter/tests/snapshots/format@pattern__pattern_maybe_parenthesize.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@pattern__pattern_maybe_parenthesize.py.snap index 45fe2da38d..533245670b 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@pattern__pattern_maybe_parenthesize.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@pattern__pattern_maybe_parenthesize.py.snap @@ -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 _, _: + ... ``` diff --git a/crates/ruff_python_formatter/tests/snapshots/format@pattern_match_regression_brackets.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@pattern_match_regression_brackets.py.snap new file mode 100644 index 0000000000..ac0c88e972 --- /dev/null +++ b/crates/ruff_python_formatter/tests/snapshots/format@pattern_match_regression_brackets.py.snap @@ -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 [[], _]: + ... +```