Avoid line break before for in comprehension if outer expression expands (#5912)

This commit is contained in:
Micha Reiser 2023-07-20 12:07:22 +02:00 committed by GitHub
parent c2b7b46717
commit eeb8a5fe0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 107 additions and 12 deletions

View file

@ -75,3 +75,14 @@
) 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
}

View file

@ -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
]

View file

@ -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
}

View file

@ -31,14 +31,14 @@ impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
f,
[parenthesized(
"{",
&format_args!(
&group(&format_args!(
group(&key.format()),
text(":"),
space(),
value.format(),
soft_line_break_or_space(),
group(&joined)
),
)),
"}"
)]
)

View file

@ -28,11 +28,11 @@ impl FormatNodeRule<ExprListComp> for FormatExprListComp {
f,
[parenthesized(
"[",
&format_args!(
&group(&format_args![
group(&elt.format()),
soft_line_break_or_space(),
group(&joined)
),
]),
"]"
)]
)

View file

@ -28,11 +28,11 @@ impl FormatNodeRule<ExprSetComp> for FormatExprSetComp {
f,
[parenthesized(
"{",
&format_args!(
&group(&format_args!(
group(&elt.format()),
soft_line_break_or_space(),
group(&joined)
),
)),
"}"
)]
)

View file

@ -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

View file

@ -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
}
```

View file

@ -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
]
```

View file

@ -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
}
```