Fixup comment handling on opening parenthesis in function definition (#6381)

## Summary

I noticed some deviations in how we treat dangling comments that hug the
opening parenthesis for function definitions.

For example, given:

```python
def f(  # first
    # second
):  # third
    ...
```

We currently format as:

```python
def f(
      # first
    # second
):  # third
    ...
```

This PR adds the proper opening-parenthesis dangling comment handling
for function parameters. Specifically, as with all other parenthesized
nodes, we now detect that dangling comment in `placement.rs` and handle
it in `parameters.rs`. We have to take some care in that file, since we
have multiple "kinds" of dangling comments, but I added a bunch of test
cases that we now format identically to Black.

## Test Plan

`cargo test`

Before:

- `zulip`: 0.99388
- `django`: 0.99784
- `warehouse`: 0.99504
- `transformers`: 0.99404
- `cpython`: 0.75913
- `typeshed`: 0.74364

After:

- `zulip`: 0.99386
- `django`: 0.99784
- `warehouse`: 0.99504
- `transformers`: 0.99404
- `cpython`: 0.75913
- `typeshed`: 0.74409

Meaningful improvement on `typeshed`, minor decrease on `zulip`.
This commit is contained in:
Charlie Marsh 2023-08-07 14:04:56 -04:00 committed by GitHub
parent 3f0eea6d87
commit a637b8b3a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 294 additions and 15 deletions

View file

@ -295,3 +295,79 @@ def f(*args, b, **kwds, ): pass
def f(*, b, **kwds, ): pass
def f(a, *args, b, **kwds, ): pass
def f(a, *, b, **kwds, ): pass
# Handle comments on open parenthesis.
def f(
# first
# second
):
...
def f( # first
# second
): # third
...
def f( # first
): # second
...
def f(
a,
/,
# first
b
# second
):
...
def f( # first
*,
# second
b
# third
):
...
def f( # first
# second
*,
# third
b
# fourth
):
...
def f( # first
a,
# second
): # third
...
def f( # first
a
): # second
...
def f( # first
a
# second
): # third
...
def f( # first
a,
/ # second
,
# third
):
...