From 2e476f0a4468585af7eadd36320195e92fda2d45 Mon Sep 17 00:00:00 2001 From: Bryan Bugyi Date: Thu, 10 Sep 2020 12:24:01 -0400 Subject: [PATCH] Fix unstable subscript assignment string wrapping (#1678) Fixes #1598 --- src/black/__init__.py | 7 +++++-- tests/data/long_strings__regression.py | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 3753d5fca8d..bfb77126218 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -3507,9 +3507,12 @@ def __get_max_string_length(self, line: Line, string_idx: int) -> int: # WMA4 a single space. offset += 1 - # WMA4 the lengths of any leaves that came before that space. - for leaf in LL[: p_idx + 1]: + # WMA4 the lengths of any leaves that came before that space, + # but after any closing bracket before that space. + for leaf in reversed(LL[: p_idx + 1]): offset += len(str(leaf)) + if leaf.type in CLOSING_BRACKETS: + break if is_valid_index(string_idx + 1): N = LL[string_idx + 1] diff --git a/tests/data/long_strings__regression.py b/tests/data/long_strings__regression.py index 1c82613d410..8290a4cbc1c 100644 --- a/tests/data/long_strings__regression.py +++ b/tests/data/long_strings__regression.py @@ -349,6 +349,10 @@ def xxxxxxx_xxxxxx(xxxx): x.xxxxx.xxxxxx.xxxxx.xxxx, ], ("xxxxxxxxxxx xxxxxxx xxxx (xxxxxx xxxx) %x xxx xxxxx" % xxxxxxx_xxxx) +value.__dict__[ + key +] = "test" # set some Thrift field to non-None in the struct aa bb cc dd ee + # output @@ -784,3 +788,8 @@ def xxxxxxx_xxxxxx(xxxx): ], ( "xxxxxxxxxxx xxxxxxx xxxx (xxxxxx xxxx) %x xxx xxxxx" % xxxxxxx_xxxx ) + + +value.__dict__[ + key +] = "test" # set some Thrift field to non-None in the struct aa bb cc dd ee