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 (