From eeb8a5fe0a13f0fe570e8042be8e7b797679f466 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Thu, 20 Jul 2023 12:07:22 +0200 Subject: [PATCH] Avoid line break before `for` in comprehension if outer expression expands (#5912) --- .../fixtures/ruff/expression/dict_comp.py | 13 +++++++++- .../fixtures/ruff/expression/list_comp.py | 10 +++++++ .../test/fixtures/ruff/expression/set_comp.py | 10 +++++++ .../src/expression/expr_dict_comp.rs | 4 +-- .../src/expression/expr_list_comp.rs | 4 +-- .../src/expression/expr_set_comp.rs | 4 +-- .../format@expression__binary.py.snap | 6 ++--- .../format@expression__dict_comp.py.snap | 26 ++++++++++++++++++- .../format@expression__list_comp.py.snap | 21 +++++++++++++++ .../format@expression__set_comp.py.snap | 21 +++++++++++++++ 10 files changed, 107 insertions(+), 12 deletions(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/dict_comp.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/dict_comp.py index 87b0c035a7..cf4df74d92 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/dict_comp.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/dict_comp.py @@ -74,4 +74,15 @@ a, # Trailing ) in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension # Trailing } # Trailing -# Trailing \ No newline at end of file +# Trailing + + +# Regression tests for https://github.com/astral-sh/ruff/issues/5911 +selected_choices = { + k: str(v) for v in value if str(v) not in self.choices.field.empty_values +} + +selected_choices = { + k: str(v) + for vvvvvvvvvvvvvvvvvvvvvvv in value if str(v) not in self.choices.field.empty_values +} diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/list_comp.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/list_comp.py index 6f8a4dbd31..34a594a7f5 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/list_comp.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/list_comp.py @@ -43,3 +43,13 @@ if gggggggggggggggggggggggggggggggggggggggggggg ] + +# Regression tests for https://github.com/astral-sh/ruff/issues/5911 +selected_choices = [ + str(v) for v in value if str(v) not in self.choices.field.empty_values +] + +selected_choices = [ + str(v) + for vvvvvvvvvvvvvvvvvvvvvvv in value if str(v) not in self.choices.field.empty_values +] diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/set_comp.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/set_comp.py index 0f260111a7..a88023ab1f 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/set_comp.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/set_comp.py @@ -43,3 +43,13 @@ if gggggggggggggggggggggggggggggggggggggggggggg } + +# Regression tests for https://github.com/astral-sh/ruff/issues/5911 +selected_choices = { + str(v) for v in value if str(v) not in self.choices.field.empty_values +} + +selected_choices = { + str(v) + for vvvvvvvvvvvvvvvvvvvvvvv in value if str(v) not in self.choices.field.empty_values +} diff --git a/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs b/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs index cc3b48569f..c57d6e1f4b 100644 --- a/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs @@ -31,14 +31,14 @@ impl FormatNodeRule for FormatExprDictComp { f, [parenthesized( "{", - &format_args!( + &group(&format_args!( group(&key.format()), text(":"), space(), value.format(), soft_line_break_or_space(), group(&joined) - ), + )), "}" )] ) diff --git a/crates/ruff_python_formatter/src/expression/expr_list_comp.rs b/crates/ruff_python_formatter/src/expression/expr_list_comp.rs index 764ff399b1..9d4a223d2c 100644 --- a/crates/ruff_python_formatter/src/expression/expr_list_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_list_comp.rs @@ -28,11 +28,11 @@ impl FormatNodeRule for FormatExprListComp { f, [parenthesized( "[", - &format_args!( + &group(&format_args![ group(&elt.format()), soft_line_break_or_space(), group(&joined) - ), + ]), "]" )] ) diff --git a/crates/ruff_python_formatter/src/expression/expr_set_comp.rs b/crates/ruff_python_formatter/src/expression/expr_set_comp.rs index e4e89268dc..a539d8e6a1 100644 --- a/crates/ruff_python_formatter/src/expression/expr_set_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_set_comp.rs @@ -28,11 +28,11 @@ impl FormatNodeRule for FormatExprSetComp { f, [parenthesized( "{", - &format_args!( + &group(&format_args!( group(&elt.format()), soft_line_break_or_space(), group(&joined) - ), + )), "}" )] ) diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__binary.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__binary.py.snap index 1dd043b7e7..61499e20e3 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__binary.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__binary.py.snap @@ -278,16 +278,14 @@ aaaaaaaaaaaaaa + { eeeeeee, } aaaaaaaaaaaaaa + [ - a - for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + a for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ] ( aaaaaaaaaaaaaa + (a for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) ) aaaaaaaaaaaaaa + { - a - for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + a for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb } # Wraps it in parentheses if it needs to break both left and right 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 1887490574..651e8e5644 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 @@ -80,7 +80,19 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression a, # Trailing ) in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension # Trailing } # Trailing -# Trailing``` +# Trailing + + +# Regression tests for https://github.com/astral-sh/ruff/issues/5911 +selected_choices = { + k: str(v) for v in value if str(v) not in self.choices.field.empty_values +} + +selected_choices = { + k: str(v) + for vvvvvvvvvvvvvvvvvvvvvvv in value if str(v) not in self.choices.field.empty_values +} +``` ## Output ```py @@ -217,6 +229,18 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression ) in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension # Trailing } # Trailing # Trailing + + +# Regression tests for https://github.com/astral-sh/ruff/issues/5911 +selected_choices = { + k: str(v) for v in value if str(v) not in self.choices.field.empty_values +} + +selected_choices = { + k: str(v) + for vvvvvvvvvvvvvvvvvvvvvvv in value + if str(v) not in self.choices.field.empty_values +} ``` 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 374e4aa6e3..aa1e9aaf32 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 @@ -49,6 +49,16 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression if gggggggggggggggggggggggggggggggggggggggggggg ] + +# Regression tests for https://github.com/astral-sh/ruff/issues/5911 +selected_choices = [ + str(v) for v in value if str(v) not in self.choices.field.empty_values +] + +selected_choices = [ + str(v) + for vvvvvvvvvvvvvvvvvvvvvvv in value if str(v) not in self.choices.field.empty_values +] ``` ## Output @@ -101,6 +111,17 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression < hhhhhhhhhhhhhhhhhhhhhhhhhh if gggggggggggggggggggggggggggggggggggggggggggg ] + +# Regression tests for https://github.com/astral-sh/ruff/issues/5911 +selected_choices = [ + str(v) for v in value if str(v) not in self.choices.field.empty_values +] + +selected_choices = [ + str(v) + for vvvvvvvvvvvvvvvvvvvvvvv in value + if str(v) not in self.choices.field.empty_values +] ``` 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 256e2a8bc1..4244dd1318 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 @@ -49,6 +49,16 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression if gggggggggggggggggggggggggggggggggggggggggggg } + +# Regression tests for https://github.com/astral-sh/ruff/issues/5911 +selected_choices = { + str(v) for v in value if str(v) not in self.choices.field.empty_values +} + +selected_choices = { + str(v) + for vvvvvvvvvvvvvvvvvvvvvvv in value if str(v) not in self.choices.field.empty_values +} ``` ## Output @@ -101,6 +111,17 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression < hhhhhhhhhhhhhhhhhhhhhhhhhh if gggggggggggggggggggggggggggggggggggggggggggg } + +# Regression tests for https://github.com/astral-sh/ruff/issues/5911 +selected_choices = { + str(v) for v in value if str(v) not in self.choices.field.empty_values +} + +selected_choices = { + str(v) + for vvvvvvvvvvvvvvvvvvvvvvv in value + if str(v) not in self.choices.field.empty_values +} ```