ruff/crates/ruff_python_formatter/src
Brent Westbrook 63b1c1ea8b
Avoid extra parentheses for long match patterns with as captures (#21176)
Summary
--

This PR fixes #17796 by taking the approach mentioned in
https://github.com/astral-sh/ruff/issues/17796#issuecomment-2847943862
of simply recursing into the `MatchAs` patterns when checking if we need
parentheses. This allows us to reuse the parentheses in the inner
pattern before also breaking the `MatchAs` pattern itself:

```diff
 match class_pattern:
     case Class(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) as capture:
         pass
-    case (
-        Class(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) as capture
-    ):
+    case Class(
+        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+    ) as capture:
         pass
-    case (
-        Class(
-            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-        ) as capture
-    ):
+    case Class(
+        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+    ) as capture:
         pass
     case (
         Class(
@@ -685,13 +683,11 @@
 match sequence_pattern_brackets:
     case [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] as capture:
         pass
-    case (
-        [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] as capture
-    ):
+    case [
+        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+    ] as capture:
         pass
-    case (
-        [
-            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-        ] as capture
-    ):
+    case [
+        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+    ] as capture:
         pass
```

I haven't really resolved the question of whether or not it's okay
always to recurse, but I'm hoping the ecosystem check on this PR might
shed some light on that.

Test Plan
--

New tests based on the issue and then reviewing the ecosystem check here
2025-11-03 17:06:52 -05:00
..
comments [ruff] improve handling of intermixed comments inside from-imports (#20561) 2025-10-07 08:14:09 -07:00
expression Remove parentheses around multiple exception types on Python 3.14+ (#20768) 2025-10-14 11:17:45 -04:00
module [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
other Avoid reusing nested, interpolated quotes before Python 3.12 (#20930) 2025-10-17 08:49:16 -04:00
pattern Avoid extra parentheses for long match patterns with as captures (#21176) 2025-11-03 17:06:52 -05:00
snapshots Update insta snapshots (#14366) 2024-11-15 19:31:15 +01:00
statement Allow newlines after function headers without docstrings (#21110) 2025-10-31 14:53:40 -04:00
string Avoid reusing nested, interpolated quotes before Python 3.12 (#20930) 2025-10-17 08:49:16 -04:00
type_param [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
builders.rs Implement template strings (#17851) 2025-05-30 15:00:56 -05:00
cli.rs Remove parentheses around multiple exception types on Python 3.14+ (#20768) 2025-10-14 11:17:45 -04:00
context.rs Avoid reusing nested, interpolated quotes before Python 3.12 (#20930) 2025-10-17 08:49:16 -04:00
db.rs Update Rust toolchain to 1.88 and MSRV to 1.86 (#19011) 2025-06-28 20:24:00 +02:00
generated.rs Implement template strings (#17851) 2025-05-30 15:00:56 -05:00
lib.rs Update Rust toolchain to 1.91 (#21179) 2025-11-01 01:50:58 +00:00
main.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
options.rs [ruff] Update schemars to v1 (#20942) 2025-10-20 08:59:52 +02:00
prelude.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
preview.rs Avoid extra parentheses for long match patterns with as captures (#21176) 2025-11-03 17:06:52 -05:00
range.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
shared_traits.rs Upgrade Rust toolchain to 1.83 (#14677) 2024-11-29 12:05:05 +00:00
verbatim.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00