Skip to content

Commit

Permalink
Account for += assignment when deciding whether to split string
Browse files Browse the repository at this point in the history
Fixes psf#2294
  • Loading branch information
bbugyi200 committed Jun 5, 2021
1 parent c53b3ad commit f58ab82
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/black/trans.py
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
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
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

0 comments on commit f58ab82

Please sign in to comment.