From ba71772d935cb04229e9a65c032468c6674437d5 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 8 Jan 2024 09:47:01 -0500 Subject: [PATCH] Parenthesize breaking named expressions in match guards (#9396) ## Summary This is an attempt to solve https://github.com/astral-sh/ruff/issues/9394 by avoiding breaks in named expressions when invalid. --- .../resources/test/fixtures/ruff/statement/match.py | 5 +++++ .../src/expression/expr_named_expr.rs | 9 +++++++-- .../tests/snapshots/format@statement__match.py.snap | 10 ++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) 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 f24f9416cc..af1e25c96f 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 @@ -109,6 +109,11 @@ match long_lines: ): # another comment pass + case { + "long_long_long_key": str(long_long_long_key) + } if value := "long long long long long long long long long long long value": + pass + match pattern_comments: case ( diff --git a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs index 3fe831c3a0..fff39ec4f2 100644 --- a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs +++ b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs @@ -3,7 +3,9 @@ use ruff_python_ast::AnyNodeRef; use ruff_python_ast::ExprNamedExpr; use crate::comments::{dangling_comments, SourceComment}; -use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; +use crate::expression::parentheses::{ + in_parentheses_only_soft_line_break_or_space, NeedsParentheses, OptionalParentheses, +}; use crate::prelude::*; #[derive(Default)] @@ -24,7 +26,10 @@ impl FormatNodeRule for FormatExprNamedExpr { write!( f, [ - group(&format_args!(target.format(), soft_line_break_or_space())), + group(&format_args![ + target.format(), + in_parentheses_only_soft_line_break_or_space() + ]), token(":=") ] )?; 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 dad7a3c526..cd64f26de6 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 @@ -115,6 +115,11 @@ match long_lines: ): # another comment pass + case { + "long_long_long_key": str(long_long_long_key) + } if value := "long long long long long long long long long long long value": + pass + match pattern_comments: case ( @@ -696,6 +701,11 @@ match long_lines: ): # another comment pass + case { + "long_long_long_key": str(long_long_long_key) + } if value := "long long long long long long long long long long long value": + pass + match pattern_comments: case (