mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:56 +00:00
Parenthesize multi-context managers (#9222)
This commit is contained in:
parent
fa2c37b411
commit
a06723da2b
13 changed files with 928 additions and 649 deletions
|
@ -1,342 +0,0 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/preview_context_managers_39.py
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
with \
|
||||
make_context_manager1() as cm1, \
|
||||
make_context_manager2() as cm2, \
|
||||
make_context_manager3() as cm3, \
|
||||
make_context_manager4() as cm4 \
|
||||
:
|
||||
pass
|
||||
|
||||
|
||||
# Leading comment
|
||||
with \
|
||||
make_context_manager1() as cm1, \
|
||||
make_context_manager2(), \
|
||||
make_context_manager3() as cm3, \
|
||||
make_context_manager4() \
|
||||
:
|
||||
pass
|
||||
|
||||
|
||||
with \
|
||||
new_new_new1() as cm1, \
|
||||
new_new_new2() \
|
||||
:
|
||||
pass
|
||||
|
||||
|
||||
with (
|
||||
new_new_new1() as cm1,
|
||||
new_new_new2()
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
# Leading comment.
|
||||
with (
|
||||
# First comment.
|
||||
new_new_new1() as cm1,
|
||||
# Second comment.
|
||||
new_new_new2()
|
||||
# Last comment.
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
with \
|
||||
this_is_a_very_long_call(looong_arg1=looong_value1, looong_arg2=looong_value2) as cm1, \
|
||||
this_is_a_very_long_call(looong_arg1=looong_value1, looong_arg2=looong_value2, looong_arg3=looong_value3, looong_arg4=looong_value4) as cm2 \
|
||||
:
|
||||
pass
|
||||
|
||||
|
||||
with mock.patch.object(
|
||||
self.my_runner, "first_method", autospec=True
|
||||
) as mock_run_adb, mock.patch.object(
|
||||
self.my_runner, "second_method", autospec=True, return_value="foo"
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
with xxxxxxxx.some_kind_of_method(
|
||||
some_argument=[
|
||||
"first",
|
||||
"second",
|
||||
"third",
|
||||
]
|
||||
).another_method() as cmd:
|
||||
pass
|
||||
|
||||
|
||||
async def func():
|
||||
async with \
|
||||
make_context_manager1() as cm1, \
|
||||
make_context_manager2() as cm2, \
|
||||
make_context_manager3() as cm3, \
|
||||
make_context_manager4() as cm4 \
|
||||
:
|
||||
pass
|
||||
|
||||
async with some_function(
|
||||
argument1, argument2, argument3="some_value"
|
||||
) as some_cm, some_other_function(
|
||||
argument1, argument2, argument3="some_value"
|
||||
):
|
||||
pass
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,19 +1,9 @@
|
||||
-with (
|
||||
- make_context_manager1() as cm1,
|
||||
- make_context_manager2() as cm2,
|
||||
- make_context_manager3() as cm3,
|
||||
- make_context_manager4() as cm4,
|
||||
-):
|
||||
+with make_context_manager1() as cm1, make_context_manager2() as cm2, make_context_manager3() as cm3, make_context_manager4() as cm4:
|
||||
pass
|
||||
|
||||
|
||||
# Leading comment
|
||||
-with (
|
||||
- make_context_manager1() as cm1,
|
||||
- make_context_manager2(),
|
||||
- make_context_manager3() as cm3,
|
||||
- make_context_manager4(),
|
||||
-):
|
||||
+with make_context_manager1() as cm1, make_context_manager2(), make_context_manager3() as cm3, make_context_manager4():
|
||||
pass
|
||||
|
||||
|
||||
@@ -36,25 +26,21 @@
|
||||
pass
|
||||
|
||||
|
||||
-with (
|
||||
- this_is_a_very_long_call(
|
||||
- looong_arg1=looong_value1, looong_arg2=looong_value2
|
||||
- ) as cm1,
|
||||
- this_is_a_very_long_call(
|
||||
- looong_arg1=looong_value1,
|
||||
- looong_arg2=looong_value2,
|
||||
- looong_arg3=looong_value3,
|
||||
- looong_arg4=looong_value4,
|
||||
- ) as cm2,
|
||||
-):
|
||||
+with this_is_a_very_long_call(
|
||||
+ looong_arg1=looong_value1, looong_arg2=looong_value2
|
||||
+) as cm1, this_is_a_very_long_call(
|
||||
+ looong_arg1=looong_value1,
|
||||
+ looong_arg2=looong_value2,
|
||||
+ looong_arg3=looong_value3,
|
||||
+ looong_arg4=looong_value4,
|
||||
+) as cm2:
|
||||
pass
|
||||
|
||||
|
||||
-with (
|
||||
- mock.patch.object(self.my_runner, "first_method", autospec=True) as mock_run_adb,
|
||||
- mock.patch.object(
|
||||
- self.my_runner, "second_method", autospec=True, return_value="foo"
|
||||
- ),
|
||||
+with mock.patch.object(
|
||||
+ self.my_runner, "first_method", autospec=True
|
||||
+) as mock_run_adb, mock.patch.object(
|
||||
+ self.my_runner, "second_method", autospec=True, return_value="foo"
|
||||
):
|
||||
pass
|
||||
|
||||
@@ -70,16 +56,10 @@
|
||||
|
||||
|
||||
async def func():
|
||||
- async with (
|
||||
- make_context_manager1() as cm1,
|
||||
- make_context_manager2() as cm2,
|
||||
- make_context_manager3() as cm3,
|
||||
- make_context_manager4() as cm4,
|
||||
- ):
|
||||
+ async with make_context_manager1() as cm1, make_context_manager2() as cm2, make_context_manager3() as cm3, make_context_manager4() as cm4:
|
||||
pass
|
||||
|
||||
- async with (
|
||||
- some_function(argument1, argument2, argument3="some_value") as some_cm,
|
||||
- some_other_function(argument1, argument2, argument3="some_value"),
|
||||
- ):
|
||||
+ async with some_function(
|
||||
+ argument1, argument2, argument3="some_value"
|
||||
+ ) as some_cm, some_other_function(argument1, argument2, argument3="some_value"):
|
||||
pass
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
with make_context_manager1() as cm1, make_context_manager2() as cm2, make_context_manager3() as cm3, make_context_manager4() as cm4:
|
||||
pass
|
||||
|
||||
|
||||
# Leading comment
|
||||
with make_context_manager1() as cm1, make_context_manager2(), make_context_manager3() as cm3, make_context_manager4():
|
||||
pass
|
||||
|
||||
|
||||
with new_new_new1() as cm1, new_new_new2():
|
||||
pass
|
||||
|
||||
|
||||
with new_new_new1() as cm1, new_new_new2():
|
||||
pass
|
||||
|
||||
|
||||
# Leading comment.
|
||||
with (
|
||||
# First comment.
|
||||
new_new_new1() as cm1,
|
||||
# Second comment.
|
||||
new_new_new2(),
|
||||
# Last comment.
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
with this_is_a_very_long_call(
|
||||
looong_arg1=looong_value1, looong_arg2=looong_value2
|
||||
) as cm1, this_is_a_very_long_call(
|
||||
looong_arg1=looong_value1,
|
||||
looong_arg2=looong_value2,
|
||||
looong_arg3=looong_value3,
|
||||
looong_arg4=looong_value4,
|
||||
) as cm2:
|
||||
pass
|
||||
|
||||
|
||||
with mock.patch.object(
|
||||
self.my_runner, "first_method", autospec=True
|
||||
) as mock_run_adb, mock.patch.object(
|
||||
self.my_runner, "second_method", autospec=True, return_value="foo"
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
with xxxxxxxx.some_kind_of_method(
|
||||
some_argument=[
|
||||
"first",
|
||||
"second",
|
||||
"third",
|
||||
]
|
||||
).another_method() as cmd:
|
||||
pass
|
||||
|
||||
|
||||
async def func():
|
||||
async with make_context_manager1() as cm1, make_context_manager2() as cm2, make_context_manager3() as cm3, make_context_manager4() as cm4:
|
||||
pass
|
||||
|
||||
async with some_function(
|
||||
argument1, argument2, argument3="some_value"
|
||||
) as some_cm, some_other_function(argument1, argument2, argument3="some_value"):
|
||||
pass
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
with (
|
||||
make_context_manager1() as cm1,
|
||||
make_context_manager2() as cm2,
|
||||
make_context_manager3() as cm3,
|
||||
make_context_manager4() as cm4,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
# Leading comment
|
||||
with (
|
||||
make_context_manager1() as cm1,
|
||||
make_context_manager2(),
|
||||
make_context_manager3() as cm3,
|
||||
make_context_manager4(),
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
with new_new_new1() as cm1, new_new_new2():
|
||||
pass
|
||||
|
||||
|
||||
with new_new_new1() as cm1, new_new_new2():
|
||||
pass
|
||||
|
||||
|
||||
# Leading comment.
|
||||
with (
|
||||
# First comment.
|
||||
new_new_new1() as cm1,
|
||||
# Second comment.
|
||||
new_new_new2(),
|
||||
# Last comment.
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
with (
|
||||
this_is_a_very_long_call(
|
||||
looong_arg1=looong_value1, looong_arg2=looong_value2
|
||||
) as cm1,
|
||||
this_is_a_very_long_call(
|
||||
looong_arg1=looong_value1,
|
||||
looong_arg2=looong_value2,
|
||||
looong_arg3=looong_value3,
|
||||
looong_arg4=looong_value4,
|
||||
) as cm2,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
with (
|
||||
mock.patch.object(self.my_runner, "first_method", autospec=True) as mock_run_adb,
|
||||
mock.patch.object(
|
||||
self.my_runner, "second_method", autospec=True, return_value="foo"
|
||||
),
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
with xxxxxxxx.some_kind_of_method(
|
||||
some_argument=[
|
||||
"first",
|
||||
"second",
|
||||
"third",
|
||||
]
|
||||
).another_method() as cmd:
|
||||
pass
|
||||
|
||||
|
||||
async def func():
|
||||
async with (
|
||||
make_context_manager1() as cm1,
|
||||
make_context_manager2() as cm2,
|
||||
make_context_manager3() as cm3,
|
||||
make_context_manager4() as cm4,
|
||||
):
|
||||
pass
|
||||
|
||||
async with (
|
||||
some_function(argument1, argument2, argument3="some_value") as some_cm,
|
||||
some_other_function(argument1, argument2, argument3="some_value"),
|
||||
):
|
||||
pass
|
||||
```
|
||||
|
||||
|
|
@ -1,79 +0,0 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/preview_context_managers_autodetect_310.py
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
# This file uses pattern matching introduced in Python 3.10.
|
||||
|
||||
|
||||
match http_code:
|
||||
case 404:
|
||||
print("Not found")
|
||||
|
||||
|
||||
with \
|
||||
make_context_manager1() as cm1, \
|
||||
make_context_manager2() as cm2, \
|
||||
make_context_manager3() as cm3, \
|
||||
make_context_manager4() as cm4 \
|
||||
:
|
||||
pass
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -6,10 +6,5 @@
|
||||
print("Not found")
|
||||
|
||||
|
||||
-with (
|
||||
- make_context_manager1() as cm1,
|
||||
- make_context_manager2() as cm2,
|
||||
- make_context_manager3() as cm3,
|
||||
- make_context_manager4() as cm4,
|
||||
-):
|
||||
+with make_context_manager1() as cm1, make_context_manager2() as cm2, make_context_manager3() as cm3, make_context_manager4() as cm4:
|
||||
pass
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
# This file uses pattern matching introduced in Python 3.10.
|
||||
|
||||
|
||||
match http_code:
|
||||
case 404:
|
||||
print("Not found")
|
||||
|
||||
|
||||
with make_context_manager1() as cm1, make_context_manager2() as cm2, make_context_manager3() as cm3, make_context_manager4() as cm4:
|
||||
pass
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
# This file uses pattern matching introduced in Python 3.10.
|
||||
|
||||
|
||||
match http_code:
|
||||
case 404:
|
||||
print("Not found")
|
||||
|
||||
|
||||
with (
|
||||
make_context_manager1() as cm1,
|
||||
make_context_manager2() as cm2,
|
||||
make_context_manager3() as cm3,
|
||||
make_context_manager4() as cm4,
|
||||
):
|
||||
pass
|
||||
```
|
||||
|
||||
|
|
@ -1,82 +0,0 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/preview_context_managers_autodetect_311.py
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
# This file uses except* clause in Python 3.11.
|
||||
|
||||
|
||||
try:
|
||||
some_call()
|
||||
except* Error as e:
|
||||
pass
|
||||
|
||||
|
||||
with \
|
||||
make_context_manager1() as cm1, \
|
||||
make_context_manager2() as cm2, \
|
||||
make_context_manager3() as cm3, \
|
||||
make_context_manager4() as cm4 \
|
||||
:
|
||||
pass
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -7,10 +7,5 @@
|
||||
pass
|
||||
|
||||
|
||||
-with (
|
||||
- make_context_manager1() as cm1,
|
||||
- make_context_manager2() as cm2,
|
||||
- make_context_manager3() as cm3,
|
||||
- make_context_manager4() as cm4,
|
||||
-):
|
||||
+with make_context_manager1() as cm1, make_context_manager2() as cm2, make_context_manager3() as cm3, make_context_manager4() as cm4:
|
||||
pass
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
# This file uses except* clause in Python 3.11.
|
||||
|
||||
|
||||
try:
|
||||
some_call()
|
||||
except* Error as e:
|
||||
pass
|
||||
|
||||
|
||||
with make_context_manager1() as cm1, make_context_manager2() as cm2, make_context_manager3() as cm3, make_context_manager4() as cm4:
|
||||
pass
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
# This file uses except* clause in Python 3.11.
|
||||
|
||||
|
||||
try:
|
||||
some_call()
|
||||
except* Error as e:
|
||||
pass
|
||||
|
||||
|
||||
with (
|
||||
make_context_manager1() as cm1,
|
||||
make_context_manager2() as cm2,
|
||||
make_context_manager3() as cm3,
|
||||
make_context_manager4() as cm4,
|
||||
):
|
||||
pass
|
||||
```
|
||||
|
||||
|
|
@ -1,81 +0,0 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/preview_context_managers_autodetect_39.py
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
# This file uses parenthesized context managers introduced in Python 3.9.
|
||||
|
||||
|
||||
with \
|
||||
make_context_manager1() as cm1, \
|
||||
make_context_manager2() as cm2, \
|
||||
make_context_manager3() as cm3, \
|
||||
make_context_manager4() as cm4 \
|
||||
:
|
||||
pass
|
||||
|
||||
|
||||
with (
|
||||
new_new_new1() as cm1,
|
||||
new_new_new2()
|
||||
):
|
||||
pass
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,12 +1,7 @@
|
||||
# This file uses parenthesized context managers introduced in Python 3.9.
|
||||
|
||||
|
||||
-with (
|
||||
- make_context_manager1() as cm1,
|
||||
- make_context_manager2() as cm2,
|
||||
- make_context_manager3() as cm3,
|
||||
- make_context_manager4() as cm4,
|
||||
-):
|
||||
+with make_context_manager1() as cm1, make_context_manager2() as cm2, make_context_manager3() as cm3, make_context_manager4() as cm4:
|
||||
pass
|
||||
|
||||
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
# This file uses parenthesized context managers introduced in Python 3.9.
|
||||
|
||||
|
||||
with make_context_manager1() as cm1, make_context_manager2() as cm2, make_context_manager3() as cm3, make_context_manager4() as cm4:
|
||||
pass
|
||||
|
||||
|
||||
with new_new_new1() as cm1, new_new_new2():
|
||||
pass
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
# This file uses parenthesized context managers introduced in Python 3.9.
|
||||
|
||||
|
||||
with (
|
||||
make_context_manager1() as cm1,
|
||||
make_context_manager2() as cm2,
|
||||
make_context_manager3() as cm3,
|
||||
make_context_manager4() as cm4,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
with new_new_new1() as cm1, new_new_new2():
|
||||
pass
|
||||
```
|
||||
|
||||
|
|
@ -315,7 +315,21 @@ with Child(aaaaaaaaa, bbbbbbbbbbbbbbb, cccccc), Document(aaaaa, bbbbbbbbbb, dddd
|
|||
pass
|
||||
```
|
||||
|
||||
## Output
|
||||
## Outputs
|
||||
### Output 1
|
||||
```
|
||||
indent-style = space
|
||||
line-width = 88
|
||||
indent-width = 4
|
||||
quote-style = Double
|
||||
line-ending = LineFeed
|
||||
magic-trailing-comma = Respect
|
||||
docstring-code = Disabled
|
||||
docstring-code-line-width = "dynamic"
|
||||
preview = Disabled
|
||||
target_version = Py38
|
||||
```
|
||||
|
||||
```python
|
||||
with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
|
||||
pass
|
||||
|
@ -653,4 +667,414 @@ with Child(aaaaaaaaa, bbbbbbbbbbbbbbb, cccccc), Document(
|
|||
```
|
||||
|
||||
|
||||
#### Preview changes
|
||||
```diff
|
||||
--- Stable
|
||||
+++ Preview
|
||||
@@ -295,8 +295,9 @@
|
||||
pass
|
||||
|
||||
with (
|
||||
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
- + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b,
|
||||
+ (
|
||||
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
+ ) as b,
|
||||
c as d,
|
||||
):
|
||||
pass
|
||||
```
|
||||
|
||||
|
||||
### Output 2
|
||||
```
|
||||
indent-style = space
|
||||
line-width = 88
|
||||
indent-width = 4
|
||||
quote-style = Double
|
||||
line-ending = LineFeed
|
||||
magic-trailing-comma = Respect
|
||||
docstring-code = Disabled
|
||||
docstring-code-line-width = "dynamic"
|
||||
preview = Enabled
|
||||
target_version = Py39
|
||||
```
|
||||
|
||||
```python
|
||||
with (
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
):
|
||||
pass
|
||||
# trailing
|
||||
|
||||
with a, a: # after colon
|
||||
pass
|
||||
# trailing
|
||||
|
||||
with (
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
):
|
||||
pass
|
||||
# trailing
|
||||
|
||||
|
||||
with (
|
||||
a, # a # comma
|
||||
b, # c
|
||||
): # colon
|
||||
pass
|
||||
|
||||
|
||||
with (
|
||||
a as ( # a # as
|
||||
# own line
|
||||
b
|
||||
), # b # comma
|
||||
c, # c
|
||||
): # colon
|
||||
pass # body
|
||||
# body trailing own
|
||||
|
||||
with (
|
||||
a as ( # a # as
|
||||
# own line
|
||||
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
) # b
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
with (
|
||||
a,
|
||||
): # magic trailing comma
|
||||
pass
|
||||
|
||||
|
||||
with a: # should remove brackets
|
||||
pass
|
||||
|
||||
with (
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as c
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
# currently unparsable by black: https://github.com/psf/black/issues/3678
|
||||
with (name_2 for name_0 in name_4):
|
||||
pass
|
||||
with (a, *b):
|
||||
pass
|
||||
|
||||
with (
|
||||
# leading comment
|
||||
a
|
||||
) as b:
|
||||
pass
|
||||
|
||||
with (
|
||||
# leading comment
|
||||
a as b
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
a as b
|
||||
# trailing comment
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
a as (
|
||||
# leading comment
|
||||
b
|
||||
)
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
a as (
|
||||
b
|
||||
# trailing comment
|
||||
)
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
a as b # trailing same line comment
|
||||
# trailing own line comment
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
a # trailing same line comment
|
||||
# trailing own line comment
|
||||
) as b:
|
||||
pass
|
||||
|
||||
with (
|
||||
(
|
||||
a
|
||||
# trailing own line comment
|
||||
) as ( # trailing as same line comment
|
||||
b
|
||||
) # trailing b same line comment
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
# comment
|
||||
a
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
a # comment
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
a
|
||||
# comment
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
# comment
|
||||
a as b
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
a as b # comment
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
a as b
|
||||
# comment
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
[
|
||||
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
"bbbbbbbbbb",
|
||||
"cccccccccccccccccccccccccccccccccccccccccc",
|
||||
dddddddddddddddddddddddddddddddd,
|
||||
] as example1,
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
+ cccccccccccccccccccccccccccc
|
||||
+ ddddddddddddddddd as example2,
|
||||
CtxManager2() as example2,
|
||||
CtxManager2() as example2,
|
||||
CtxManager2() as example2,
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
[
|
||||
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
"bbbbbbbbbb",
|
||||
"cccccccccccccccccccccccccccccccccccccccccc",
|
||||
dddddddddddddddddddddddddddddddd,
|
||||
] as example1,
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
* bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
* cccccccccccccccccccccccccccc
|
||||
+ ddddddddddddddddd as example2,
|
||||
CtxManager222222222222222() as example2,
|
||||
):
|
||||
pass
|
||||
|
||||
# Comments on open parentheses
|
||||
with ( # comment
|
||||
CtxManager1() as example1,
|
||||
CtxManager2() as example2,
|
||||
CtxManager3() as example3,
|
||||
):
|
||||
pass
|
||||
|
||||
with ( # outer comment
|
||||
( # inner comment
|
||||
CtxManager1()
|
||||
) as example1,
|
||||
CtxManager2() as example2,
|
||||
CtxManager3() as example3,
|
||||
):
|
||||
pass
|
||||
|
||||
with ( # outer comment
|
||||
CtxManager()
|
||||
) as example:
|
||||
pass
|
||||
|
||||
with (
|
||||
( # outer comment
|
||||
CtxManager()
|
||||
) as example,
|
||||
( # inner comment
|
||||
CtxManager2()
|
||||
) as example2,
|
||||
):
|
||||
pass
|
||||
|
||||
with ( # outer comment
|
||||
CtxManager1(),
|
||||
CtxManager2(),
|
||||
) as example:
|
||||
pass
|
||||
|
||||
with ( # outer comment
|
||||
( # inner comment
|
||||
CtxManager1()
|
||||
),
|
||||
CtxManager2(),
|
||||
) as example:
|
||||
pass
|
||||
|
||||
# Breaking of with items.
|
||||
with (
|
||||
test as ( # bar # foo
|
||||
# test
|
||||
foo
|
||||
)
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
test as (
|
||||
# test
|
||||
foo
|
||||
)
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
test as ( # bar # foo # baz
|
||||
# test
|
||||
foo
|
||||
)
|
||||
):
|
||||
pass
|
||||
|
||||
with a as b, c as d:
|
||||
pass
|
||||
|
||||
with (
|
||||
a as b,
|
||||
# foo
|
||||
c as d,
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
a as ( # foo
|
||||
b
|
||||
)
|
||||
):
|
||||
pass
|
||||
|
||||
with f(
|
||||
a,
|
||||
) as b:
|
||||
pass
|
||||
|
||||
with (x := 1) as d:
|
||||
pass
|
||||
|
||||
with x[
|
||||
1,
|
||||
2,
|
||||
] as d:
|
||||
pass
|
||||
|
||||
with (
|
||||
f(
|
||||
a,
|
||||
) as b,
|
||||
c as d,
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
f(
|
||||
a,
|
||||
) as b,
|
||||
c as d,
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
) as b:
|
||||
pass
|
||||
|
||||
with (
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
(
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
) as b,
|
||||
c as d,
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b,
|
||||
c as d,
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
(
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||
) as b,
|
||||
c as d,
|
||||
):
|
||||
pass
|
||||
|
||||
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
|
||||
|
||||
|
||||
with (
|
||||
Child(aaaaaaaaa, bbbbbbbbbbbbbbb, cccccc),
|
||||
Document(aaaaa, bbbbbbbbbb, ddddddddddddd),
|
||||
):
|
||||
pass
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,222 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with_39.py
|
||||
---
|
||||
## Input
|
||||
```python
|
||||
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
|
||||
|
||||
|
||||
```
|
||||
|
||||
## Outputs
|
||||
### Output 1
|
||||
```
|
||||
indent-style = space
|
||||
line-width = 88
|
||||
indent-width = 4
|
||||
quote-style = Double
|
||||
line-ending = LineFeed
|
||||
magic-trailing-comma = Respect
|
||||
docstring-code = Disabled
|
||||
docstring-code-line-width = "dynamic"
|
||||
preview = Enabled
|
||||
target_version = Py39
|
||||
```
|
||||
|
||||
```python
|
||||
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
|
||||
```
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue