-
Notifications
You must be signed in to change notification settings - Fork 883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parenthesize multi-context managers #9222
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems I don't understand these rules after all :(