Format function argument separator comments (#5211)

## Summary

This is a complete rewrite of the handling of `/` and `*` comment
handling in function signatures. The key problem is that slash and star
don't have a note. We now parse out the positions of slash and star and
their respective preceding and following note. I've left code comments
for each possible case of function signature structure and comment
placement

## Test Plan

I extended the function statement fixtures with cases that i found. If
you have more weird edge cases your input would be appreciated.
This commit is contained in:
konstin 2023-06-21 19:56:47 +02:00 committed by GitHub
parent bc63cc9b3c
commit d7c7484618
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 847 additions and 86 deletions

View file

@ -103,6 +103,140 @@ def foo(
b=3 + 2 # comment
):
...
# Comments on the slash or the star, both of which don't have a node
def f11(
a,
# positional only comment, leading
/, # positional only comment, trailing
b,
):
pass
def f12(
a=1,
# positional only comment, leading
/, # positional only comment, trailing
b=2,
):
pass
def f13(
a,
# positional only comment, leading
/, # positional only comment, trailing
):
pass
def f21(
a=1,
# keyword only comment, leading
*, # keyword only comment, trailing
b=2,
):
pass
def f22(
a,
# keyword only comment, leading
*, # keyword only comment, trailing
b,
):
pass
def f23(
a,
# keyword only comment, leading
*args, # keyword only comment, trailing
b,
):
pass
def f24(
# keyword only comment, leading
*, # keyword only comment, trailing
a
):
pass
def f31(
a=1,
# positional only comment, leading
/, # positional only comment, trailing
b=2,
# keyword only comment, leading
*, # keyword only comment, trailing
c=3,
):
pass
def f32(
a,
# positional only comment, leading
/, # positional only comment, trailing
b,
# keyword only comment, leading
*, # keyword only comment, trailing
c,
):
pass
def f33(
a,
# positional only comment, leading
/, # positional only comment, trailing
# keyword only comment, leading
*args, # keyword only comment, trailing
c,
):
pass
def f34(
a,
# positional only comment, leading
/, # positional only comment, trailing
# keyword only comment, leading
*, # keyword only comment, trailing
c,
):
pass
def f35(
# keyword only comment, leading
*, # keyword only comment, trailing
c,
):
pass
# Multiple trailing comments
def f41(
a,
/ # 1
, # 2
# 3
* # 4
, # 5
c,
):
pass
# Multiple trailing comments strangely places. The goal here is only stable formatting,
# the comments are placed to strangely to keep their relative position intact
def f42(
a,
/ # 1
# 2
, # 3
# 4
* # 5
# 6
, # 7
c,
):
pass
```
@ -237,6 +371,148 @@ def foo(
b=3 + 2, # comment
):
...
# Comments on the slash or the star, both of which don't have a node
def f11(
a,
# positional only comment, leading
/, # positional only comment, trailing
b,
):
pass
def f12(
a=1,
# positional only comment, leading
/, # positional only comment, trailing
b=2,
):
pass
def f13(
a,
# positional only comment, leading
/, # positional only comment, trailing
):
pass
def f21(
a=1,
# keyword only comment, leading
*, # keyword only comment, trailing
b=2,
):
pass
def f22(
a,
# keyword only comment, leading
*, # keyword only comment, trailing
b,
):
pass
def f23(
a,
# keyword only comment, leading
*args, # keyword only comment, trailing
b,
):
pass
def f24(
# keyword only comment, leading
*, # keyword only comment, trailing
a,
):
pass
def f31(
a=1,
# positional only comment, leading
/, # positional only comment, trailing
b=2,
# keyword only comment, leading
*, # keyword only comment, trailing
c=3,
):
pass
def f32(
a,
# positional only comment, leading
/, # positional only comment, trailing
b,
# keyword only comment, leading
*, # keyword only comment, trailing
c,
):
pass
def f33(
a,
# positional only comment, leading
/, # positional only comment, trailing
# keyword only comment, leading
*args, # keyword only comment, trailing
c,
):
pass
def f34(
a,
# positional only comment, leading
/, # positional only comment, trailing
# keyword only comment, leading
*, # keyword only comment, trailing
c,
):
pass
def f35(
# keyword only comment, leading
*, # keyword only comment, trailing
c,
):
pass
# Multiple trailing comments
def f41(
a,
/, # 1 # 2
# 3
*, # 4 # 5
c,
):
pass
# Multiple trailing comments strangely places. The goal here is only stable formatting,
# the comments are placed to strangely to keep their relative position intact
def f42(
a,
/, # 1
# 2
# 3
# 4
*, # 5 # 7
# 6
c,
):
pass
```