-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parenthesize multi-context managers (#9222)
- Loading branch information
1 parent
fa2c37b
commit a06723d
Showing
13 changed files
with
927 additions
and
648 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with.options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[ | ||
{ | ||
"target_version": "py38" | ||
}, | ||
{ | ||
"target_version": "py39", | ||
"preview": "enabled" | ||
} | ||
] |
6 changes: 6 additions & 0 deletions
6
crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with_39.options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
{ | ||
"target_version": "py39", | ||
"preview": "enabled" | ||
} | ||
] |
88 changes: 88 additions & 0 deletions
88
crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with_39.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
if True: | ||
with ( | ||
anyio.CancelScope(shield=True) | ||
if get_running_loop() | ||
else contextlib.nullcontext() | ||
): | ||
pass | ||
|
||
|
||
# Black avoids parenthesizing the with because it can make all with items fit by just breaking | ||
# around parentheses. We don't implement this optimisation because it makes it difficult to see where | ||
# the different context managers start and end. | ||
with cmd, xxxxxxxx.some_kind_of_method( | ||
some_argument=[ | ||
"first", | ||
"second", | ||
"third", | ||
] | ||
) as cmd, another, and_more as x: | ||
pass | ||
|
||
# Avoid parenthesizing single item context managers when splitting after the parentheses (can_omit_optional_parentheses) | ||
# is sufficient | ||
with xxxxxxxx.some_kind_of_method( | ||
some_argument=[ | ||
"first", | ||
"second", | ||
"third", | ||
] | ||
).another_method(): pass | ||
|
||
if True: | ||
with ( | ||
anyio.CancelScope(shield=True) | ||
if get_running_loop() | ||
else contextlib.nullcontext() | ||
): | ||
pass | ||
|
||
|
||
# Black avoids parentheses here because it can make the entire with | ||
# header fit without requiring parentheses to do so. | ||
# We don't implement this optimisation because it very difficult to see where | ||
# the different context managers start or end. | ||
with cmd, xxxxxxxx.some_kind_of_method( | ||
some_argument=[ | ||
"first", | ||
"second", | ||
"third", | ||
] | ||
) as cmd, another, and_more as x: | ||
pass | ||
|
||
# Avoid parenthesizing single item context managers when splitting after the parentheses | ||
# is sufficient | ||
with xxxxxxxx.some_kind_of_method( | ||
some_argument=[ | ||
"first", | ||
"second", | ||
"third", | ||
] | ||
).another_method(): pass | ||
|
||
# Parenthesize the with items if it makes them fit. Breaking the binary expression isn't | ||
# necessary because the entire items fit just into the 88 character limit. | ||
with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as c: | ||
pass | ||
|
||
|
||
# Black parenthesizes this binary expression but also preserves the parentheses of the first with-item. | ||
# It does so because it prefers splitting already parenthesized context managers, even if it leads to more parentheses | ||
# like in this case. | ||
with ( | ||
( | ||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb | ||
) as b, | ||
c as d, | ||
): | ||
pass | ||
|
||
if True: | ||
with anyio.CancelScope(shield=True) if get_running_loop() else contextlib.nullcontext(): | ||
pass | ||
|
||
with (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as c): | ||
pass | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.