Exemplo n.º 1
0
    def _check_bound_methods(self, node: types.AnyFunctionDef) -> None:
        node_context = nodes.get_context(node)
        if not isinstance(node_context, ast.ClassDef):
            return

        if not functions.get_all_arguments(node):
            self.add_violation(
                oop.MethodWithoutArgumentsViolation(node, text=node.name),
            )

        if node.name in constants.MAGIC_METHODS_BLACKLIST:
            self.add_violation(
                oop.BadMagicMethodViolation(node, text=node.name),
            )

        is_async = isinstance(node, ast.AsyncFunctionDef)
        if is_async and access.is_magic(node.name):
            if node.name in constants.ASYNC_MAGIC_METHODS_BLACKLIST:
                self.add_violation(
                    oop.AsyncMagicMethodViolation(node, text=node.name),
                )

        self._check_useless_overwritten_methods(
            node,
            class_name=node_context.name,
        )
    def _check_implicit_primitive(self, node: ast.Lambda) -> None:
        arguments = functions.get_all_arguments(node)
        if arguments:
            # We return from this check, since `lambda` has some arguments.
            return

        with suppress(ValueError):
            return_value = ast.literal_eval(node.body)
            if return_value is not None and not return_value:
                self.add_violation(ImplicitPrimitiveViolation(node))
Exemplo n.º 3
0
    def _check_bound_methods(self, node: types.AnyFunctionDef) -> None:
        node_context = get_context(node)
        if not isinstance(node_context, ast.ClassDef):
            return

        if not get_all_arguments(node):
            self.add_violation(
                MethodWithoutArgumentsViolation(node, text=node.name), )

        if node.name in constants.MAGIC_METHODS_BLACKLIST:
            self.add_violation(BadMagicMethodViolation(node, text=node.name))
Exemplo n.º 4
0
    def check_function_signature(self, node: AnyFunctionDefAndLambda) -> None:
        arguments = functions.get_all_arguments(node)
        is_lambda = isinstance(node, ast.Lambda)
        for arg in arguments:
            should_check_argument = functions.is_first_argument(
                node,
                arg.arg,
            ) and not is_lambda

            self.check_name(
                arg,
                arg.arg,
                is_first_argument=should_check_argument,
            )
Exemplo n.º 5
0
 def visit_any_function(self, node: AnyFunctionDef) -> None:
     """Checks function parameters indentation."""
     self._check_indentation(node, functions.get_all_arguments(node))
     self.generic_visit(node)
Exemplo n.º 6
0
 def check_arguments_count(self, node: AnyFunctionDefAndLambda) -> None:
     """Checks the number of the arguments in a function."""
     self.arguments[node] = len(functions.get_all_arguments(node))