Skip to content

Commit 13bd092

Browse files
authored
fix: Stop moving multiline strings to a new line unless inside brackets (#4289)
Signed-off-by: RedGuy12 <[email protected]>
1 parent c9d2635 commit 13bd092

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
- `if` guards in `case` blocks are now wrapped in parentheses when the line is too long.
1818
(#4269)
19+
- Stop moving multiline strings to a new line unless inside brackets (#4289)
1920

2021
### Configuration
2122

src/black/lines.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -858,11 +858,13 @@ def is_line_short_enough( # noqa: C901
858858
return False
859859

860860
if leaf.bracket_depth <= max_level_to_update and leaf.type == token.COMMA:
861-
# Ignore non-nested trailing comma
861+
# Inside brackets, ignore trailing comma
862862
# directly after MLS/MLS-containing expression
863863
ignore_ctxs: List[Optional[LN]] = [None]
864864
ignore_ctxs += multiline_string_contexts
865-
if not (leaf.prev_sibling in ignore_ctxs and i == len(line.leaves) - 1):
865+
if (line.inside_brackets or leaf.bracket_depth > 0) and (
866+
i != len(line.leaves) - 1 or leaf.prev_sibling not in ignore_ctxs
867+
):
866868
commas[leaf.bracket_depth] += 1
867869
if max_level_to_update != math.inf:
868870
max_level_to_update = min(max_level_to_update, leaf.bracket_depth)

tests/data/cases/preview_multiline_strings.py

+14
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ def dastardly_default_value(
175175
"c"
176176
)
177177

178+
assert some_var == expected_result, """
179+
test
180+
"""
181+
assert some_var == expected_result, f"""
182+
expected: {expected_result}
183+
actual: {some_var}"""
184+
178185
# output
179186
"""cow
180187
say""",
@@ -385,3 +392,10 @@ def dastardly_default_value(
385392
)
386393

387394
this_will_also_become_one_line = "abc" # comment
395+
396+
assert some_var == expected_result, """
397+
test
398+
"""
399+
assert some_var == expected_result, f"""
400+
expected: {expected_result}
401+
actual: {some_var}"""

0 commit comments

Comments
 (0)