format ExprListComp (#5600)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
David Szotten 2023-07-11 07:35:51 +01:00 committed by GitHub
parent 987111f5fb
commit 1782fb8c30
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 489 additions and 128 deletions

View file

@ -42,7 +42,7 @@ def make_arange(n):
```diff
--- Black
+++ Ruff
@@ -2,29 +2,21 @@
@@ -2,29 +2,27 @@
def f():
@ -60,13 +60,16 @@ def make_arange(n):
async def func():
if test:
- out_batched = [
- i
out_batched = [
i
- async for i in aitertools._async_map(
- self.async_inc, arange(8), batch_size=3
- )
- ]
+ out_batched = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
+ async for
+ i
+ in
+ aitertools._async_map(self.async_inc, arange(8), batch_size=3)
]
def awaited_generator_value(n):
@ -95,7 +98,13 @@ def g():
async def func():
if test:
out_batched = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
out_batched = [
i
async for
i
in
aitertools._async_map(self.async_inc, arange(8), batch_size=3)
]
def awaited_generator_value(n):

View file

@ -59,7 +59,7 @@ while x := f(x):
```diff
--- Black
+++ Ruff
@@ -2,10 +2,10 @@
@@ -2,7 +2,7 @@
(a := a)
if (match := pattern.search(data)) is None:
pass
@ -67,11 +67,7 @@ while x := f(x):
+if (match := pattern.search(data)):
pass
[y := f(x), y**2, y**3]
-filtered_data = [y for x in data if (y := f(x)) is None]
+filtered_data = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
(y := f(x))
y0 = (y1 := f(x))
foo(x=(y := f(x)))
filtered_data = [y for x in data if (y := f(x)) is None]
@@ -19,10 +19,10 @@
pass
@ -122,7 +118,7 @@ if (match := pattern.search(data)) is None:
if (match := pattern.search(data)):
pass
[y := f(x), y**2, y**3]
filtered_data = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
filtered_data = [y for x in data if (y := f(x)) is None]
(y := f(x))
y0 = (y1 := f(x))
foo(x=(y := f(x)))

View file

@ -224,36 +224,43 @@ instruction()#comment with bad spacing
if (
self._proc is not None
# has the child process finished?
@@ -114,25 +122,9 @@
# yup
@@ -115,7 +123,12 @@
arg3=True,
)
- lcomp = [
lcomp = [
- element for element in collection if element is not None # yup # yup # right
- ]
- lcomp2 = [
- # hello
- element
- # yup
- for element in collection
- # right
+ element # yup
+ for
+ element
+ in
+ collection # yup
+ if element is not None # right
]
lcomp2 = [
# hello
@@ -123,7 +136,9 @@
# yup
for element in collection
# right
- if element is not None
- ]
- lcomp3 = [
- # This one is actually too long to fit in a single line.
- element.split("\n", 1)[0]
- # yup
- for element in collection.select_elements()
- # right
+ if
+ element
+ is not None
]
lcomp3 = [
# This one is actually too long to fit in a single line.
@@ -131,7 +146,9 @@
# yup
for element in collection.select_elements()
# right
- if element is not None
- ]
+ lcomp = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
+ lcomp2 = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
+ lcomp3 = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
+ if
+ element
+ is not None
]
while True:
if False:
continue
@@ -143,7 +135,10 @@
@@ -143,7 +160,10 @@
# let's return
return Node(
syms.simple_stmt,
@ -265,7 +272,7 @@ instruction()#comment with bad spacing
)
@@ -158,7 +153,11 @@
@@ -158,7 +178,11 @@
class Test:
def _init_host(self, parsed) -> None:
@ -407,9 +414,34 @@ short
# yup
arg3=True,
)
lcomp = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
lcomp2 = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
lcomp3 = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
lcomp = [
element # yup
for
element
in
collection # yup
if element is not None # right
]
lcomp2 = [
# hello
element
# yup
for element in collection
# right
if
element
is not None
]
lcomp3 = [
# This one is actually too long to fit in a single line.
element.split("\n", 1)[0]
# yup
for element in collection.select_elements()
# right
if
element
is not None
]
while True:
if False:
continue

View file

