# Buggy code
def mark(self, leaf: Leaf) -> None:
    """Mark `leaf` with bracket-related metadata. Keep track of delimiters.

    All leaves receive an int `bracket_depth` field that stores how deep
    within brackets a given leaf is. 0 means there are no enclosing brackets
    that started on this line.

    If a leaf is itself a closing bracket, it receives an `opening_bracket`
    field that it forms a pair with. This is a one-directional link to
    avoid reference cycles.

    If a leaf is a delimiter (a token on which Black can split the line if
    needed) and it's on depth 0, its `id()` is stored in the tracker's
    `delimiters` field.
    """
    if leaf.type == token.COMMENT:
        return

    self.maybe_decrement_after_for_loop_variable(leaf)
    self.maybe_decrement_after_lambda_arguments(leaf)
    if leaf.type in CLOSING_BRACKETS:
        self.depth -= 1
        opening_bracket = self.bracket_match.pop((self.depth, leaf.type))
        leaf.opening_bracket = opening_bracket
        if not leaf.value:
            self.invisible.append(leaf)
    leaf.bracket_depth = self.depth
    if self.depth == 0:
        delim = is_split_before_delimiter(leaf, self.previous)
        if delim and self.previous is not None:
            self.delimiters[id(self.previous)] = delim
        else:
            delim = is_split_after_delimiter(leaf, self.previous)
            if delim:
                self.delimiters[id(leaf)] = delim
    if leaf.type in OPENING_BRACKETS:
        self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
        self.depth += 1
        if not leaf.value:
            self.invisible.append(leaf)
    self.previous = leaf
    self.maybe_increment_lambda_arguments(leaf)
    self.maybe_increment_for_loop_variable(leaf)


# 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 mark(self, leaf: Leaf) -> None:
    """Mark `leaf` with bracket-related metadata. Keep track of delimiters.

    All leaves receive an int `bracket_depth` field that stores how deep
    within brackets a given leaf is. 0 means there are no enclosing brackets
    that started on this line.

    If a leaf is itself a closing bracket, it receives an `opening_bracket`
    field that it forms a pair with. This is a one-directional link to
    avoid reference cycles.

    If a leaf is a delimiter (a token on which Black can split the line if
    needed) and it's on depth 0, its `id()` is stored in the tracker's
    `delimiters` field.
    """
    if leaf.type == token.COMMENT:
        return

    self.maybe_decrement_after_for_loop_variable(leaf)
    self.maybe_decrement_after_lambda_arguments(leaf)
    if leaf.type in CLOSING_BRACKETS:
        self.depth -= 1
        try:
            opening_bracket = self.bracket_match.pop((self.depth, leaf.type))
        except KeyError as e:
            raise BracketMatchError(
                "Unable to match a closing bracket to the following opening"
                f" bracket: {leaf}"
            ) from e
        leaf.opening_bracket = opening_bracket
        if not leaf.value:
            self.invisible.append(leaf)
    leaf.bracket_depth = self.depth
    if self.depth == 0:
        delim = is_split_before_delimiter(leaf, self.previous)
        if delim and self.previous is not None:
            self.delimiters[id(self.previous)] = delim
        else:
            delim = is_split_after_delimiter(leaf, self.previous)
            if delim:
                self.delimiters[id(leaf)] = delim
    if leaf.type in OPENING_BRACKETS:
        self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
        self.depth += 1
        if not leaf.value:
            self.invisible.append(leaf)
    self.previous = leaf
    self.maybe_increment_lambda_arguments(leaf)
    self.maybe_increment_for_loop_variable(leaf)