tonybaloney / perflint

Python Linter for performance anti patterns
MIT License
663 stars 10 forks source link

Comprehensions can only have If with no Elif/Else #20

Closed tonybaloney closed 2 years ago

tonybaloney commented 2 years ago

This is being raised as a potential comprehension, which is impossible


def should_not_be_a_list_comprehension(args):
    """Internal helper for get_args."""
    res = []
    for arg in args:
        if not isinstance(arg, tuple):
            res.append(arg)
        elif is_callable_type(arg[0]):
            if len(arg) == 2:
                res.append(Callable[[], arg[1]])
            elif arg[1] is Ellipsis:
                res.append(Callable[..., arg[2]])
            else:
                res.append(Callable[list(arg[1:-1]), arg[-1]])
        else:
            res.append(type(arg[0]).__getitem__(arg[0], _eval_args(arg[1:])))
    return tuple(res)