Add general support for parenthesized comments on expressions (#6485)

## Summary

This PR adds support for parenthesized comments. A parenthesized comment
is a comment that appears within a parenthesis, but not within the range
of the expression enclosed by the parenthesis. For example, the comment
here is a parenthesized comment:

```python
if (
    # comment
    True
):
    ...
```

The parentheses enclose the `True`, but the range of `True` doesn’t
include the `# comment`.

There are at least two problems associated with parenthesized comments:
(1) associating the comment with the correct (i.e., enclosed) node; and
(2) formatting the comment correctly, once it has been associated with
the enclosed node.

The solution proposed here for (1) is to search for parentheses between
preceding and following node, and use open and close parentheses to
break ties, rather than always assigning to the preceding node.

For (2), we handle these special parenthesized comments in `FormatExpr`.
The biggest risk with this approach is that we forget some codepath that
force-disables parenthesization (by passing in `Parentheses::Never`).
I've audited all usages of that enum and added additional handling +
test coverage for such cases.

Closes https://github.com/astral-sh/ruff/issues/6390.

## Test Plan

`cargo test` with new cases.

Before:

| project      | similarity index |
|--------------|------------------|
| build        | 0.75623          |
| cpython      | 0.75472          |
| django       | 0.99804          |
| transformers | 0.99618          |
| typeshed     | 0.74233          |
| warehouse    | 0.99601          |
| zulip        | 0.99727          |

After:

| project      | similarity index |
|--------------|------------------|
| build        | 0.75623          |
| cpython      | 0.75472          |
| django       | 0.99804          |
| transformers | 0.99618          |
| typeshed     | 0.74237          |
| warehouse    | 0.99601          |
| zulip        | 0.99727          |
This commit is contained in:
Charlie Marsh 2023-08-15 14:59:18 -04:00 committed by GitHub
parent 29c0b9f91c
commit a3d4f08f29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 806 additions and 236 deletions

View file

@ -116,8 +116,51 @@ threshold_date = datetime.datetime.now() - datetime.timedelta( # comment
days=threshold_days_threshold_days
)
f(
( # comment
# Parenthesized and opening-parenthesis comments
func(
(x for x in y)
)
func( # outer comment
(x for x in y)
)
func(
( # inner comment
x for x in y
)
)
func(
(
# inner comment
x for x in y
)
)
func( # outer comment
( # inner comment
1
)
)
func(
# outer comment
( # inner comment
x for x in y
)
)
func(
( # inner comment
[]
)
)
func(
# outer comment
( # inner comment
[]
)
)

View file

@ -13,3 +13,13 @@ call(
[What, i, this, s, very, long, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]
) # trailing value comment
)
call(
x,
# Leading starred comment
* # Trailing star comment
[
# Leading value comment
[What, i, this, s, very, long, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]
] # trailing value comment
)

View file

@ -73,7 +73,49 @@ def e2() -> ( # e 2
x): pass
class E3( # e 3
def e3() -> (
# e 2
x): pass
def e4() -> (
x
# e 4
): pass
def e5() -> ( # e 5
( # e 5
x
)
): pass
def e6() -> (
(
# e 6
x
)
): pass
def e7() -> (
(
x
# e 7
)
): pass
def e8() -> (
(
x
)
# e 8
): pass
class E9( # e 9
x): pass

View file

@ -27,3 +27,30 @@ aa = ([
aaaa = ( # trailing
# comment
bbbbb) = cccccccccccccccc = 3
x = ( # comment
[ # comment
a,
b,
c,
]
) = 1
x = (
# comment
[
a,
b,
c,
]
) = 1
x = (
[ # comment
a,
b,
c,
]
) = 1

View file

@ -108,7 +108,6 @@ with (
):
...
with [
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"bbbbbbbbbb",
@ -125,7 +124,6 @@ with ( # comment
):
...
with ( # outer comment
( # inner comment
CtxManager1()
@ -134,3 +132,29 @@ with ( # outer comment
CtxManager3() as example3,
):
...
with ( # outer comment
CtxManager()
) as example:
...
with ( # outer comment
CtxManager()
) as example, ( # inner comment
CtxManager2()
) as example2:
...
with ( # outer comment
CtxManager1(),
CtxManager2(),
) as example:
...
with ( # outer comment
( # inner comment
CtxManager1()
),
CtxManager2(),
) as example:
...