diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with.py index a9d94a2026..05249bb9ea 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with.py @@ -288,3 +288,22 @@ with ( with (foo() as bar, baz() as bop): pass + +if True: + with ( + anyio.CancelScope(shield=True) + if get_running_loop() + else contextlib.nullcontext() + ): + pass + +if True: + with ( + anyio.CancelScope(shield=True) + and B and [aaaaaaaa, bbbbbbbbbbbbb, cccccccccc, dddddddddddd, eeeeeeeeeeeee] + ): + pass + +if True: + with anyio.CancelScope(shield=True) if get_running_loop() else contextlib.nullcontext(): + pass diff --git a/crates/ruff_python_formatter/src/other/with_item.rs b/crates/ruff_python_formatter/src/other/with_item.rs index 0559e4a993..59a7918777 100644 --- a/crates/ruff_python_formatter/src/other/with_item.rs +++ b/crates/ruff_python_formatter/src/other/with_item.rs @@ -1,9 +1,12 @@ use ruff_formatter::write; + use ruff_python_ast::WithItem; use crate::comments::SourceComment; use crate::expression::maybe_parenthesize_expression; -use crate::expression::parentheses::{parenthesized, Parentheses, Parenthesize}; +use crate::expression::parentheses::{ + is_expression_parenthesized, parenthesized, Parentheses, Parenthesize, +}; use crate::prelude::*; #[derive(Default)] @@ -20,12 +23,24 @@ impl FormatNodeRule for FormatWithItem { let comments = f.context().comments().clone(); let trailing_as_comments = comments.dangling(item); + // Prefer keeping parentheses for already parenthesized expressions over + // parenthesizing other nodes. + let parenthesize = if is_expression_parenthesized( + context_expr.into(), + f.context().comments().ranges(), + f.context().source(), + ) { + Parenthesize::IfBreaks + } else { + Parenthesize::IfRequired + }; + write!( f, [maybe_parenthesize_expression( context_expr, item, - Parenthesize::IfRequired + parenthesize )] )?; diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__with.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__with.py.snap index e49f4aaab2..0a49e0a82d 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__with.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__with.py.snap @@ -294,6 +294,25 @@ with ( with (foo() as bar, baz() as bop): pass + +if True: + with ( + anyio.CancelScope(shield=True) + if get_running_loop() + else contextlib.nullcontext() + ): + pass + +if True: + with ( + anyio.CancelScope(shield=True) + and B and [aaaaaaaa, bbbbbbbbbbbbb, cccccccccc, dddddddddddd, eeeeeeeeeeeee] + ): + pass + +if True: + with anyio.CancelScope(shield=True) if get_running_loop() else contextlib.nullcontext(): + pass ``` ## Output @@ -580,13 +599,17 @@ with f( ) as b, c as d: pass -with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b: +with ( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +) as b: pass with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b: pass -with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b, c as d: +with ( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +) as b, c as d: pass with ( @@ -605,6 +628,28 @@ with ( with foo() as bar, baz() as bop: pass + +if True: + with ( + anyio.CancelScope(shield=True) + if get_running_loop() + else contextlib.nullcontext() + ): + pass + +if True: + with ( + anyio.CancelScope(shield=True) + and B + and [aaaaaaaa, bbbbbbbbbbbbb, cccccccccc, dddddddddddd, eeeeeeeeeeeee] + ): + pass + +if True: + with anyio.CancelScope( + shield=True + ) if get_running_loop() else contextlib.nullcontext(): + pass ```