Skip to content

Commit

Permalink
Correct max string length calculation after psf#2286
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bbugyi200 committed May 31, 2021
1 parent cf75673 commit e120f45
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/black/trans.py
Expand Up @@ -738,6 +738,17 @@ class BaseStringSplitter(StringTransformer):
* The target string is not a multiline (i.e. triple-quote) string.
"""

STRING_OPERATORS = [
token.PLUS,
token.STAR,
token.EQEQUAL,
token.NOTEQUAL,
token.LESS,
token.LESSEQUAL,
token.GREATER,
token.GREATEREQUAL,
]

@abstractmethod
def do_splitter_match(self, line: Line) -> TMatchResult:
"""
Expand Down Expand Up @@ -847,9 +858,9 @@ def _get_max_string_length(self, line: Line, string_idx: int) -> int:
p_idx -= 1

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

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

STRING_OPERATORS = [
token.PLUS,
token.STAR,
token.EQEQUAL,
token.NOTEQUAL,
token.LESS,
token.LESSEQUAL,
token.GREATER,
token.GREATEREQUAL,
]
MIN_SUBSTR_SIZE = 6
# Matches an "f-expression" (e.g. {var}) that might be found in an f-string.
RE_FEXPR = r"""
Expand Down
12 changes: 12 additions & 0 deletions tests/data/long_strings__edge_case.py
Expand Up @@ -29,6 +29,8 @@
)
return f'{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaa'
return f'{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaaa'
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"


# output
Expand Down Expand Up @@ -108,3 +110,13 @@
f"{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaa"
)
return f"{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaaa"
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"
)

0 comments on commit e120f45

Please sign in to comment.