Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Stop moving multiline strings to a new line unless inside brackets #4289

Merged
merged 3 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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}"""