# Buggy code
def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
    LL = line.leaves

    string_parser = StringParser()
    rpar_idx = string_parser.parse(LL, string_idx)

    for leaf in (LL[string_idx - 1], LL[rpar_idx]):
        if line.comments_after(leaf):
            yield TErr(
                "Will not strip parentheses which have comments attached to them."
            )

    new_line = line.clone()
    new_line.comments = line.comments.copy()
    append_leaves(new_line, line, LL[: string_idx - 1])

    string_leaf = Leaf(token.STRING, LL[string_idx].value)
    LL[string_idx - 1].remove()
    replace_child(LL[string_idx], string_leaf)
    new_line.append(string_leaf)

    append_leaves(new_line, line, LL[string_idx + 1 : rpar_idx] + LL[rpar_idx + 1 :])

    LL[rpar_idx].remove()

    yield Ok(new_line)


# Traceback (KeyError)
$ cat author.py
class xxxxxxxxxxxxxxxxxxxxx(xxxx.xxxxxxxxxxxxx):
def xxxxxxx_xxxxxx(xxxx):
assert xxxxxxx_xxxx in [
x.xxxxx.xxxxxx.xxxxx.xxxxxx,
x.xxxxx.xxxxxx.xxxxx.xxxx,
], ("xxxxxxxxxxx xxxxxxx xxxx (xxxxxx xxxx) %x xxx xxxxx" % xxxxxxx_xxxx)
$ black -v author.py
Using configuration from /Users/jelle/py/black/pyproject.toml.
Traceback (most recent call last):
File "/Users/jelle/py/black/src/black/__init__.py", line 648, in reformat_one
if changed is not Changed.CACHED and format_file_in_place(
File "/Users/jelle/py/black/src/black/__init__.py", line 791, in format_file_in_place
dst_contents = format_file_contents(src_contents, fast=fast, mode=mode)
File "/Users/jelle/py/black/src/black/__init__.py", line 912, in format_file_contents
dst_contents = format_str(src_contents, mode=mode)
File "/Users/jelle/py/black/src/black/__init__.py", line 977, in format_str
for line in transform_line(
File "/Users/jelle/py/black/src/black/__init__.py", line 2731, in transform_line
result.extend(
File "/Users/jelle/py/black/src/black/__init__.py", line 2725, in transform_line
for transformed_line in transform(line, features):
File "/Users/jelle/py/black/src/black/__init__.py", line 2834, in __call__
for line_result in self.do_transform(line, string_idx):
File "/Users/jelle/py/black/src/black/__init__.py", line 3315, in do_transform
append_leaves(new_line, line, LL[: string_idx - 1])
File "/Users/jelle/py/black/src/black/__init__.py", line 4601, in append_leaves
new_line.append(new_leaf)
File "/Users/jelle/py/black/src/black/__init__.py", line 1425, in append
self.bracket_tracker.mark(leaf)
File "/Users/jelle/py/black/src/black/__init__.py", line 1291, in mark
opening_bracket = self.bracket_match.pop((self.depth, leaf.type))
KeyError: (-1, 10)
error: cannot format author.py: (-1, 10)
Oh no! 💥 💔 💥
1 file failed to reformat.

# Fixed code
def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
    LL = line.leaves

    string_parser = StringParser()
    rpar_idx = string_parser.parse(LL, string_idx)

    for leaf in (LL[string_idx - 1], LL[rpar_idx]):
        if line.comments_after(leaf):
            yield TErr(
                "Will not strip parentheses which have comments attached to them."
            )
            return

    new_line = line.clone()
    new_line.comments = line.comments.copy()
    try:
        append_leaves(new_line, line, LL[: string_idx - 1])
    except BracketMatchError:
        # HACK: I believe there is currently a bug somewhere in
        # right_hand_split() that is causing brackets to not be tracked
        # properly by a shared BracketTracker.
        append_leaves(new_line, line, LL[: string_idx - 1], preformatted=True)

    string_leaf = Leaf(token.STRING, LL[string_idx].value)
    LL[string_idx - 1].remove()
    replace_child(LL[string_idx], string_leaf)
    new_line.append(string_leaf)

    append_leaves(new_line, line, LL[string_idx + 1 : rpar_idx] + LL[rpar_idx + 1 :])

    LL[rpar_idx].remove()

    yield Ok(new_line)