diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index 52255ec4f4611e..363eff9501d300 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -247,10 +247,13 @@ impl Format> for MaybeParenthesizeExpression<'_> { if format_expression.inspect(f)?.will_break() { // The group here is necessary because `format_expression` may contain IR elements // that refer to the group id - group(&format_expression) - .with_group_id(Some(group_id)) - .should_expand(true) - .fmt(f) + group(&format_args![ + text("("), + soft_block_indent(&format_expression), + text(")") + ]) + .with_group_id(Some(group_id)) + .fmt(f) } else { // Only add parentheses if it makes the expression fit on the line. // Using the flat version as the most expanded version gives a left-to-right splitting behavior diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@simple_cases__remove_await_parens.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@simple_cases__remove_await_parens.py.snap index 428ca613760819..628dbb5c6a0487 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@simple_cases__remove_await_parens.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@simple_cases__remove_await_parens.py.snap @@ -93,7 +93,7 @@ async def main(): ```diff --- Black +++ Ruff -@@ -21,7 +21,9 @@ +@@ -21,11 +21,15 @@ # Check comments async def main(): @@ -103,6 +103,13 @@ async def main(): + ) + async def main(): +- await asyncio.sleep(1) # Hello ++ await ( ++ asyncio.sleep(1) # Hello ++ ) + + async def main(): ``` @@ -138,7 +145,9 @@ async def main(): async def main(): - await asyncio.sleep(1) # Hello + await ( + asyncio.sleep(1) # Hello + ) async def main(): diff --git a/crates/ruff_python_formatter/tests/snapshots/format@parentheses__call_chains.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@parentheses__call_chains.py.snap index 8c8c952a79d7f3..1bd2089f6b76f6 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@parentheses__call_chains.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@parentheses__call_chains.py.snap @@ -268,7 +268,9 @@ d13 = ( ) # Doesn't fit, default -d2 = x.e().esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkfsdddd() # +d2 = ( + x.e().esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkfsdddd() # +) # Doesn't fit, fluent style d3 = (