diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py index d3590f2f8ca3b..9bf4b31bd4fa9 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py @@ -522,3 +522,45 @@ def foo(): case a, b,: pass + + case (a, # comment + ): + pass + + case (a, b # comment + ): + pass + + case (a, b, # comment + ): + pass + + case ( # comment + a, + ): + pass + + case ( # comment + a, b + ): + pass + + case ( # comment + a, b, + ): + pass + + case ( + # comment + a,): + pass + + case ( + # comment + a, b): + pass + + case ( + # comment + a, b,): + pass 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 f6bb3152cd7f0..86f4d6f7f6904 100644 --- a/crates/ruff_python_formatter/src/pattern/pattern_match_sequence.rs +++ b/crates/ruff_python_formatter/src/pattern/pattern_match_sequence.rs @@ -22,7 +22,8 @@ impl FormatNodeRule for FormatPatternMatchSequence { let sequence_type = SequenceType::from_pattern(item, f.context().source()); match (patterns.as_slice(), sequence_type) { - // If the sequence is empty, the parentheses with any dangling comments. + // If the sequence is empty, format the empty parentheses, along with any dangling + // comments. ([], SequenceType::Tuple | SequenceType::TupleNoParens) => { return empty_parenthesized("(", dangling, ")").fmt(f) } diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap index bc0a0bce3ba26..16f7e29cf1022 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap @@ -528,6 +528,48 @@ match pattern: case a, b,: pass + + case (a, # comment + ): + pass + + case (a, b # comment + ): + pass + + case (a, b, # comment + ): + pass + + case ( # comment + a, + ): + pass + + case ( # comment + a, b + ): + pass + + case ( # comment + a, b, + ): + pass + + case ( + # comment + a,): + pass + + case ( + # comment + a, b): + pass + + case ( + # comment + a, b,): + pass ``` ## Output @@ -1086,6 +1128,60 @@ match pattern: b, ): pass + + case ( + a, # comment + ): + pass + + case ( + a, + b, # comment + ): + pass + + case ( + a, + b, # comment + ): + pass + + case ( # comment + a, + ): + pass + + case ( # comment + a, + b, + ): + pass + + case ( # comment + a, + b, + ): + pass + + case ( + # comment + a, + ): + pass + + case ( + # comment + a, + b, + ): + pass + + case ( + # comment + a, + b, + ): + pass ```