From cd055efd7d999421984e8b63afe53b5f9854deb9 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 3753d5f..bfb7712 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -3507,9 +3507,12 @@ class BaseStringSplitter(StringTransformer): # 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 1c82613..8290a4c 100644 --- a/tests/data/long_strings__regression.py +++ b/tests/data/long_strings__regression.py @@ -349,6 +349,10 @@ class xxxxxxxxxxxxxxxxxxxxx(xxxx.xxxxxxxxxxxxx): 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 @@ class xxxxxxxxxxxxxxxxxxxxx(xxxx.xxxxxxxxxxxxx): ], ( "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 -- 2.39.5