Preserve comments on non-defaulted arguments (#3264)

This commit is contained in:
Charlie Marsh 2023-02-27 18:41:40 -05:00 committed by GitHub
parent 16be691712
commit 470e1c1754
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 87 additions and 189 deletions

View file

@ -178,43 +178,9 @@ instruction()#comment with bad spacing
```diff
--- Black
+++ Ruff
@@ -13,7 +13,7 @@
"Callable",
"ClassVar",
# ABCs (from collections.abc).
- "AbstractSet", # collections.abc.Set.
+ "AbstractSet",
"ByteString",
"Container",
# Concrete collection types.
@@ -24,7 +24,7 @@
"List",
"Set",
"FrozenSet",
- "NamedTuple", # Not really a type.
+ "NamedTuple",
"Generator",
]
@@ -60,26 +60,32 @@
# Comment before function.
def inline_comments_in_brackets_ruin_everything():
if typedargslist:
- parameters.children = [children[0], body, children[-1]] # (1 # )1
+ parameters.children = [children[0], body, children[-1]]
parameters.children = [
children[0],
@@ -72,14 +72,20 @@
body,
- children[-1], # type: ignore
+ children[-1],
]
else:
parameters.children = [
- parameters.children[0], # (2 what if this was actually long
+ parameters.children[0],
body,
- parameters.children[-1], # )2
+ parameters.children[-1],
parameters.children[-1], # )2
]
- parameters.children = [parameters.what_if_this_was_actually_long.children[0], body, parameters.children[-1]] # type: ignore
+ parameters.children = [
@ -319,7 +285,7 @@ __all__ = [
"Callable",
"ClassVar",
# ABCs (from collections.abc).
"AbstractSet",
"AbstractSet", # collections.abc.Set.
"ByteString",
"Container",
# Concrete collection types.
@ -330,7 +296,7 @@ __all__ = [
"List",
"Set",
"FrozenSet",
"NamedTuple",
"NamedTuple", # Not really a type.
"Generator",
]
@ -366,17 +332,17 @@ else:
# Comment before function.
def inline_comments_in_brackets_ruin_everything():
if typedargslist:
parameters.children = [children[0], body, children[-1]]
parameters.children = [children[0], body, children[-1]] # (1 # )1
parameters.children = [
children[0],
body,
children[-1],
children[-1], # type: ignore
]
else:
parameters.children = [
parameters.children[0],
parameters.children[0], # (2 what if this was actually long
body,
parameters.children[-1],
parameters.children[-1], # )2
]
parameters.children = [
parameters.what_if_this_was_actually_long.children[0],

View file

@ -131,68 +131,18 @@ aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*ite
```diff
--- Black
+++ Ruff
@@ -2,7 +2,7 @@
@@ -31,8 +31,8 @@
def f(
- a, # type: int
+ a,
):
pass
@@ -14,44 +14,42 @@
def f(
- a, # type: int
- b, # type: int
- c, # type: int
- d, # type: int
- e, # type: int
- f, # type: int
- g, # type: int
- h, # type: int
- i, # type: int
+ a,
+ b,
+ c,
+ d,
+ e,
+ f,
+ g,
+ h,
+ i,
):
# type: (...) -> None
pass
def f(
- arg, # type: int
- *args, # type: *Any
arg, # type: int
*args, # type: *Any
- default=False, # type: bool
- **kwargs, # type: **Any
+ arg,
+ *args,
+ default=False,
+ default=False, # type: bool # type: **Any
+ **kwargs,
):
# type: (...) -> None
pass
def f(
- a, # type: int
- b, # type: int
- c, # type: int
- d, # type: int
+ a,
+ b,
+ c,
+ d,
):
# type: (...) -> None
@@ -49,9 +49,7 @@
element = 0 # type: int
another_element = 1 # type: float
another_element_with_long_name = 2 # type: int
@ -203,38 +153,7 @@ aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*ite
an_element_with_a_long_value = calls() or more_calls() and more() # type: bool
tup = (
@@ -70,21 +68,21 @@
def f(
- x, # not a type comment
- y, # type: int
+ x,
+ y,
):
# type: (...) -> None
pass
def f(
- x, # not a type comment
+ x,
): # type: (int) -> None
pass
def func(
- a=some_list[0], # type: int
+ a=some_list[0],
): # type: () -> int
c = call(
0.0123,
@@ -96,23 +94,37 @@
0.0123,
0.0456,
0.0789,
- a[-1], # type: ignore
+ a[-1],
@@ -100,19 +98,33 @@
)
c = call(
@ -245,7 +164,7 @@ aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*ite
+ "aaaaaaaa",
+ "aaaaaaaa",
+ "aaaaaaaa",
+ "aaaaaaaa",
+ "aaaaaaaa", # type: ignore
)
@ -270,7 +189,7 @@ aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*ite
+ AAAAAAAAAAAAAAAAAAAAAAA,
+ AAAAAAAAAAAAAAAAAAAAAAA,
+ BBBBBBBBBBBB,
+ ],
+ ], # type: ignore
)
aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # type: ignore[arg-type]
@ -283,7 +202,7 @@ from typing import Any, Tuple
def f(
a,
a, # type: int
):
pass
@ -295,24 +214,24 @@ def f(a, b, c, d, e, f, g, h, i):
def f(
a,
b,
c,
d,
e,
f,
g,
h,
i,
a, # type: int
b, # type: int
c, # type: int
d, # type: int
e, # type: int
f, # type: int
g, # type: int
h, # type: int
i, # type: int
):
# type: (...) -> None
pass
def f(
arg,
*args,
default=False,
arg, # type: int
*args, # type: *Any
default=False, # type: bool # type: **Any
**kwargs,
):
# type: (...) -> None
@ -320,10 +239,10 @@ def f(
def f(
a,
b,
c,
d,
a, # type: int
b, # type: int
c, # type: int
d, # type: int
):
# type: (...) -> None
@ -349,21 +268,21 @@ def f(
def f(
x,
y,
x, # not a type comment
y, # type: int
):
# type: (...) -> None
pass
def f(
x,
x, # not a type comment
): # type: (int) -> None
pass
def func(
a=some_list[0],
a=some_list[0], # type: int
): # type: () -> int
c = call(
0.0123,
@ -375,7 +294,7 @@ def func(
0.0123,
0.0456,
0.0789,
a[-1],
a[-1], # type: ignore
)
c = call(
@ -385,7 +304,7 @@ def func(
"aaaaaaaa",
"aaaaaaaa",
"aaaaaaaa",
"aaaaaaaa",
"aaaaaaaa", # type: ignore
)
@ -405,7 +324,7 @@ call_to_some_function_asdf(
AAAAAAAAAAAAAAAAAAAAAAA,
AAAAAAAAAAAAAAAAAAAAAAA,
BBBBBBBBBBBB,
],
], # type: ignore
)
aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # type: ignore[arg-type]

View file

@ -106,7 +106,7 @@ elif unformatted:
- # fmt: on
- ] # Includes an formatted indentation.
+ "foo-bar" "=foo.bar.:main",
+ ],
+ ], # Includes an formatted indentation.
},
)
@ -200,7 +200,7 @@ setup(
# fmt: off
"console_scripts": [
"foo-bar" "=foo.bar.:main",
],
], # Includes an formatted indentation.
},
)

View file

@ -136,9 +136,8 @@ def foo() -> tuple[int, int, int,]:
# Don't lose the comments
-def double(a: int) -> int: # Hello
def double(a: int) -> int: # Hello
- return 2 * a
+def double(a: int) -> int:
+ return 2
+ * a
@ -276,7 +275,7 @@ def double(a: int) -> int:
# Don't lose the comments
def double(a: int) -> int:
def double(a: int) -> int: # Hello
return 2
* a