mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-01 12:25:10 +00:00
Handle comments on open parentheses in with statements (#6515)
## Summary
This PR adds handling for comments on open parentheses in parenthesized
context managers. For example, given:
```python
with ( # comment
CtxManager1() as example1,
CtxManager2() as example2,
CtxManager3() as example3,
):
...
```
We want to preserve that formatting. (Black does the same.) On `main`,
we format as:
```python
with (
# comment
CtxManager1() as example1,
CtxManager2() as example2,
CtxManager3() as example3,
):
...
```
It's very similar to how `StmtImportFrom` is handled.
Note that this case _isn't_ covered by the "parenthesized comment"
proposal, since this is a common on the statement that would typically
be attached to the first `WithItem`, and the `WithItem` _itself_ can
have parenthesized comments, like:
```python
with ( # comment
(
CtxManager1() # comment
) as example1,
CtxManager2() as example2,
CtxManager3() as example3,
):
...
```
## Test Plan
`cargo test`
Confirmed no change in similarity score.
This commit is contained in:
parent
3711f8ad59
commit
c3a9151eb5
7 changed files with 148 additions and 19 deletions
|
|
@ -170,8 +170,7 @@ async def h():
|
|||
)
|
||||
|
||||
|
||||
with (
|
||||
# d 1
|
||||
with ( # d 1
|
||||
x
|
||||
):
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -88,7 +88,9 @@ from a import ( # comment
|
|||
bar,
|
||||
)
|
||||
|
||||
from a import bar # comment
|
||||
from a import ( # comment
|
||||
bar
|
||||
)
|
||||
|
||||
from a import (
|
||||
bar,
|
||||
|
|
|
|||
|
|
@ -122,6 +122,24 @@ with [
|
|||
dddddddddddddddddddddddddddddddd,
|
||||
] as example1, aaaaaaaaaaaaaaaaaaaaaaaaaa * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb * cccccccccccccccccccccccccccc + ddddddddddddddddd as example2, CtxManager222222222222222() as example2:
|
||||
...
|
||||
|
||||
# Comments on open parentheses
|
||||
with ( # comment
|
||||
CtxManager1() as example1,
|
||||
CtxManager2() as example2,
|
||||
CtxManager3() as example3,
|
||||
):
|
||||
...
|
||||
|
||||
|
||||
with ( # outer comment
|
||||
( # inner comment
|
||||
CtxManager1()
|
||||
) as example1,
|
||||
CtxManager2() as example2,
|
||||
CtxManager3() as example3,
|
||||
):
|
||||
...
|
||||
```
|
||||
|
||||
## Output
|
||||
|
|
@ -250,6 +268,25 @@ with [
|
|||
dddddddddddddddddddddddddddddddd,
|
||||
] as example1, aaaaaaaaaaaaaaaaaaaaaaaaaa * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb * cccccccccccccccccccccccccccc + ddddddddddddddddd as example2, CtxManager222222222222222() as example2:
|
||||
...
|
||||
|
||||
# Comments on open parentheses
|
||||
with ( # comment
|
||||
CtxManager1() as example1,
|
||||
CtxManager2() as example2,
|
||||
CtxManager3() as example3,
|
||||
):
|
||||
...
|
||||
|
||||
|
||||
with ( # outer comment
|
||||
(
|
||||
# inner comment
|
||||
CtxManager1()
|
||||
) as example1,
|
||||
CtxManager2() as example2,
|
||||
CtxManager3() as example3,
|
||||
):
|
||||
...
|
||||
```
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue