From e120f45a452eedc26566df08d7946399c7198694 Mon Sep 17 00:00:00 2001 From: Bryan Bugyi Date: Mon, 31 May 2021 12:21:47 -0400 Subject: [PATCH 1/3] Correct max string length calculation after #2286 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. --- src/black/trans.py | 27 ++++++++++++++------------- tests/data/long_strings__edge_case.py | 12 ++++++++++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/black/trans.py b/src/black/trans.py index fd0de727620..b30e9993e73 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -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: """ @@ -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`]. @@ -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""" diff --git a/tests/data/long_strings__edge_case.py b/tests/data/long_strings__edge_case.py index 6919db5a80b..0cf5db4c39b 100644 --- a/tests/data/long_strings__edge_case.py +++ b/tests/data/long_strings__edge_case.py @@ -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 @@ -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" +) From f2ccbf5b5788528e4d35850f4c77a35d570aed59 Mon Sep 17 00:00:00 2001 From: Bryan Bugyi Date: Mon, 31 May 2021 12:34:35 -0400 Subject: [PATCH 2/3] [9999] Add percent symbol as "string operator" --- src/black/trans.py | 11 ++++++----- tests/data/long_strings__edge_case.py | 7 +++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/black/trans.py b/src/black/trans.py index b30e9993e73..bc6e93b01b4 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -739,14 +739,15 @@ class BaseStringSplitter(StringTransformer): """ STRING_OPERATORS = [ - token.PLUS, - token.STAR, token.EQEQUAL, - token.NOTEQUAL, - token.LESS, - token.LESSEQUAL, token.GREATER, token.GREATEREQUAL, + token.LESS, + token.LESSEQUAL, + token.NOTEQUAL, + token.PERCENT, + token.PLUS, + token.STAR, ] @abstractmethod diff --git a/tests/data/long_strings__edge_case.py b/tests/data/long_strings__edge_case.py index 0cf5db4c39b..07c27537191 100644 --- a/tests/data/long_strings__edge_case.py +++ b/tests/data/long_strings__edge_case.py @@ -31,6 +31,7 @@ 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 @@ -120,3 +121,9 @@ < "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 +) From d00a7293b1a0769ec2556e7a6af11a711278abb2 Mon Sep 17 00:00:00 2001 From: Bryan Bugyi Date: Mon, 31 May 2021 12:35:56 -0400 Subject: [PATCH 3/3] [9999] Add bullet to changelog --- CHANGES.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 4bf08275cf8..de67943d29f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,11 @@ # Change Log +## Unreleased + +### _Black_ + +- Correct max string length calculation when there are string operators (#2292) + ## 21.5b2 ### _Black_