# Buggy code
def __validate_msg(line: Line, string_idx: int) -> TResult[None]:
    """Validate (M)erge (S)tring (G)roup

    Transform-time string validation logic for __merge_string_group(...).

    Returns:
        * Ok(None), if ALL validation checks (listed below) pass.
            OR
        * Err(CannotTransform), if any of the following are true:
            - The target string is not in a string group (i.e. it has no
              adjacent strings).
            - The string group has more than one inline comment.
            - The string group has an inline comment that appears to be a pragma.
            - The set of all string prefixes in the string group is of
              length greater than one and is not equal to {"", "f"}.
            - The string group consists of raw strings.
    """
    num_of_inline_string_comments = 0
    set_of_prefixes = set()
    num_of_strings = 0
    for leaf in line.leaves[string_idx:]:
        if leaf.type != token.STRING:
            # If the string group is trailed by a comma, we count the
            # comments trailing the comma to be one of the string group's
            # comments.
            if leaf.type == token.COMMA and id(leaf) in line.comments:
                num_of_inline_string_comments += 1
            break

        if has_triple_quotes(leaf.value):
            return TErr("StringMerger does NOT merge multiline strings.")

        num_of_strings += 1
        prefix = get_string_prefix(leaf.value)
        if "r" in prefix:
            return TErr("StringMerger does NOT merge raw strings.")

        set_of_prefixes.add(prefix)

        if id(leaf) in line.comments:
            num_of_inline_string_comments += 1
            if contains_pragma_comment(line.comments[id(leaf)]):
                return TErr("Cannot merge strings which have pragma comments.")

    if num_of_strings < 2:
        return TErr(f"Not enough strings to merge (num_of_strings={num_of_strings}).")

    if num_of_inline_string_comments > 1:
        return TErr(
            f"Too many inline string comments ({num_of_inline_string_comments})."
        )

    if len(set_of_prefixes) > 1 and set_of_prefixes != {"", "f"}:
        return TErr(f"Too many different prefixes ({set_of_prefixes}).")

    return Ok(None)


# Traceback (AssertionError)
$ cat xxx.py
xxxxxxx_xxxxxx_xxxxxxx = xxx(
[
xxxxxxxxxxxx(
xxxxxx_xxxxxxx=(
'((x.xxxxxxxxx = "xxxxxx.xxxxxxxxxxxxxxxxxxxxx") || (x.xxxxxxxxx = "xxxxxxxxxxxx")) &amp;&amp; '
# xxxxx xxxxxxxxxxxx xxxx xxx (xxxxxxxxxxxxxxxx) xx x xxxxxxxxx xx xxxxxx.
"(x.xxxxxxxxxxxx.xxx != "
'"xxx:xxx:xxx::xxxxxxxxxxxx:xxxxxxx-xxxx/xxxxxxxxxxx/xxxxxxxxxxxxxxxxx") &amp;&amp; '
)
)
]
)
$ black -v xxx.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 918, in format_file_contents
assert_stable(src_contents, dst_contents, mode=mode)
File "/Users/jelle/py/black/src/black/__init__.py", line 6076, in assert_stable
newdst = format_str(dst, 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 2978, in do_transform
msg_result = self.__merge_string_group(new_line, string_idx)
File "/Users/jelle/py/black/src/black/__init__.py", line 3164, in __merge_string_group
new_line.append(string_leaf)
File "/Users/jelle/py/black/src/black/__init__.py", line 1421, in append
leaf.prefix += whitespace(
File "/Users/jelle/py/black/src/black/__init__.py", line 2152, in whitespace
assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
AssertionError: INTERNAL ERROR: hand-made leaf without parent: Leaf(STRING, '\'(x.xxxxxxxxxxxx.xxx != "xxx:xxx:xxx::xxxxxxxxxxxx:xxxxxxx-xxxx/xxxxxxxxxxx/xxxxxxxxxxxxxxxxx") &amp;&amp; \'')
error: cannot format xxx.py: INTERNAL ERROR: hand-made leaf without parent: Leaf(STRING, '\'(x.xxxxxxxxxxxx.xxx != "xxx:xxx:xxx::xxxxxxxxxxxx:xxxxxxx-xxxx/xxxxxxxxxxx/xxxxxxxxxxxxxxxxx") &amp;&amp; \'')
Oh no! 💥 💔 💥
1 file failed to reformat.

# Fixed code
def __validate_msg(line: Line, string_idx: int) -> TResult[None]:
    """Validate (M)erge (S)tring (G)roup

    Transform-time string validation logic for __merge_string_group(...).

    Returns:
        * Ok(None), if ALL validation checks (listed below) pass.
            OR
        * Err(CannotTransform), if any of the following are true:
            - The target string group does not contain ANY stand-alone comments.
            - The target string is not in a string group (i.e. it has no
              adjacent strings).
            - The string group has more than one inline comment.
            - The string group has an inline comment that appears to be a pragma.
            - The set of all string prefixes in the string group is of
              length greater than one and is not equal to {"", "f"}.
            - The string group consists of raw strings.
    """
    # We first check for "inner" stand-alone comments (i.e. stand-alone
    # comments that have a string leaf before them AND after them).
    for inc in [1, -1]:
        i = string_idx
        found_sa_comment = False
        is_valid_index = is_valid_index_factory(line.leaves)
        while is_valid_index(i) and line.leaves[i].type in [
            token.STRING,
            STANDALONE_COMMENT,
        ]:
            if line.leaves[i].type == STANDALONE_COMMENT:
                found_sa_comment = True
            elif found_sa_comment:
                return TErr(
                    "StringMerger does NOT merge string groups which contain "
                    "stand-alone comments."
                )

            i += inc

    num_of_inline_string_comments = 0
    set_of_prefixes = set()
    num_of_strings = 0
    for leaf in line.leaves[string_idx:]:
        if leaf.type != token.STRING:
            # If the string group is trailed by a comma, we count the
            # comments trailing the comma to be one of the string group's
            # comments.
            if leaf.type == token.COMMA and id(leaf) in line.comments:
                num_of_inline_string_comments += 1
            break

        if has_triple_quotes(leaf.value):
            return TErr("StringMerger does NOT merge multiline strings.")

        num_of_strings += 1
        prefix = get_string_prefix(leaf.value)
        if "r" in prefix:
            return TErr("StringMerger does NOT merge raw strings.")

        set_of_prefixes.add(prefix)

        if id(leaf) in line.comments:
            num_of_inline_string_comments += 1
            if contains_pragma_comment(line.comments[id(leaf)]):
                return TErr("Cannot merge strings which have pragma comments.")

    if num_of_strings < 2:
        return TErr(f"Not enough strings to merge (num_of_strings={num_of_strings}).")

    if num_of_inline_string_comments > 1:
        return TErr(
            f"Too many inline string comments ({num_of_inline_string_comments})."
        )

    if len(set_of_prefixes) > 1 and set_of_prefixes != {"", "f"}:
        return TErr(f"Too many different prefixes ({set_of_prefixes}).")

    return Ok(None)