Repository: psf/black
File: src/black/__init__.py (Python)

Code before:

        if line.magic_trailing_comma and penultimate.type == token.COMMA:
            # The rightmost non-omitted bracket pair is the one we want to explode on.
            return True

        if _can_omit_closing_paren(line, last=last, line_length=line_length):
            return True

    return False


def _can_omit_opening_paren(line: Line, *, first: Leaf, line_length: int) -> bool:
    """See `can_omit_invisible_parens`."""
    remainder = False
    length = 4 * line.depth
    _index = -1
    for _index, leaf, leaf_length in enumerate_with_length(line):
        if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
            remainder = True
        if remainder:
            length += leaf_length
            if length > line_length:
                break

            if leaf.type in OPENING_BRACKETS:
                # There are brackets we can further split on.
                remainder = False

    else:
        # checked the entire string and line length wasn't exceeded
        if len(line.leaves) == _index + 1:
            return True

    return False


def _can_omit_closing_paren(line: Line, *, last: Leaf, line_length: int) -> bool:
    """See `can_omit_invisible_parens`."""
    length = 4 * line.depth
    seen_other_brackets = False
    for _index, leaf, leaf_length in enumerate_with_length(line):
        length += leaf_length
        if leaf is last.opening_bracket:
            if seen_other_brackets or length <= line_length:
                return True

        elif leaf.type in OPENING_BRACKETS:
            # There are brackets we can further split on.
            seen_other_brackets = True

    return False

Review comment (refactor):
It may be possible to remove this conditional check block completely, with only a minor impact on the formatter's behaviour.  See https://github.com/jayaddison/black/commit/1096bc908d56a6013c82f626f08f2915a38ecd32 for a possible change (cc @JelleZijlstra)

I'm not including that commit here yet, since I think it'd be good for this refactoring-focused pull request to maintain near-zero change in formatter behaviour.  In practice I think there are small logical changes here, but I hope they're extremely minimal.

Code after:
            penultimate, last = last_two_except(line.leaves, omit=omit_on_explode)
        except LookupError:
            # Turns out we'd omit everything.  We cannot skip the optional parentheses.
            return False

    if (
        last.type == token.RPAR
        or last.type == token.RBRACE
        or (
            # don't use indexing for omitting optional parentheses;
            # it looks weird
            last.type == token.RSQB
            and last.parent
            and last.parent.type != syms.trailer
        )
    ):
        if penultimate.type in OPENING_BRACKETS:
            # Empty brackets don't help.
            return False

        if is_multiline_string(first):
            # Additional wrapping of a multiline string in this situation is
            # unnecessary.
            return True

        if line.magic_trailing_comma and penultimate.type == token.COMMA:
            # The rightmost non-omitted bracket pair is the one we want to explode on.
            return True

        if _can_omit_closing_paren(line, last=last, line_length=line_length):
            return True

    return False


def _can_omit_opening_paren(line: Line, *, first: Leaf, line_length: int) -> bool:
    """See `can_omit_invisible_parens`."""
    remainder = False
    length = 4 * line.depth
    _index = -1
    for _index, leaf, leaf_length in enumerate_with_length(line):
        if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
            remainder = True
        if remainder:
            length += leaf_length
            if length > line_length:
                break

            if leaf.type in OPENING_BRACKETS:
                # There are brackets we can further split on.
                remainder = False