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_function_signature(self, node: AnyFunctionDefAndLambda) -> None: for arg in functions.get_all_arguments(node): is_first_argument = ( functions.is_first_argument(node, arg.arg) and not isinstance(node, ast.Lambda) ) self.check_name( arg, arg.arg, is_first_argument=is_first_argument, )
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))
def _check_function_annotations_complexity( self, node: AnyFunctionDef, ) -> None: annotations = [ arg.annotation for arg in get_all_arguments(node) if arg.annotation is not None ] if node.returns is not None: annotations.append(node.returns) self._check_annotations_complexity(node, annotations)
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, )
def check_arguments_count(self, node: AnyFunctionDefAndLambda) -> None: """Checks the number of the arguments in a function.""" self.metrics.arguments[node] = len(functions.get_all_arguments(node))
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)