Skip to content

Commit

Permalink
fix: Stop moving multiline strings to a new line unless inside bracke…
Browse files Browse the repository at this point in the history
…ts (#4289)

Signed-off-by: RedGuy12 <[email protected]>
  • Loading branch information
cobaltt7 authored Mar 23, 2024
1 parent c9d2635 commit 13bd092
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

### Configuration

Expand Down
6 changes: 4 additions & 2 deletions src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,11 +858,13 @@ def is_line_short_enough( # noqa: C901
return False

if leaf.bracket_depth <= max_level_to_update and leaf.type == token.COMMA:
# Ignore non-nested trailing comma
# Inside brackets, ignore trailing comma
# directly after MLS/MLS-containing expression
ignore_ctxs: List[Optional[LN]] = [None]
ignore_ctxs += multiline_string_contexts
if not (leaf.prev_sibling in ignore_ctxs and i == len(line.leaves) - 1):
if (line.inside_brackets or leaf.bracket_depth > 0) and (
i != len(line.leaves) - 1 or leaf.prev_sibling not in ignore_ctxs
):
commas[leaf.bracket_depth] += 1
if max_level_to_update != math.inf:
max_level_to_update = min(max_level_to_update, leaf.bracket_depth)
Expand Down
14 changes: 14 additions & 0 deletions tests/data/cases/preview_multiline_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ def dastardly_default_value(
"c"
)

assert some_var == expected_result, """
test
"""
assert some_var == expected_result, f"""
expected: {expected_result}
actual: {some_var}"""

# output
"""cow
say""",
Expand Down Expand Up @@ -385,3 +392,10 @@ def dastardly_default_value(
)

this_will_also_become_one_line = "abc" # comment

assert some_var == expected_result, """
test
"""
assert some_var == expected_result, f"""
expected: {expected_result}
actual: {some_var}"""

0 comments on commit 13bd092

Please sign in to comment.