mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
Prefer preserving WithItem
parentheses (#7694)
This commit is contained in:
parent
1e173f7909
commit
f53c410ff8
3 changed files with 83 additions and 4 deletions
|
@ -288,3 +288,22 @@ with (
|
||||||
|
|
||||||
with (foo() as bar, baz() as bop):
|
with (foo() as bar, baz() as bop):
|
||||||
pass
|
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
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
use ruff_formatter::write;
|
use ruff_formatter::write;
|
||||||
|
|
||||||
use ruff_python_ast::WithItem;
|
use ruff_python_ast::WithItem;
|
||||||
|
|
||||||
use crate::comments::SourceComment;
|
use crate::comments::SourceComment;
|
||||||
use crate::expression::maybe_parenthesize_expression;
|
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::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -20,12 +23,24 @@ impl FormatNodeRule<WithItem> for FormatWithItem {
|
||||||
let comments = f.context().comments().clone();
|
let comments = f.context().comments().clone();
|
||||||
let trailing_as_comments = comments.dangling(item);
|
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!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[maybe_parenthesize_expression(
|
[maybe_parenthesize_expression(
|
||||||
context_expr,
|
context_expr,
|
||||||
item,
|
item,
|
||||||
Parenthesize::IfRequired
|
parenthesize
|
||||||
)]
|
)]
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
|
|
@ -294,6 +294,25 @@ with (
|
||||||
|
|
||||||
with (foo() as bar, baz() as bop):
|
with (foo() as bar, baz() as bop):
|
||||||
pass
|
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
|
## Output
|
||||||
|
@ -580,13 +599,17 @@ with f(
|
||||||
) as b, c as d:
|
) as b, c as d:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b:
|
with (
|
||||||
|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||||
|
) as b:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b:
|
with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b, c as d:
|
with (
|
||||||
|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||||
|
) as b, c as d:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
with (
|
with (
|
||||||
|
@ -605,6 +628,28 @@ with (
|
||||||
|
|
||||||
with foo() as bar, baz() as bop:
|
with foo() as bar, baz() as bop:
|
||||||
pass
|
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
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue