Repository: psf/black

Issue:
Unstable formatting on subscripted assignment + string + comment
```
$ cat test_ranking_store.py 
value.__dict__[
    key
] = "test"  # set some Thrift field to non-None in the struct aa bb cc dd ee
$ black -v test_ranking_store.py 
Using configuration from /Users/jelle/py/black/pyproject.toml.
error: cannot format test_ranking_store.py: INTERNAL ERROR: Black produced different code on the second pass of the formatter.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: /var/folders/wx/zkst6jdx1zg_s32d23gz4_0r0000gn/T/blk_mf5ru27i.log
Oh no! 💥 💔 💥
1 file failed to reformat.
$ cat  /var/folders/wx/zkst6jdx1zg_s32d23gz4_0r0000gn/T/blk_mf5ru27i.log
--- source
+++ first pass
@@ -1,3 +1,5 @@
 value.__dict__[
     key
-] = "test"  # set some Thrift field to non-None in the struct aa bb cc dd ee
+] = (  # set some Thrift field to non-None in the struct aa bb cc dd ee
+    "test"
+)
--- first pass
+++ second pass
@@ -1,5 +1,3 @@
 value.__dict__[
     key
-] = (  # set some Thrift field to non-None in the struct aa bb cc dd ee
-    "test"
-)
+] = "test"  # set some Thrift field to non-None in the struct aa bb cc dd ee
```
Unstable formatting on subscripted assignment + string + comment
```
$ cat test_ranking_store.py 
value.__dict__[
    key
] = "test"  # set some Thrift field to non-None in the struct aa bb cc dd ee
$ black -v test_ranking_store.py 
Using configuration from /Users/jelle/py/black/pyproject.toml.
error: cannot format test_ranking_store.py: INTERNAL ERROR: Black produced different code on the second pass of the formatter.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: /var/folders/wx/zkst6jdx1zg_s32d23gz4_0r0000gn/T/blk_mf5ru27i.log
Oh no! 💥 💔 💥
1 file failed to reformat.
$ cat  /var/folders/wx/zkst6jdx1zg_s32d23gz4_0r0000gn/T/blk_mf5ru27i.log
--- source
+++ first pass
@@ -1,3 +1,5 @@
 value.__dict__[
     key
-] = "test"  # set some Thrift field to non-None in the struct aa bb cc dd ee
+] = (  # set some Thrift field to non-None in the struct aa bb cc dd ee
+    "test"
+)
--- first pass
+++ second pass
@@ -1,5 +1,3 @@
 value.__dict__[
     key
-] = (  # set some Thrift field to non-None in the struct aa bb cc dd ee
-    "test"
-)
+] = "test"  # set some Thrift field to non-None in the struct aa bb cc dd ee
```


Discussion:
`git bisect` points to 544ea9c217cad9459d7b60665db787e94b52f93d as the first bad commit, from PR https://github.com/psf/black/pull/1132 "Improve String Handling".
This is now working with `master`

```console
$ black --check test_ranking_store.py
All done! ✨ 🍰 ✨
1 file would be left unchanged.
```

`git bisect` points to 544ea9c217cad9459d7b60665db787e94b52f93d as the first bad commit, from PR https://github.com/psf/black/pull/1132 "Improve String Handling".
This is now working with `master`

```console
$ black --check test_ranking_store.py
All done! ✨ 🍰 ✨
1 file would be left unchanged.
```


Resolving patch:
diff --git a/src/black/__init__.py b/src/black/__init__.py
--- 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]


Test patch:
diff --git a/tests/data/long_strings__regression.py b/tests/data/long_strings__regression.py
--- 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