def check_io_pipes_complexity( cls, pipeline_classdef: ast.ClassDef, max_io_pipe_cyclomatic_complexity: int, max_io_pipe_cognitive_complexity: int, ) -> Generator[Tuple[int, int, str], None, None]: for pipe_funcdef in get_all_pipes_from(pipeline_classdef): if not has_any_decorator(pipe_funcdef, cls.IO_DECORATOR_NAMES): continue cyclomatic_complexity = get_cyclomatic_complexity(pipe_funcdef) if cyclomatic_complexity > max_io_pipe_cyclomatic_complexity: yield ( pipe_funcdef.lineno, pipe_funcdef.col_offset, (f'SME002 Pipe {pipe_funcdef.name} has too high cyclomatic complexity ' f'({cyclomatic_complexity} > {max_io_pipe_cyclomatic_complexity})' ), ) cognitive_complexity = get_cognitive_complexity(pipe_funcdef) if cognitive_complexity > max_io_pipe_cognitive_complexity: yield ( pipe_funcdef.lineno, pipe_funcdef.col_offset, (f'SME003 Pipe {pipe_funcdef.name} has too high cognitive complexity ' f'({cognitive_complexity} > {max_io_pipe_cognitive_complexity})' ), )
def cognitive_score(node: AnyFunctionDef) -> int: """ A thin wrapper around 3rd party dependency. We only need to be sure that our visitors API does not directly related to some 3rd party code. """ return get_cognitive_complexity(node)
def cognitive(self): if self._cognitive is None: cog = self.ast_node if not cog: raise MetricsException( 'Error while computing Cognitive complexity: no result') else: self._cognitive = get_cognitive_complexity(cog.body[0]) return self._cognitive
def run(self) -> Generator[Tuple[int, int, str, type], None, None]: funcdefs = (n for n in ast.walk(self.tree) if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef))) for funcdef in funcdefs: complexity = get_cognitive_complexity(funcdef) if complexity > self.max_cognitive_complexity: yield ( funcdef.lineno, funcdef.col_offset, f'CCR001 Cognitive complexity is too high ' f'({complexity} > {self.max_cognitive_complexity})', type(self), )
def get_code_snippet_compexity(src: str) -> int: funcdef = ast.parse(src.strip()).body[0] return get_cognitive_complexity(funcdef)