mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:56 +00:00
Generalize comment-after-bracket handling to lists, sets, etc. (#6320)
## Summary We already support preserving the end-of-line comment in calls and type parameters, as in: ```python foo( # comment bar, ) ``` This PR adds the same behavior for lists, sets, comprehensions, etc., such that we preserve: ```python [ # comment 1, 2, 3, ] ``` And related cases.
This commit is contained in:
parent
d3aa8b4ee0
commit
1d8759d5df
20 changed files with 265 additions and 99 deletions
|
@ -70,8 +70,7 @@ x={ # dangling end of line comment
|
|||
## Output
|
||||
```py
|
||||
# before
|
||||
{
|
||||
# open
|
||||
{ # open
|
||||
key: value # key # colon # value
|
||||
} # close
|
||||
# after
|
||||
|
|
|
@ -92,6 +92,13 @@ selected_choices = {
|
|||
k: str(v)
|
||||
for vvvvvvvvvvvvvvvvvvvvvvv in value if str(v) not in self.choices.field.empty_values
|
||||
}
|
||||
|
||||
{
|
||||
k: v
|
||||
for ( # foo
|
||||
|
||||
x, aaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaay) in z
|
||||
}
|
||||
```
|
||||
|
||||
## Output
|
||||
|
@ -201,11 +208,9 @@ selected_choices = {
|
|||
}
|
||||
|
||||
# Leading
|
||||
{
|
||||
# Leading
|
||||
{ # Leading
|
||||
k: v # Trailing
|
||||
for a, a, a, a, a, a, a, a, a, a, (
|
||||
# Trailing
|
||||
for a, a, a, a, a, a, a, a, a, a, ( # Trailing
|
||||
a,
|
||||
a,
|
||||
a,
|
||||
|
@ -241,6 +246,11 @@ selected_choices = {
|
|||
for vvvvvvvvvvvvvvvvvvvvvvv in value
|
||||
if str(v) not in self.choices.field.empty_values
|
||||
}
|
||||
|
||||
{
|
||||
k: v
|
||||
for (x, aaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaay) in z # foo
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
|
|
@ -31,6 +31,12 @@ len(
|
|||
# trailing
|
||||
)
|
||||
)
|
||||
|
||||
len(
|
||||
# leading
|
||||
a for b in c
|
||||
# trailing
|
||||
)
|
||||
```
|
||||
|
||||
## Output
|
||||
|
@ -56,6 +62,13 @@ f((1) for _ in (a))
|
|||
|
||||
# black keeps these atm, but intends to remove them in the future:
|
||||
# https://github.com/psf/black/issues/2943
|
||||
len(
|
||||
# leading
|
||||
a
|
||||
for b in c
|
||||
# trailing
|
||||
)
|
||||
|
||||
len(
|
||||
# leading
|
||||
a
|
||||
|
|
|
@ -38,6 +38,18 @@ c1 = [ # trailing open bracket
|
|||
2, # trailing item
|
||||
# leading close bracket
|
||||
] # trailing close bracket
|
||||
|
||||
|
||||
[ # end-of-line comment
|
||||
]
|
||||
|
||||
[ # end-of-line comment
|
||||
# own-line comment
|
||||
]
|
||||
|
||||
[ # end-of-line comment
|
||||
1
|
||||
]
|
||||
```
|
||||
|
||||
## Output
|
||||
|
@ -66,14 +78,22 @@ b3 = [
|
|||
]
|
||||
|
||||
# Comment placement in non-empty lists
|
||||
c1 = [
|
||||
# trailing open bracket
|
||||
c1 = [ # trailing open bracket
|
||||
# leading item
|
||||
1,
|
||||
# between
|
||||
2, # trailing item
|
||||
# leading close bracket
|
||||
] # trailing close bracket
|
||||
|
||||
|
||||
[] # end-of-line comment
|
||||
|
||||
[ # end-of-line comment
|
||||
# own-line comment
|
||||
]
|
||||
|
||||
[1] # end-of-line comment
|
||||
```
|
||||
|
||||
|
||||
|
|
|
@ -248,14 +248,12 @@ f4 = ( # end-of-line
|
|||
) # trailing
|
||||
|
||||
# Comments in other tuples
|
||||
g1 = (
|
||||
# a
|
||||
g1 = ( # a
|
||||
# b
|
||||
1, # c
|
||||
# d
|
||||
) # e
|
||||
g2 = (
|
||||
# a
|
||||
g2 = ( # a
|
||||
# b
|
||||
1, # c
|
||||
# d
|
||||
|
|
|
@ -204,8 +204,7 @@ raise hello( # sould I stay here
|
|||
# just a comment here
|
||||
) # trailing comment
|
||||
|
||||
raise (
|
||||
# sould I stay here
|
||||
raise ( # sould I stay here
|
||||
test,
|
||||
# just a comment here
|
||||
) # trailing comment
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue