Skip to content

Commit 6380b9f

Browse files
authored
Account for += assignment when deciding whether to split string (#2312)
Fixes #2294
1 parent c53b3ad commit 6380b9f

4 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎CHANGES.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### _Black_
66

7+
- Account for += assignment when deciding whether to split string (#2312)
78
- Correct max string length calculation when there are string operators (#2292)
89
- Fixed option usage when using the `--code` flag (#2259)
910
- Do not call `uvloop.install()` when _Black_ is used as a library (#2303)

‎src/black/trans.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ def _get_max_string_length(self, line: Line, string_idx: int) -> int:
867867
# WMA4 a space, a comma, and a closing bracket [e.g. `), STRING`].
868868
offset += 3
869869

870-
if P.type in [token.COLON, token.EQUAL, token.NAME]:
870+
if P.type in [token.COLON, token.EQUAL, token.PLUSEQUAL, token.NAME]:
871871
# This conditional branch is meant to handle dictionary keys,
872872
# variable assignments, 'return STRING' statement lines, and
873873
# 'else STRING' ternary expression lines.

‎tests/data/long_strings__edge_case.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
assert str(result) == "This long string should be split at some point right close to or around hereeeeeee"
3333
assert str(result) < "This long string should be split at some point right close to or around hereeeeee"
3434
assert "A format string: %s" % "This long string should be split at some point right close to or around hereeeeeee" != result
35+
msg += "This long string should be wrapped in parens at some point right around hereeeee"
36+
msg += "This long string should be split at some point right close to or around hereeeeeeee"
37+
msg += "This long string should not be split at any point ever since it is just righttt"
3538

3639

3740
# output
@@ -127,3 +130,11 @@
127130
" hereeeeeee"
128131
!= result
129132
)
133+
msg += (
134+
"This long string should be wrapped in parens at some point right around hereeeee"
135+
)
136+
msg += (
137+
"This long string should be split at some point right close to or around"
138+
" hereeeeeeee"
139+
)
140+
msg += "This long string should not be split at any point ever since it is just righttt"

‎tests/data/long_strings__regression.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,16 @@ def _legacy_listen_examples():
407407
)
408408

409409

410+
class X:
411+
async def foo(self):
412+
msg = ""
413+
for candidate in CANDIDATES:
414+
msg += (
415+
"**{candidate.object_type} {candidate.rev}**"
416+
" - {candidate.description}\n"
417+
)
418+
419+
410420
temp_msg = (
411421
f"{f'{humanize_number(pos)}.': <{pound_len+2}} "
412422
f"{balance: <{bal_len + 5}} "
@@ -948,6 +958,16 @@ def _legacy_listen_examples():
948958
)
949959

950960

961+
class X:
962+
async def foo(self):
963+
msg = ""
964+
for candidate in CANDIDATES:
965+
msg += (
966+
"**{candidate.object_type} {candidate.rev}**"
967+
" - {candidate.description}\n"
968+
)
969+
970+
951971
temp_msg = (
952972
f"{f'{humanize_number(pos)}.': <{pound_len+2}} "
953973
f"{balance: <{bal_len + 5}} "

0 commit comments

Comments
 (0)