Compare commits

...

1 Commits

Author SHA1 Message Date
Micha Reiser
ee7294e556 Split over-long comprehensions after in 2024-08-15 18:31:59 +02:00
6 changed files with 72 additions and 47 deletions

View File

@@ -84,13 +84,13 @@ impl FormatNodeRule<Comprehension> for FormatComprehension {
let (trailing_in_comments, dangling_if_comments) = dangling_comments let (trailing_in_comments, dangling_if_comments) = dangling_comments
.split_at(dangling_comments.partition_point(|comment| comment.start() < iter.start())); .split_at(dangling_comments.partition_point(|comment| comment.start() < iter.start()));
let in_spacer = format_with(|f| { // let in_spacer = format_with(|f| {
if before_in_comments.is_empty() { // // if before_in_comments.is_empty() {
space().fmt(f) // // space().fmt(f)
} else { // // } else {
soft_line_break_or_space().fmt(f) // soft_line_break_or_space().fmt(f)
} // // }
}); // });
write!( write!(
f, f,
@@ -101,10 +101,12 @@ impl FormatNodeRule<Comprehension> for FormatComprehension {
expression: target, expression: target,
preserve_parentheses: !target.is_tuple_expr() preserve_parentheses: !target.is_tuple_expr()
}, },
ExprTupleWithoutParentheses(target), group(&format_args![
in_spacer, ExprTupleWithoutParentheses(target),
leading_comments(before_in_comments), soft_line_break_or_space(),
token("in"), leading_comments(before_in_comments),
token("in"),
]),
trailing_comments(trailing_in_comments), trailing_comments(trailing_in_comments),
Spacer { Spacer {
expression: iter, expression: iter,

View File

@@ -266,7 +266,17 @@ last_call()
```diff ```diff
--- Black --- Black
+++ Ruff +++ Ruff
@@ -115,7 +115,7 @@ @@ -101,7 +101,8 @@
{a: b * -2 for a, b in dictionary.items()}
{
k: v
- for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension
+ for k, v
+ in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension
}
Python3 > Python2 > COBOL
Life is Life
@@ -115,7 +116,7 @@
arg, arg,
another, another,
kwarg="hey", kwarg="hey",
@@ -383,7 +393,8 @@ str or None if (1 if True else 2) else str or bytes or None
{a: b * -2 for a, b in dictionary.items()} {a: b * -2 for a, b in dictionary.items()}
{ {
k: v k: v
for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension for k, v
in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension
} }
Python3 > Python2 > COBOL Python3 > Python2 > COBOL
Life is Life Life is Life
@@ -1028,5 +1039,3 @@ bbbb >> bbbb * bbbb
last_call() last_call()
# standalone comment at ENDMARKER # standalone comment at ENDMARKER
``` ```

View File

@@ -373,7 +373,7 @@ for foo in ["a", "b"]:
func( func(
[ [
@@ -114,13 +140,15 @@ @@ -114,13 +140,16 @@
func( func(
[x for x in "long line long line long line long line long line long line long line"] [x for x in "long line long line long line long line long line long line long line"]
) )
@@ -386,7 +386,8 @@ for foo in ["a", "b"]:
- for x in "long line long line long line long line long line long line long line" - for x in "long line long line long line long line long line long line long line"
+ for x in [ + for x in [
+ x + x
+ for x in "long line long line long line long line long line long line long line" + for x
+ in "long line long line long line long line long line long line long line"
+ ] + ]
] ]
-]) -])
@@ -394,7 +395,7 @@ for foo in ["a", "b"]:
foooooooooooooooooooo( foooooooooooooooooooo(
[{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size} [{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size}
@@ -131,10 +159,12 @@ @@ -131,10 +160,12 @@
) )
nested_mapping = { nested_mapping = {
@@ -411,7 +412,7 @@ for foo in ["a", "b"]:
} }
explicit_exploding = [ explicit_exploding = [
[ [
@@ -144,24 +174,34 @@ @@ -144,24 +175,34 @@
], ],
], ],
] ]
@@ -461,7 +462,7 @@ for foo in ["a", "b"]:
# Edge case when deciding whether to hug the brackets without inner content. # Edge case when deciding whether to hug the brackets without inner content.
very_very_very_long_variable = very_very_very_long_module.VeryVeryVeryVeryLongClassName( very_very_very_long_variable = very_very_very_long_module.VeryVeryVeryVeryLongClassName(
@@ -169,11 +209,13 @@ @@ -169,11 +210,14 @@
) )
for foo in ["a", "b"]: for foo in ["a", "b"]:
@@ -478,7 +479,8 @@ for foo in ["a", "b"]:
+ individual + individual
+ for + for
+ # Foobar + # Foobar
+ container in xs_by_y[foo] + container
+ in xs_by_y[foo]
+ # Foobar + # Foobar
+ for individual in container["nested"] + for individual in container["nested"]
+ ] + ]
@@ -635,7 +637,8 @@ func(
x x
for x in [ for x in [
x x
for x in "long line long line long line long line long line long line long line" for x
in "long line long line long line long line long line long line long line"
] ]
] ]
) )
@@ -704,7 +707,8 @@ for foo in ["a", "b"]:
individual individual
for for
# Foobar # Foobar
container in xs_by_y[foo] container
in xs_by_y[foo]
# Foobar # Foobar
for individual in container["nested"] for individual in container["nested"]
] ]

