mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:56 +00:00
Add formatter support for call and class definition Arguments
(#6274)
## Summary This PR leverages the `Arguments` AST node introduced in #6259 in the formatter, which ensures that we correctly handle trailing comments in calls, like: ```python f( 1, # comment ) pass ``` (Previously, this was treated as a leading comment on `pass`.) This also allows us to unify the argument handling across calls and class definitions. ## Test Plan A bunch of new fixture tests, plus improved Black compatibility.
This commit is contained in:
parent
b095b7204b
commit
4c53bfe896
19 changed files with 640 additions and 252 deletions
|
@ -92,6 +92,31 @@ f(
|
|||
f(
|
||||
a.very_long_function_function_that_is_so_long_that_it_expands_the_parent_but_its_only_a_single_argument()
|
||||
)
|
||||
|
||||
f( # abc
|
||||
)
|
||||
|
||||
f( # abc
|
||||
# abc
|
||||
)
|
||||
|
||||
f(
|
||||
# abc
|
||||
)
|
||||
|
||||
f ( # abc
|
||||
1
|
||||
)
|
||||
|
||||
f (
|
||||
# abc
|
||||
1
|
||||
)
|
||||
|
||||
f (
|
||||
1
|
||||
# abc
|
||||
)
|
||||
```
|
||||
|
||||
## Output
|
||||
|
@ -137,8 +162,9 @@ f(
|
|||
these_arguments_have_values_that_need_to_break_because_they_are_too_long3=session,
|
||||
)
|
||||
|
||||
f()
|
||||
# dangling comment
|
||||
f(
|
||||
# dangling comment
|
||||
)
|
||||
|
||||
|
||||
f(only=1, short=1, arguments=1)
|
||||
|
@ -177,6 +203,28 @@ f(
|
|||
f(
|
||||
a.very_long_function_function_that_is_so_long_that_it_expands_the_parent_but_its_only_a_single_argument()
|
||||
)
|
||||
|
||||
f() # abc
|
||||
|
||||
f( # abc
|
||||
# abc
|
||||
)
|
||||
|
||||
f(
|
||||
# abc
|
||||
)
|
||||
|
||||
f(1) # abc
|
||||
|
||||
f(
|
||||
# abc
|
||||
1
|
||||
)
|
||||
|
||||
f(
|
||||
1
|
||||
# abc
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
|
|
|
@ -130,8 +130,7 @@ a = {
|
|||
3: True,
|
||||
}
|
||||
|
||||
x = { # dangling end of line comment
|
||||
}
|
||||
x = {} # dangling end of line comment
|
||||
```
|
||||
|
||||
|
||||
|
|
|
@ -149,8 +149,7 @@ a = (
|
|||
# Regression test: lambda empty arguments ranges were too long, leading to unstable
|
||||
# formatting
|
||||
(
|
||||
lambda: ( #
|
||||
),
|
||||
lambda: (), #
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -33,8 +33,7 @@ b3 = [
|
|||
```py
|
||||
# Dangling comment placement in empty lists
|
||||
# Regression test for https://github.com/python/cpython/blob/03160630319ca26dcbbad65225da4248e54c45ec/Tools/c-analyzer/c_analyzer/datafiles.py#L14-L16
|
||||
a1 = [ # a
|
||||
]
|
||||
a1 = [] # a
|
||||
a2 = [ # a
|
||||
# b
|
||||
]
|
||||
|
|
|
@ -98,6 +98,58 @@ class Test:
|
|||
"""Docstring"""
|
||||
# comment
|
||||
x = 1
|
||||
|
||||
|
||||
class C(): # comment
|
||||
pass
|
||||
|
||||
|
||||
class C( # comment
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class C(
|
||||
# comment
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class C(): # comment
|
||||
pass
|
||||
|
||||
|
||||
class C( # comment
|
||||
# comment
|
||||
1
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class C(
|
||||
1
|
||||
# comment
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
# Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
|
||||
class AltCLIPOutput(ModelOutput):
|
||||
...
|
||||
|
||||
|
||||
@dataclass
|
||||
class AltCLIPOutput( # Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
|
||||
):
|
||||
...
|
||||
|
||||
|
||||
@dataclass
|
||||
class AltCLIPOutput(
|
||||
# Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
|
||||
):
|
||||
...
|
||||
```
|
||||
|
||||
## Output
|
||||
|
@ -116,8 +168,7 @@ class Test((Aaaaaaaaaaaaaaaaa), Bbbbbbbbbbbbbbbb, metaclass=meta):
|
|||
pass
|
||||
|
||||
|
||||
class Test(
|
||||
# trailing class comment
|
||||
class Test( # trailing class comment
|
||||
Aaaaaaaaaaaaaaaaa, # trailing comment
|
||||
# in between comment
|
||||
Bbbbbbbbbbbbbbbb,
|
||||
|
@ -217,6 +268,56 @@ class Test:
|
|||
|
||||
# comment
|
||||
x = 1
|
||||
|
||||
|
||||
class C: # comment
|
||||
pass
|
||||
|
||||
|
||||
class C: # comment
|
||||
pass
|
||||
|
||||
|
||||
class C(
|
||||
# comment
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class C: # comment
|
||||
pass
|
||||
|
||||
|
||||
class C( # comment
|
||||
# comment
|
||||
1
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class C(
|
||||
1
|
||||
# comment
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
# Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
|
||||
class AltCLIPOutput(ModelOutput):
|
||||
...
|
||||
|
||||
|
||||
@dataclass
|
||||
class AltCLIPOutput: # Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
|
||||
...
|
||||
|
||||
|
||||
@dataclass
|
||||
class AltCLIPOutput(
|
||||
# Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
|
||||
):
|
||||
...
|
||||
```
|
||||
|
||||
|
||||
|
|
|
@ -215,8 +215,7 @@ del (
|
|||
) # Completed
|
||||
# Done
|
||||
|
||||
del ( # dangling end of line comment
|
||||
)
|
||||
del () # dangling end of line comment
|
||||
```
|
||||
|
||||
|
||||
|
|
|
@ -192,8 +192,7 @@ raise aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfk < (
|
|||
) # the other end
|
||||
# sneaky comment
|
||||
|
||||
raise ( # another comment
|
||||
)
|
||||
raise () # another comment
|
||||
|
||||
raise () # what now
|
||||
|
||||
|
@ -201,8 +200,9 @@ raise ( # sould I stay here
|
|||
# just a comment here
|
||||
) # trailing comment
|
||||
|
||||
raise hello() # sould I stay here
|
||||
# just a comment here # trailing comment
|
||||
raise hello( # sould I stay here
|
||||
# just a comment here
|
||||
) # trailing comment
|
||||
|
||||
raise (
|
||||
# sould I stay here
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue