Create dedicated Body nodes in the formatter CST (#3223)

This commit is contained in:
Charlie Marsh 2023-02-27 17:55:05 -05:00 committed by GitHub
parent cd6413ca09
commit 2261e194a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 1239 additions and 611 deletions

View file

@ -196,15 +196,7 @@ instruction()#comment with bad spacing
"Generator",
]
@@ -54,32 +54,39 @@
# for compiler in compilers.values():
# add_compiler(compiler)
add_compiler(compilers[(7.0, 32)])
- # add_compiler(compilers[(7.1, 64)])
+# add_compiler(compilers[(7.1, 64)])
+
@@ -60,26 +60,32 @@
# Comment before function.
def inline_comments_in_brackets_ruin_everything():
if typedargslist:
@ -229,7 +221,7 @@ instruction()#comment with bad spacing
+ parameters.what_if_this_was_actually_long.children[0],
+ body,
+ parameters.children[-1],
+ ]
+ ] # type: ignore
if (
self._proc is not None
- # has the child process finished?
@ -246,7 +238,7 @@ instruction()#comment with bad spacing
):
pass
# no newline before or after
@@ -103,42 +110,42 @@
@@ -103,35 +109,35 @@
############################################################################
call2(
@ -298,16 +290,7 @@ instruction()#comment with bad spacing
]
while True:
if False:
continue
- # and round and round we go
- # and round and round we go
+ # and round and round we go
+ # and round and round we go
# let's return
return Node(
@@ -167,7 +174,7 @@
@@ -167,7 +173,7 @@
#######################
@ -377,10 +360,9 @@ else:
# for compiler in compilers.values():
# add_compiler(compiler)
add_compiler(compilers[(7.0, 32)])
# add_compiler(compilers[(7.1, 64)])
# add_compiler(compilers[(7.1, 64)])
# Comment before function.
def inline_comments_in_brackets_ruin_everything():
if typedargslist:
@ -400,7 +382,7 @@ def inline_comments_in_brackets_ruin_everything():
parameters.what_if_this_was_actually_long.children[0],
body,
parameters.children[-1],
]
] # type: ignore
if (
self._proc is not None
and # has the child process finished?
@ -467,8 +449,8 @@ short
if False:
continue
# and round and round we go
# and round and round we go
# and round and round we go
# and round and round we go
# let's return
return Node(

View file

@ -131,17 +131,15 @@ aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*ite
```diff
--- Black
+++ Ruff
@@ -2,8 +2,8 @@
@@ -2,7 +2,7 @@
def f(
- a, # type: int
-):
+ a,
+): # type: int
):
pass
@@ -14,44 +14,42 @@
@ -155,7 +153,6 @@ aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*ite
- g, # type: int
- h, # type: int
- i, # type: int
-):
+ a,
+ b,
+ c,
@ -165,7 +162,7 @@ aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*ite
+ g,
+ h,
+ i,
+): # type: int# type: int# type: int# type: int# type: int# type: int# type: int# type: int# type: int
):
# type: (...) -> None
pass
@ -175,12 +172,11 @@ aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*ite
- *args, # type: *Any
- default=False, # type: bool
- **kwargs, # type: **Any
-):
+ arg,
+ *args,
+ default=False,
+ **kwargs,
+): # type: int# type: *Any
):
# type: (...) -> None
pass
@ -190,12 +186,11 @@ aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*ite
- b, # type: int
- c, # type: int
- d, # type: int
-):
+ a,
+ b,
+ c,
+ d,
+): # type: int# type: int# type: int# type: int# type: int
):
# type: (...) -> None
element = 0 # type: int
@ -208,41 +203,32 @@ 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 = (
@@ -66,26 +64,26 @@
+ element
+ another_element
+ another_element_with_long_name
- ) # type: int
+ )
@@ -70,21 +68,21 @@
def f(
- x, # not a type comment
- y, # type: int
-):
+ x,
+ y,
+): # not a type comment# type: int
):
# type: (...) -> None
pass
def f(
- x, # not a type comment
-): # type: (int) -> None
+ x,
+): # not a type comment# type: (int) -> None
): # type: (int) -> None
pass
def func(
- a=some_list[0], # type: int
-): # type: () -> int
+ a=some_list[0],
+):
): # type: () -> int
c = call(
0.0123,
0.0456,
@@ -96,23 +94,37 @@
0.0123,
0.0456,
@ -298,7 +284,7 @@ from typing import Any, Tuple
def f(
a,
): # type: int
):
pass
@ -318,7 +304,7 @@ def f(
g,
h,
i,
): # type: int# type: int# type: int# type: int# type: int# type: int# type: int# type: int# type: int
):
# type: (...) -> None
pass
@ -328,7 +314,7 @@ def f(
*args,
default=False,
**kwargs,
): # type: int# type: *Any
):
# type: (...) -> None
pass
@ -338,7 +324,7 @@ def f(
b,
c,
d,
): # type: int# type: int# type: int# type: int# type: int
):
# type: (...) -> None
element = 0 # type: int
@ -359,26 +345,26 @@ def f(
+ element
+ another_element
+ another_element_with_long_name
)
) # type: int
def f(
x,
y,
): # not a type comment# type: int
):
# type: (...) -> None
pass
def f(
x,
): # not a type comment# type: (int) -> None
): # type: (int) -> None
pass
def func(
a=some_list[0],
):
): # type: () -> int
c = call(
0.0123,
0.0456,

View file

@ -164,7 +164,7 @@ def bar():
# This should be split from the above by two lines
class MyClassWithComplexLeadingComments:
pass
@@ -57,13 +58,13 @@
@@ -57,11 +58,11 @@
# leading 1
@deco1
@ -174,16 +174,13 @@ def bar():
-@deco2(with_args=True)
-# leading 3
-@deco3
-# leading 4
+deco2(with_args=True)
+@# leading 3
+deco3
# leading 4
def decorated():
+ # leading 4
pass
@@ -72,13 +73,12 @@
@@ -72,11 +73,10 @@
# leading 1
@deco1
@ -192,17 +189,14 @@ def bar():
-
-# leading 3 that already has an empty line
-@deco3
-# leading 4
+@# leading 2
+deco2(with_args=True)
+@# leading 3 that already has an empty line
+deco3
# leading 4
def decorated_with_split_leading_comments():
+ # leading 4
pass
@@ -87,18 +87,18 @@
@@ -87,10 +87,10 @@
# leading 1
@deco1
@ -210,16 +204,14 @@ def bar():
-@deco2(with_args=True)
-# leading 3
-@deco3
-
-# leading 4 that already has an empty line
+@# leading 2
+deco2(with_args=True)
+@# leading 3
+deco3
def decorated_with_split_leading_comments():
+ # leading 4 that already has an empty line
pass
# leading 4 that already has an empty line
def decorated_with_split_leading_comments():
@@ -99,6 +99,7 @@
def main():
if a:
@ -227,7 +219,7 @@ def bar():
# Leading comment before inline function
def inline():
pass
@@ -108,12 +108,14 @@
@@ -108,12 +109,14 @@
pass
else:
@ -242,7 +234,7 @@ def bar():
# Leading comment before "top-level inline" function
def top_level_quote_inline():
pass
@@ -123,6 +125,7 @@
@@ -123,6 +126,7 @@
pass
else:
@ -250,37 +242,6 @@ def bar():
# More leading comments
def top_level_quote_inline_after_else():
pass
@@ -138,9 +141,11 @@
# Regression test for https://github.com/psf/black/issues/3454.
def foo():
pass
- # Trailing comment that belongs to this function
+# Trailing comment that belongs to this function
+
+
@decorator1
@decorator2 # fmt: skip
def bar():
@@ -150,12 +155,13 @@
# Regression test for https://github.com/psf/black/issues/3454.
def foo():
pass
- # Trailing comment that belongs to this function.
- # NOTE this comment only has one empty line below, and the formatter
- # should enforce two blank lines.
+# Trailing comment that belongs to this function.
+# NOTE this comment only has one empty line below, and the formatter
+# should enforce two blank lines.
+
@decorator1
-# A standalone comment
def bar():
+ # A standalone comment
pass
```
## Ruff Output
@ -351,8 +312,8 @@ some = statement
deco2(with_args=True)
@# leading 3
deco3
# leading 4
def decorated():
# leading 4
pass
@ -365,8 +326,8 @@ some = statement
deco2(with_args=True)
@# leading 3 that already has an empty line
deco3
# leading 4
def decorated_with_split_leading_comments():
# leading 4
pass
@ -379,8 +340,9 @@ some = statement
deco2(with_args=True)
@# leading 3
deco3
# leading 4 that already has an empty line
def decorated_with_split_leading_comments():
# leading 4 that already has an empty line
pass
@ -429,9 +391,7 @@ class MyClass:
# Regression test for https://github.com/psf/black/issues/3454.
def foo():
pass
# Trailing comment that belongs to this function
# Trailing comment that belongs to this function
@decorator1
@ -443,15 +403,14 @@ def bar():
# Regression test for https://github.com/psf/black/issues/3454.
def foo():
pass
# Trailing comment that belongs to this function.
# NOTE this comment only has one empty line below, and the formatter
# should enforce two blank lines.
# Trailing comment that belongs to this function.
# NOTE this comment only has one empty line below, and the formatter
# should enforce two blank lines.
@decorator1
# A standalone comment
def bar():
# A standalone comment
pass
```

View file

@ -204,7 +204,7 @@ class C:
)
self.assertEqual(
unstyle(str(report)),
@@ -22,133 +23,156 @@
@@ -22,133 +23,155 @@
if (
# Rule 1
i % 2 == 0
@ -217,10 +217,10 @@ class C:
- while (
- # Just a comment
- call()
- # Another
- ):
+ while # Just a comment
+ call():
# Another
- ):
print(i)
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,
@ -460,7 +460,7 @@ class C:
"Not what we expected and the message is too long to fit in one line"
" because it's too long"
)
@@ -161,9 +185,8 @@
@@ -161,9 +184,8 @@
8 STORE_ATTR 0 (x)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
@ -508,7 +508,6 @@ class C:
):
while # Just a comment
call():
# Another
print(i)
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,

View file

@ -204,7 +204,7 @@ class C:
)
self.assertEqual(
unstyle(str(report)),
@@ -22,133 +23,156 @@
@@ -22,133 +23,155 @@
if (
# Rule 1
i % 2 == 0
@ -217,10 +217,10 @@ class C:
- while (
- # Just a comment
- call()
- # Another
- ):
+ while # Just a comment
+ call():
# Another
- ):
print(i)
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,
@ -460,7 +460,7 @@ class C:
"Not what we expected and the message is too long to fit in one line"
" because it's too long"
)
@@ -161,9 +185,8 @@
@@ -161,9 +184,8 @@
8 STORE_ATTR 0 (x)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
@ -508,7 +508,6 @@ class C:
):
while # Just a comment
call():
# Another
print(i)
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,

View file

@ -26,13 +26,12 @@ def f(): pass
```diff
--- Black
+++ Ruff
@@ -1,10 +1,14 @@
@@ -1,8 +1,12 @@
# fmt: off
-@test([
- 1, 2,
- 3, 4,
-])
-# fmt: on
+@test(
+ [
+ 1,
@ -41,11 +40,9 @@ def f(): pass
+ 4,
+ ]
+)
# fmt: on
def f():
+ # fmt: on
pass
```
## Ruff Output
@ -60,8 +57,8 @@ def f(): pass
4,
]
)
# fmt: on
def f():
# fmt: on
pass

View file

@ -165,7 +165,7 @@ elif unformatted:
print("This will be formatted")
@@ -68,20 +62,21 @@
@@ -68,20 +62,19 @@
class Named(t.Protocol):
# fmt: off
@property
@ -177,11 +177,9 @@ elif unformatted:
class Factory(t.Protocol):
def this_will_be_formatted(self, **kwargs) -> Named:
...
-
# fmt: on
- # fmt: on
+# fmt: on
+
# Regression test for https://github.com/psf/black/issues/3436.
if x:
@ -267,9 +265,7 @@ class Named(t.Protocol):
class Factory(t.Protocol):
def this_will_be_formatted(self, **kwargs) -> Named:
...
# fmt: on
# fmt: on
# Regression test for https://github.com/psf/black/issues/3436.

View file

@ -1,49 +0,0 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/fmtskip6.py
---
## Input
```py
class A:
def f(self):
for line in range(10):
if True:
pass # fmt: skip
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -2,4 +2,4 @@
def f(self):
for line in range(10):
if True:
- pass # fmt: skip
+ pass
```
## Ruff Output
```py
class A:
def f(self):
for line in range(10):
if True:
pass
```
## Black Output
```py
class A:
def f(self):
for line in range(10):
if True:
pass # fmt: skip
```

View file

@ -117,12 +117,13 @@ def __await__(): return (yield)
def func_no_args():
@@ -64,19 +64,14 @@
@@ -64,19 +64,15 @@
def spaces2(result=_core.Value(None)):
- assert fut is self._read_fut, (fut, self._read_fut)
+ assert fut is self._read_fut, fut, self._read_fut
+
def example(session):
@ -142,7 +143,7 @@ def __await__(): return (yield)
def long_lines():
@@ -135,14 +130,13 @@
@@ -135,14 +131,13 @@
a,
**kwargs,
) -> A:
@ -233,6 +234,7 @@ def spaces2(result=_core.Value(None)):
assert fut is self._read_fut, fut, self._read_fut
def example(session):
result = session.query(models.Customer.id).filter(
models.Customer.account_id == account_id,

View file

@ -123,10 +123,9 @@ async def main():
+ await (asyncio.sleep(1)) # Hello
-async def main():
async def main():
- await asyncio.sleep(1) # Hello
+async def main(): # Hello
+ await (asyncio.sleep(1))
+ await (asyncio.sleep(1)) # Hello
# Long lines
@ -231,8 +230,8 @@ async def main():
await (asyncio.sleep(1)) # Hello
async def main(): # Hello
await (asyncio.sleep(1))
async def main():
await (asyncio.sleep(1)) # Hello
# Long lines