Correct max string length calculation when there are string operators (#2292)
PR #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.
This commit is contained in:
parent
cf75673e1a
commit
a4e35b3149
@ -1,5 +1,11 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
### _Black_
|
||||||
|
|
||||||
|
- Correct max string length calculation when there are string operators (#2292)
|
||||||
|
|
||||||
## 21.5b2
|
## 21.5b2
|
||||||
|
|
||||||
### _Black_
|
### _Black_
|
||||||
|
@ -738,6 +738,18 @@ class BaseStringSplitter(StringTransformer):
|
|||||||
* The target string is not a multiline (i.e. triple-quote) string.
|
* The target string is not a multiline (i.e. triple-quote) string.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
STRING_OPERATORS = [
|
||||||
|
token.EQEQUAL,
|
||||||
|
token.GREATER,
|
||||||
|
token.GREATEREQUAL,
|
||||||
|
token.LESS,
|
||||||
|
token.LESSEQUAL,
|
||||||
|
token.NOTEQUAL,
|
||||||
|
token.PERCENT,
|
||||||
|
token.PLUS,
|
||||||
|
token.STAR,
|
||||||
|
]
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def do_splitter_match(self, line: Line) -> TMatchResult:
|
def do_splitter_match(self, line: Line) -> TMatchResult:
|
||||||
"""
|
"""
|
||||||
@ -847,9 +859,9 @@ def _get_max_string_length(self, line: Line, string_idx: int) -> int:
|
|||||||
p_idx -= 1
|
p_idx -= 1
|
||||||
|
|
||||||
P = LL[p_idx]
|
P = LL[p_idx]
|
||||||
if P.type == token.PLUS:
|
if P.type in self.STRING_OPERATORS:
|
||||||
# WMA4 a space and a '+' character (e.g. `+ STRING`).
|
# WMA4 a space and a string operator (e.g. `+ STRING` or `== STRING`).
|
||||||
offset += 2
|
offset += len(str(P)) + 1
|
||||||
|
|
||||||
if P.type == token.COMMA:
|
if P.type == token.COMMA:
|
||||||
# WMA4 a space, a comma, and a closing bracket [e.g. `), STRING`].
|
# WMA4 a space, a comma, and a closing bracket [e.g. `), STRING`].
|
||||||
@ -952,16 +964,6 @@ class StringSplitter(CustomSplitMapMixin, BaseStringSplitter):
|
|||||||
CustomSplit objects and add them to the custom split map.
|
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
|
MIN_SUBSTR_SIZE = 6
|
||||||
# Matches an "f-expression" (e.g. {var}) that might be found in an f-string.
|
# Matches an "f-expression" (e.g. {var}) that might be found in an f-string.
|
||||||
RE_FEXPR = r"""
|
RE_FEXPR = r"""
|
||||||
|
@ -29,6 +29,9 @@
|
|||||||
)
|
)
|
||||||
return f'{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaa'
|
return f'{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaa'
|
||||||
return f'{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaaa'
|
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"
|
||||||
|
assert "A format string: %s" % "This long string should be split at some point right close to or around hereeeeeee" != result
|
||||||
|
|
||||||
|
|
||||||
# output
|
# output
|
||||||
@ -108,3 +111,19 @@
|
|||||||
f"{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaa"
|
f"{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaa"
|
||||||
)
|
)
|
||||||
return f"{x}/b/c/d/d/d/dadfjsadjsaidoaisjdsfjaofjdfijaidfjaodfjaoifjodjafojdoajaaaaaaaaaaaa"
|
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"
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
"A format string: %s"
|
||||||
|
% "This long string should be split at some point right close to or around"
|
||||||
|
" hereeeeeee"
|
||||||
|
!= result
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user