Handle keyword comments between = and value (#6883)

## Summary

This PR adds comment handling for comments between the `=` and the
`value` for keywords, as in the following cases:

```python
func(
    x  # dangling
    =  # dangling
    # dangling
    1,
    **  # dangling
    y
)
```

(Comments after the `**` were already handled in some cases, but I've
unified the handling with the `=` handling.)

Note that, previously, comments between the `**` and its value were
rendered as trailing comments on the value (so they'd appear after `y`).
This struck me as odd since it effectively re-ordered the comment with
respect to its closest AST node (the value). I've made them leading
comments, though I don't know that that's a significant improvement. I
could also imagine us leaving them where they are.
This commit is contained in:
Charlie Marsh 2023-08-30 09:52:51 -04:00 committed by GitHub
parent a3f4d7745a
commit e2b2b1759f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 211 additions and 17 deletions

View file

@ -86,6 +86,21 @@ f(
# oddly placed own line comment
dict()
)
f(
session,
b=1,
**(# oddly placed own line comment
dict()
)
)
f(
session,
b=1,
**(
# oddly placed own line comment
dict()
)
)
# Don't add a magic trailing comma when there is only one entry
# Minimized from https://github.com/django/django/blob/7eeadc82c2f7d7a778e3bb43c34d642e6275dacf/django/contrib/admin/checks.py#L674-L681
@ -211,6 +226,28 @@ aaa = (
()
.bbbbbbbbbbbbbbbb
)
# Comments around keywords
f(x= # comment
1)
f(x # comment
=
1)
f(x=
# comment
1)
f(x=(# comment
1
))
f(x=(
# comment
1
))
```
## Output
@ -288,13 +325,29 @@ f("aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaa
f(
session,
b=1,
**dict(), # oddly placed end-of-line comment
# oddly placed end-of-line comment
**dict(),
)
f(
session,
b=1,
**dict(),
# oddly placed own line comment
**dict(),
)
f(
session,
b=1,
**( # oddly placed own line comment
dict()
),
)
f(
session,
b=1,
**(
# oddly placed own line comment
dict()
),
)
# Don't add a magic trailing comma when there is only one entry
@ -408,6 +461,36 @@ aaa = (
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
# awkward comment
)().bbbbbbbbbbbbbbbb
# Comments around keywords
f(
# comment
x=1
)
f(
# comment
x=1
)
f(
# comment
x=1
)
f(
x=( # comment
1
)
)
f(
x=(
# comment
1
)
)
```

View file

@ -80,22 +80,26 @@ x={ # dangling end of line comment
{
**a, # leading
**b, # middle # trailing
# middle
**b, # trailing
}
{
**b # middle with single item
# middle with single item
**b
}
{
# before
**b, # between
# between
**b,
}
{
**a, # comment before preceding node's comma
# before
**b, # between
# between
**b,
}
{}

View file

@ -985,18 +985,18 @@ match pattern_match_class:
...
case A(
b=# b
# b
# c
2 # d
b=2 # d
# e
):
pass
case A(
# a
b=# b
# b
# c
2 # d
b=2 # d
# e
):
pass