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:
Charlie Marsh 2023-08-14 11:11:03 -04:00 committed by GitHub
parent 3711f8ad59
commit c3a9151eb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 148 additions and 19 deletions

View file

@ -170,8 +170,7 @@ async def h():
)
with (
# d 1
with ( # d 1
x
):
pass

View file

@ -88,7 +88,9 @@ from a import ( # comment
bar,
)
from a import bar # comment
from a import ( # comment
bar
)
from a import (
bar,

View file

@ -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,
):
...
```