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

Code before:
StringID = int
TResult = Result[T, CannotTransform]  # (T)ransform Result
TMatchResult = TResult[Index]


def TErr(err_msg: str) -> Err[CannotTransform]:
    """(T)ransform Err

    Convenience function used when working with the TResult type.
    """
    cant_transform = CannotTransform(err_msg)
    return Err(cant_transform)


def hug_power_op(line: Line, features: Collection[Feature]) -> Iterator[Line]:
    """A transformer which normalizes spacing around power operators."""

    # Performance optimization to avoid unnecessary Leaf clones and other ops.
    for leaf in line.leaves:
        if leaf.type == token.DOUBLESTAR:
            break
    else:
        raise CannotTransform("No doublestar token was found in the line.")

    def is_simple_lookup(index: int, step: Literal[1, -1]) -> bool:
        # Brackets and parenthesises indicate calls, subscripts, etc. ...
        # basically stuff that doesn't count as "simple". Only a NAME lookup
        # or dotted lookup (eg. NAME.NAME) is OK.
        if step == -1:
            disallowed = {token.RPAR, token.RSQB}
        else:
            disallowed = {token.LPAR, token.LSQB}

        while 0 <= index < len(line.leaves):
            current = line.leaves[index]
            if current.type in disallowed:
                return False
            if current.type not in {token.NAME, token.DOT} or current.value == "for":
                # If the current token isn't disallowed, we'll assume this is simple as
                # only the disallowed tokens are semantically attached to this lookup
                # expression we're checking. Also, stop early if we hit the 'for' bit
                # of a comprehension.
                return True

            index += step

        return True

    def is_simple_operand(index: int, kind: Literal["base", "exponent"]) -> bool:
        # An operand is considered "simple" if's a NAME, a numeric CONSTANT, a simple
        # lookup (see above), with or without a preceding unary operator.

Review comment (suggestion):
```suggestion
        # Brackets and parentheses indicate calls, subscripts, etc. ...
```

Code after:
StringID = int
TResult = Result[T, CannotTransform]  # (T)ransform Result
TMatchResult = TResult[Index]


def TErr(err_msg: str) -> Err[CannotTransform]:
    """(T)ransform Err

    Convenience function used when working with the TResult type.
    """
    cant_transform = CannotTransform(err_msg)
    return Err(cant_transform)


def hug_power_op(line: Line, features: Collection[Feature]) -> Iterator[Line]:
    """A transformer which normalizes spacing around power operators."""

    # Performance optimization to avoid unnecessary Leaf clones and other ops.
    for leaf in line.leaves:
        if leaf.type == token.DOUBLESTAR:
            break
    else:
        raise CannotTransform("No doublestar token was found in the line.")

    def is_simple_lookup(index: int, step: Literal[1, -1]) -> bool:
        # Brackets and parentheses indicate calls, subscripts, etc. ...
        # basically stuff that doesn't count as "simple". Only a NAME lookup
        # or dotted lookup (eg. NAME.NAME) is OK.
        if step == -1:
            disallowed = {token.RPAR, token.RSQB}
        else:
            disallowed = {token.LPAR, token.LSQB}

        while 0 <= index < len(line.leaves):
            current = line.leaves[index]
            if current.type in disallowed:
                return False
            if current.type not in {token.NAME, token.DOT} or current.value == "for":
                # If the current token isn't disallowed, we'll assume this is simple as
                # only the disallowed tokens are semantically attached to this lookup
                # expression we're checking. Also, stop early if we hit the 'for' bit
                # of a comprehension.
                return True

            index += step

        return True

    def is_simple_operand(index: int, kind: Literal["base", "exponent"]) -> bool:
        # An operand is considered "simple" if's a NAME, a numeric CONSTANT, a simple
        # lookup (see above), with or without a preceding unary operator.