@ -60,22 +60,17 @@ def func():
```diff
--- Black
+++ Ruff
@@ -6,14 +6,7 @@
x = """
a really long string
"""
- lcomp3 = [
- # This one is actually too long to fit in a single line.
- element.split("\n", 1)[0]
- # yup
- for element in collection.select_elements()
- # right
@@ -12,7 +12,9 @@
# yup
for element in collection.select_elements()
# right
- if element is not None
- ]
+ lcomp3 = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
+ if
+ element
+ is not None
]
# Capture each of the exceptions in the MultiError along with each of their causes and contexts
if isinstance(exc_value, MultiError):
embedded = []
```
## Ruff Output
@ -89,7 +84,16 @@ def func():
x = """
a really long string
"""
lcomp3 = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
lcomp3 = [
# This one is actually too long to fit in a single line.
element.split("\n", 1)[0]
# yup
for element in collection.select_elements()
# right
if
element
is not None
]
# Capture each of the exceptions in the MultiError along with each of their causes and contexts
if isinstance(exc_value, MultiError):
embedded = []

View file

@ -324,10 +324,14 @@ last_call()
-{(i**2) for i in (1, 2, 3)}
-{(i**2) for i, _ in ((1, "a"), (2, "b"), (3, "c"))}
-{((i**2) + j) for i in (1, 2, 3) for j in (1, 2, 3)}
-[i for i in (1, 2, 3)]
-[(i**2) for i in (1, 2, 3)]
-[(i**2) for i, _ in ((1, "a"), (2, "b"), (3, "c"))]
-[((i**2) + j) for i in (1, 2, 3) for j in (1, 2, 3)]
+{NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}
+{NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}
+{NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}
+{NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}
[i for i in (1, 2, 3)]
[(i**2) for i in (1, 2, 3)]
[(i**2) for i, _ in ((1, "a"), (2, "b"), (3, "c"))]
[((i**2) + j) for i in (1, 2, 3) for j in (1, 2, 3)]
-{i: 0 for i in (1, 2, 3)}
-{i: j for i, j in ((1, "a"), (2, "b"), (3, "c"))}
-{a: b * 2 for a, b in dictionary.items()}
@ -336,14 +340,6 @@ last_call()
- k: v
- for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension
-}
+{NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}
+{NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}
+{NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}
+{NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}
+[NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
+[NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
+[NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
+[NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
+{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict}
+{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict}
+{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict}
@ -369,9 +365,6 @@ last_call()
- int,
- float,
- dict[str, int],
-]
-very_long_variable_name_filters: t.List[
- t.Tuple[str, t.Union[str, t.List[t.Optional[str]]]],
+ (
+ str,
+ int,
@ -379,6 +372,9 @@ last_call()
+ dict[str, int],
+ )
]
-very_long_variable_name_filters: t.List[
- t.Tuple[str, t.Union[str, t.List[t.Optional[str]]]],
-]
-xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod( # type: ignore
- sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
-)
@ -433,10 +429,11 @@ last_call()
(*starred,)
{
"id": "1",
@@ -208,24 +202,14 @@
@@ -207,25 +201,15 @@
)
what_is_up_with_those_new_coord_names = (coord_names | set(vars_to_create)) - set(
vars_to_remove
)
-)
-result = (
- session.query(models.Customer.id)
- .filter(
@ -444,7 +441,7 @@ last_call()
- )
- .order_by(models.Customer.id.asc())
- .all()
-)
)
-result = (
- session.query(models.Customer.id)
- .filter(
@ -645,10 +642,10 @@ str or None if (1 if True else 2) else str or bytes or None
{NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}
{NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}
{NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}
[NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
[NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
[NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
[NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
[i for i in (1, 2, 3)]
[(i**2) for i in (1, 2, 3)]
[(i**2) for i, _ in ((1, "a"), (2, "b"), (3, "c"))]
[((i**2) + j) for i in (1, 2, 3) for j in (1, 2, 3)]
{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict}
{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict}
{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict}

View file

@ -75,7 +75,7 @@ return np.divide(
```diff
--- Black
+++ Ruff
@@ -15,38 +15,38 @@
@@ -15,7 +15,7 @@
b = 5 ** f()
c = -(5**2)
d = 5 ** f["hi"]
@ -84,21 +84,16 @@ return np.divide(
f = f() ** 5
g = a.b**c.d
h = 5 ** funcs.f()
i = funcs.f() ** 5
j = super().name ** 5
-k = [(2**idx, value) for idx, value in pairs]
+k = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
l = mod.weights_[0] == pytest.approx(0.95**100, abs=0.001)
@@ -26,7 +26,7 @@
m = [([2**63], [1, 2**63])]
n = count <= 10**5
o = settings(max_examples=10**6)
-p = {(k, k**2): v**2 for k, v in pairs}
-q = [10**i for i in range(6)]
+p = {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict}
+q = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
q = [10**i for i in range(6)]
r = x**y
a = 5.0**~4.0
@@ -34,7 +34,7 @@
b = 5.0 ** f()
c = -(5.0**2.0)
d = 5.0 ** f["hi"]
@ -107,21 +102,15 @@ return np.divide(
f = f() ** 5.0
g = a.b**c.d
h = 5.0 ** funcs.f()
i = funcs.f() ** 5.0
j = super().name ** 5.0
-k = [(2.0**idx, value) for idx, value in pairs]
+k = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
l = mod.weights_[0] == pytest.approx(0.95**100, abs=0.001)
@@ -45,7 +45,7 @@
m = [([2.0**63.0], [1.0, 2**63.0])]
n = count <= 10**5.0
o = settings(max_examples=10**6.0)
-p = {(k, k**2): v**2.0 for k, v in pairs}
-q = [10.5**i for i in range(6)]
+p = {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict}
+q = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
q = [10.5**i for i in range(6)]
# WE SHOULD DEFINITELY NOT EAT THESE COMMENTS (https://github.com/psf/black/issues/2873)
@@ -55,9 +55,11 @@
view.variance, # type: ignore[union-attr]
view.sum_of_weights, # type: ignore[union-attr]
@ -164,13 +153,13 @@ g = a.b**c.d
h = 5 ** funcs.f()
i = funcs.f() ** 5
j = super().name ** 5
k = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
k = [(2**idx, value) for idx, value in pairs]
l = mod.weights_[0] == pytest.approx(0.95**100, abs=0.001)
m = [([2**63], [1, 2**63])]
n = count <= 10**5
o = settings(max_examples=10**6)
p = {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict}
q = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
q = [10**i for i in range(6)]
r = x**y
a = 5.0**~4.0
@ -183,13 +172,13 @@ g = a.b**c.d
h = 5.0 ** funcs.f()
i = funcs.f() ** 5.0
j = super().name ** 5.0
k = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
k = [(2.0**idx, value) for idx, value in pairs]
l = mod.weights_[0] == pytest.approx(0.95**100, abs=0.001)
m = [([2.0**63.0], [1.0, 2**63.0])]
n = count <= 10**5.0
o = settings(max_examples=10**6.0)
p = {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict}
q = [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
q = [10.5**i for i in range(6)]
# WE SHOULD DEFINITELY NOT EAT THESE COMMENTS (https://github.com/psf/black/issues/2873)

View file

@ -43,7 +43,7 @@ ham[lower + offset : upper + offset]
```diff
--- Black
+++ Ruff
@@ -4,28 +4,34 @@
@@ -4,19 +4,21 @@
slice[d::d]
slice[0]
slice[-1]
@ -68,19 +68,11 @@ ham[lower + offset : upper + offset]
+slice[
+ (NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []) : x
+]
+slice[
+ :: [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
+]
+slice[ :: [i for i in range(42)]]
async def f():
- slice[await x : [i async for i in arange(42)] : 42]
+ slice[
+ await x : [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []] : 42
+ ]
# These are from PEP-8:
@@ -27,5 +29,5 @@
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
# ham[lower+offset : upper+offset]
@ -112,15 +104,11 @@ slice[not so_simple : 1 < val <= 10]
slice[
(NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []) : x
]
slice[
:: [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
]
slice[ :: [i for i in range(42)]]
async def f():
slice[
await x : [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []] : 42
]
slice[await x : [i async for i in arange(42)] : 42]
# These are from PEP-8:

View file

@ -277,10 +277,10 @@ aaaaaaaaaaaaaa + {
dddddddddddddddd,
eeeeeee,
}
(
aaaaaaaaaaaaaa
+ [NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in []]
)
aaaaaaaaaaaaaa + [
a
for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
]
(
aaaaaaaaaaaaaa
+ (NOT_YET_IMPLEMENTED_generator_key for NOT_YET_IMPLEMENTED_generator_key in [])

View file

@ -0,0 +1,86 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/list_comp.py
---
## Input
```py
[i for i in []]
[i for i in [1,]]
[
a # a
for # for
c # c
in # in
e # e
]
[
# above a
a # a
# above for
for # for
# above c
c # c
# above in
in # in
# above e
e # e
# above if
if # if
# above f
f # f
# above if2
if # if2
# above g
g # g
]
```
## Output
```py
[i for i in []]
[
i
for
i
in
[
1,
]
]
[
a # a
for # for
c # c
in # in
e # e
]
[
# above a
a # a
# above for
for # for
# above c
c # c
# above in
in # in
# above e
e # e
# above if
if # if
# above f
f # f
# above if2
if # if2
# above g
g # g
]
```