View File

@@ -198,7 +198,8 @@ query = {
{ {
a: a # a a: a # a
for c in e # for # c # in # e for c # for # c
in e # in # e
} }
{ {
@@ -232,7 +233,8 @@ query = {
for ccccccccccccccccccccccccccccccccccccccc, ddddddddddddddddddd, [ for ccccccccccccccccccccccccccccccccccccccc, ddddddddddddddddddd, [
eeeeeeeeeeeeeeeeeeeeee, eeeeeeeeeeeeeeeeeeeeee,
fffffffffffffffffffffffff, fffffffffffffffffffffffff,
] in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd ]
in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd
if fffffffffffffffffffffffffffffffffffffffffff if fffffffffffffffffffffffffffffffffffffffffff
< gggggggggggggggggggggggggggggggggggggggggggggg < gggggggggggggggggggggggggggggggggggggggggggggg
< hhhhhhhhhhhhhhhhhhhhhhhhhh < hhhhhhhhhhhhhhhhhhhhhhhhhh
@@ -263,7 +265,8 @@ query = {
a, a,
a, a,
a, a,
] in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension ]
in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension
} }
{ {
k: v k: v
@@ -288,7 +291,8 @@ query = {
a, a,
a, a,
a, a,
) in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension )
in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension
} }
# Leading # Leading
@@ -315,7 +319,8 @@ query = {
a, a,
a, a,
a, # Trailing a, # Trailing
) in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension # Trailing )
in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension # Trailing
} # Trailing } # Trailing
# Trailing # Trailing
@@ -336,7 +341,8 @@ selected_choices = {
for ( # foo for ( # foo
x, x,
aaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaay, aaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaay,
) in z )
in z
} }
a = { a = {
@@ -403,6 +409,3 @@ query = {
for key, queries in self._filters.items() for key, queries in self._filters.items()
} }
``` ```

View File

@@ -189,7 +189,8 @@ y = [
[ [
a # a a # a
for c in e # for # c # in # e for c # for # c
in e # in # e
] ]
[ [
@@ -220,7 +221,8 @@ y = [
for ccccccccccccccccccccccccccccccccccccccc, ddddddddddddddddddd, [ for ccccccccccccccccccccccccccccccccccccccc, ddddddddddddddddddd, [
eeeeeeeeeeeeeeeeeeeeee, eeeeeeeeeeeeeeeeeeeeee,
fffffffffffffffffffffffff, fffffffffffffffffffffffff,
] in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd ]
in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd
if fffffffffffffffffffffffffffffffffffffffffff if fffffffffffffffffffffffffffffffffffffffffff
< gggggggggggggggggggggggggggggggggggggggggggggg < gggggggggggggggggggggggggggggggggggggggggggggg
< hhhhhhhhhhhhhhhhhhhhhhhhhh < hhhhhhhhhhhhhhhhhhhhhhhhhh
@@ -304,7 +306,8 @@ aaaaaaaaaaaaaaaaaaaaa = [
for ( for (
x, x,
y, y,
) in z )
in z
if head_name if head_name
] ]
@@ -326,7 +329,8 @@ y = [
( (
# comment # comment
a a
) in )
in
( (
# comment # comment
x x
@@ -350,7 +354,8 @@ y = [
a a
for for
# comment # comment
a, b in x a, b
in x
if True if True
] ]
@@ -361,7 +366,8 @@ y = [
# comment # comment
a, a,
b, b,
) in x )
in x
if True if True
] ]
@@ -370,7 +376,8 @@ y = [
a a
for for
# comment # comment
a in a
in
# comment # comment
x x
if if
@@ -388,7 +395,7 @@ y = [
```diff ```diff
--- Stable --- Stable
+++ Preview +++ Preview
@@ -142,24 +142,20 @@ @@ -145,25 +145,21 @@
# Leading expression comments: # Leading expression comments:
y = [ y = [
a a
@@ -397,9 +404,10 @@ y = [
+ for ( + for (
# comment # comment
a a
- ) in )
- in
- ( - (
+ ) in ( + in (
# comment # comment
x x
) )

View File

@@ -74,7 +74,8 @@ selected_choices = {
{ {
a # a a # a
for c in e # for # c # in # e for c # for # c
in e # in # e
} }
{ {
@@ -105,7 +106,8 @@ selected_choices = {
for ccccccccccccccccccccccccccccccccccccccc, ddddddddddddddddddd, [ for ccccccccccccccccccccccccccccccccccccccc, ddddddddddddddddddd, [
eeeeeeeeeeeeeeeeeeeeee, eeeeeeeeeeeeeeeeeeeeee,
fffffffffffffffffffffffff, fffffffffffffffffffffffff,
] in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd ]
in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd
if fffffffffffffffffffffffffffffffffffffffffff if fffffffffffffffffffffffffffffffffffffffffff
< gggggggggggggggggggggggggggggggggggggggggggggg < gggggggggggggggggggggggggggggggggggggggggggggg
< hhhhhhhhhhhhhhhhhhhhhhhhhh < hhhhhhhhhhhhhhhhhhhhhhhhhh
@@ -123,6 +125,3 @@ selected_choices = {
if str(v) not in self.choices.field.empty_values if str(v) not in self.choices.field.empty_values
} }
``` ```