Exemplo n.º 1
0
    def _check_range_len(self, node: ast.Call) -> None:
        if not isinstance(nodes.get_parent(node), ForNodes):
            return
        if not functions.given_function_called(node, {'range'}):
            return

        args_len = len(node.args)

        is_one_arg_range = (
            args_len == 1 and
            isinstance(node.args[0], ast.Call) and
            functions.given_function_called(node.args[0], {'len'})
        )
        is_two_args_range = (
            self._is_multiple_args_range_with_len(node) and
            args_len == 2
        )
        # for three args add violation
        # only if `step` arg do not equals 1 or -1
        step_arg = args_len == 3 and operators.unwrap_unary_node(node.args[2])
        is_three_args_range = (
            self._is_multiple_args_range_with_len(node) and
            args_len == 3 and
            isinstance(step_arg, ast.Num) and
            abs(step_arg.n) == 1
        )
        if any([is_one_arg_range, is_two_args_range, is_three_args_range]):
            self.add_violation(ImplicitEnumerateViolation(node))
Exemplo n.º 2
0
    def _check_range_len(self, node: ast.Call) -> None:
        function_name = functions.given_function_called(node, {'range'})
        if not function_name:
            return

        is_one_argument_range = (len(node.args) == 1
                                 and isinstance(node.args[0], ast.Call)
                                 and functions.given_function_called(
                                     node.args[0], {'len'}))
        is_two_arguments_range = (len(node.args) in {2, 3}
                                  and isinstance(node.args[1], ast.Call)
                                  and functions.given_function_called(
                                      node.args[1], {'len'}))
        if is_one_argument_range or is_two_arguments_range:
            self.add_violation(ImplicitEnumerateViolation(node))