Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a4e35b3

Browse files
authored
Correct max string length calculation when there are string operators (psf#2292)
PR psf#2286 did not fix the edge-cases (e.g. when the string is just long enough to cause a line to be 89 characters long). This PR corrects that mistake.
1 parent cf75673 commit a4e35b3

3 files changed

Lines changed: 40 additions & 13 deletions

File tree

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## Unreleased
4+
5+
### _Black_
6+
7+
- Correct max string length calculation when there are string operators (#2292)
8+
39
## 21.5b2
410

511
### _Black_

src/black/trans.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,18 @@ class BaseStringSplitter(StringTransformer):
738738
* The target string is not a multiline (i.e. triple-quote) string.
739739
"""
740740

741+
STRING_OPERATORS = [
742+
token.EQEQUAL,
743+
token.GREATER,
744+
token.GREATEREQUAL,
745+
token.LESS,
746+
token.LESSEQUAL,
747+
token.NOTEQUAL,
748+
token.PERCENT,
749+
token.PLUS,
750+
token.STAR,
751+
]
752+
741753
@abstractmethod
742754
def do_splitter_match(self, line: Line) -> TMatchResult:
743755
"""
@@ -847,9 +859,9 @@ def _get_max_string_length(self, line: Line, string_idx: int) -> int:
847859
p_idx -= 1
848860

849861
P = LL[p_idx]
850-
if P.type == token.PLUS:
851-
# WMA4 a space and a '+' character (e.g. `+ STRING`).
852-
offset += 2
862+
if P.type in self.STRING_OPERATORS:
863+
# WMA4 a space and a string operator (e.g. `+ STRING` or `== STRING`).
864+
offset += len(str(P)) + 1
853865

854866
if P.type == token.COMMA:
855867
# WMA4 a space, a comma, and a closing bracket [e.g. `), STRING`].
@@ -952,16 +964,6 @@ class StringSplitter(CustomSplitMapMixin, BaseStringSplitter):
952964
CustomSplit objects and add them to the custom split map.
953965
"""
954966

955-
STRING_OPERATORS = [
956-
token.PLUS,
957-
token.STAR,
958-
token.EQEQUAL,
959-
token.NOTEQUAL,
960-
token.LESS,
961-
token.LESSEQUAL,
962-
token.GREATER,
963-
token.GREATEREQUAL,
964-
]
965967
MIN_SUBSTR_SIZE = 6
966968
# Matches an "f-expression" (e.g. {var}) that might be found in an f-string.
967969
RE_FEXPR = r"""

tests/data/long_strings__edge_case.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
)
3030
return f'{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaa'
3131
return f'{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaaa'
32+
assert str(result) == "This long string should be split at some point right close to or around hereeeeeee"
33+
assert str(result) < "This long string should be split at some point right close to or around hereeeeee"
34+
assert "A format string: %s" % "This long string should be split at some point right close to or around hereeeeeee" != result
3235

3336

3437
# output
@@ -108,3 +111,19 @@
108111
f"{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaa"
109112
)
110113
return f"{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaaa"
114+
assert (
115+
str(result)
116+
== "This long string should be split at some point right close to or around"
117+
" hereeeeeee"
118+
)
119+
assert (
120+
str(result)
121+
< "This long string should be split at some point right close to or around"
122+
" hereeeeee"
123+
)
124+
assert (
125+
"A format string: %s"
126+
% "This long string should be split at some point right close to or around"
127+
" hereeeeeee"
128+
!= result
129+
)

0 commit comments

Comments
 (0)