diff --git a/crates/ruff_python_formatter/src/other/comprehension.rs b/crates/ruff_python_formatter/src/other/comprehension.rs index 106cf1957c..ed4c94ef77 100644 --- a/crates/ruff_python_formatter/src/other/comprehension.rs +++ b/crates/ruff_python_formatter/src/other/comprehension.rs @@ -84,13 +84,13 @@ impl FormatNodeRule for FormatComprehension { let (trailing_in_comments, dangling_if_comments) = dangling_comments .split_at(dangling_comments.partition_point(|comment| comment.start() < iter.start())); - let in_spacer = format_with(|f| { - if before_in_comments.is_empty() { - space().fmt(f) - } else { - soft_line_break_or_space().fmt(f) - } - }); + // let in_spacer = format_with(|f| { + // // if before_in_comments.is_empty() { + // // space().fmt(f) + // // } else { + // soft_line_break_or_space().fmt(f) + // // } + // }); write!( f, @@ -101,10 +101,12 @@ impl FormatNodeRule for FormatComprehension { expression: target, preserve_parentheses: !target.is_tuple_expr() }, - ExprTupleWithoutParentheses(target), - in_spacer, - leading_comments(before_in_comments), - token("in"), + group(&format_args![ + ExprTupleWithoutParentheses(target), + soft_line_break_or_space(), + leading_comments(before_in_comments), + token("in"), + ]), trailing_comments(trailing_in_comments), Spacer { expression: iter, diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__expression.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__expression.py.snap index 5b60337c94..b13747cc95 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__expression.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__expression.py.snap @@ -266,7 +266,17 @@ last_call() ```diff --- Black +++ 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, another, 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()} { 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 @@ -1028,5 +1039,3 @@ bbbb >> bbbb * bbbb last_call() # standalone comment at ENDMARKER ``` - - diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__preview_hug_parens_with_braces_and_square_brackets.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__preview_hug_parens_with_braces_and_square_brackets.py.snap index 6460c6bfd3..bcb2359269 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__preview_hug_parens_with_braces_and_square_brackets.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__preview_hug_parens_with_braces_and_square_brackets.py.snap @@ -373,7 +373,7 @@ for foo in ["a", "b"]: func( [ -@@ -114,13 +140,15 @@ +@@ -114,13 +140,16 @@ func( [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 [ + 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( [{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size} -@@ -131,10 +159,12 @@ +@@ -131,10 +160,12 @@ ) nested_mapping = { @@ -411,7 +412,7 @@ for foo in ["a", "b"]: } 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. very_very_very_long_variable = very_very_very_long_module.VeryVeryVeryVeryLongClassName( -@@ -169,11 +209,13 @@ +@@ -169,11 +210,14 @@ ) for foo in ["a", "b"]: @@ -478,7 +479,8 @@ for foo in ["a", "b"]: + individual + for + # Foobar -+ container in xs_by_y[foo] ++ container ++ in xs_by_y[foo] + # Foobar + for individual in container["nested"] + ] @@ -635,7 +637,8 @@ func( x for x in [ 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 for # Foobar - container in xs_by_y[foo] + container + in xs_by_y[foo] # Foobar for individual in container["nested"] ] diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__dict_comp.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__dict_comp.py.snap index 56181cd10a..276b186b03 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__dict_comp.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__dict_comp.py.snap @@ -198,7 +198,8 @@ query = { { 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, [ eeeeeeeeeeeeeeeeeeeeee, fffffffffffffffffffffffff, - ] in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd + ] + in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd if fffffffffffffffffffffffffffffffffffffffffff < gggggggggggggggggggggggggggggggggggggggggggggg < hhhhhhhhhhhhhhhhhhhhhhhhhh @@ -263,7 +265,8 @@ query = { 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 @@ -288,7 +291,8 @@ query = { 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 @@ -315,7 +319,8 @@ query = { a, a, 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 @@ -336,7 +341,8 @@ selected_choices = { for ( # foo x, aaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaay, - ) in z + ) + in z } a = { @@ -403,6 +409,3 @@ query = { for key, queries in self._filters.items() } ``` - - - diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__list_comp.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__list_comp.py.snap index ca2f9d45d2..59d17738ec 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__list_comp.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__list_comp.py.snap @@ -189,7 +189,8 @@ y = [ [ 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, [ eeeeeeeeeeeeeeeeeeeeee, fffffffffffffffffffffffff, - ] in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd + ] + in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd if fffffffffffffffffffffffffffffffffffffffffff < gggggggggggggggggggggggggggggggggggggggggggggg < hhhhhhhhhhhhhhhhhhhhhhhhhh @@ -304,7 +306,8 @@ aaaaaaaaaaaaaaaaaaaaa = [ for ( x, y, - ) in z + ) + in z if head_name ] @@ -326,7 +329,8 @@ y = [ ( # comment a - ) in + ) + in ( # comment x @@ -350,7 +354,8 @@ y = [ a for # comment - a, b in x + a, b + in x if True ] @@ -361,7 +366,8 @@ y = [ # comment a, b, - ) in x + ) + in x if True ] @@ -370,7 +376,8 @@ y = [ a for # comment - a in + a + in # comment x if @@ -388,7 +395,7 @@ y = [ ```diff --- Stable +++ Preview -@@ -142,24 +142,20 @@ +@@ -145,25 +145,21 @@ # Leading expression comments: y = [ a @@ -397,9 +404,10 @@ y = [ + for ( # comment a -- ) in + ) +- in - ( -+ ) in ( ++ in ( # comment x ) diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__set_comp.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__set_comp.py.snap index b73d9a599e..4d06678414 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__set_comp.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__set_comp.py.snap @@ -74,7 +74,8 @@ selected_choices = { { 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, [ eeeeeeeeeeeeeeeeeeeeee, fffffffffffffffffffffffff, - ] in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd + ] + in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd if fffffffffffffffffffffffffffffffffffffffffff < gggggggggggggggggggggggggggggggggggggggggggggg < hhhhhhhhhhhhhhhhhhhhhhhhhh @@ -123,6 +125,3 @@ selected_choices = { if str(v) not in self.choices.field.empty_values } ``` - - -