Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Account for += assignment when deciding whether to split string
Fixes #2294
  • Loading branch information
bbugyi200 committed Jun 5, 2021
commit f58ab825f9f7d672bbb22ba5c399b9cd8e27f84d
2 changes: 1 addition & 1 deletion src/black/trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ def _get_max_string_length(self, line: Line, string_idx: int) -> int:
# WMA4 a space, a comma, and a closing bracket [e.g. `), STRING`].
offset += 3

if P.type in [token.COLON, token.EQUAL, token.NAME]:
if P.type in [token.COLON, token.EQUAL, token.PLUSEQUAL, token.NAME]:
# This conditional branch is meant to handle dictionary keys,
# variable assignments, 'return STRING' statement lines, and
# 'else STRING' ternary expression lines.
Expand Down
11 changes: 11 additions & 0 deletions tests/data/long_strings__edge_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
assert str(result) == "This long string should be split at some point right close to or around hereeeeeee"
assert str(result) < "This long string should be split at some point right close to or around hereeeeee"
assert "A format string: %s" % "This long string should be split at some point right close to or around hereeeeeee" != result
msg += "This long string should be split at some point right close to or around hereeeee"
msg += "This long string should be split at some point right close to or around hereeeeeeee"
msg += "This long string should not be split at any point ever since it is just righttt"


# output
Expand Down Expand Up @@ -127,3 +130,11 @@
" hereeeeeee"
!= result
)
msg += (
"This long string should be split at some point right close to or around hereeeee"
)
msg += (
"This long string should be split at some point right close to or around"
" hereeeeeeee"
)
msg += "This long string should not be split at any point ever since it is just righttt"
20 changes: 20 additions & 0 deletions tests/data/long_strings__regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,16 @@ def _legacy_listen_examples():
)


class X:
async def foo(self):
msg = ""
for candidate in CANDIDATES:
msg += (
"**{candidate.object_type} {candidate.rev}**"
" - {candidate.description}\n"
)


temp_msg = (
f"{f'{humanize_number(pos)}.': <{pound_len+2}} "
f"{balance: <{bal_len + 5}} "
Expand Down Expand Up @@ -948,6 +958,16 @@ def _legacy_listen_examples():
)


class X:
async def foo(self):
msg = ""
for candidate in CANDIDATES:
msg += (
"**{candidate.object_type} {candidate.rev}**"
" - {candidate.description}\n"
)


temp_msg = (
f"{f'{humanize_number(pos)}.': <{pound_len+2}} "
f"{balance: <{bal_len + 5}} "
Expand Down