import ast import collections import dataclasses FUNCTION = (ast.FunctionDef, ast.AsyncFunctionDef) @dataclasses.dataclass(frozen=True) class Unit: path: str name: str cognitive: int length: int shared_state: int def python_units(path: str, source: str) -> list[Unit]: """Each module-level function and each method in source, with its metrics; a source that does not parse has none.""" try: tree = ast.parse(source) except (SyntaxError, ValueError): return [] units = [] for node in tree.body: if isinstance(node, FUNCTION): units.append(unit(path, node, 0)) elif isinstance(node, ast.ClassDef): shared = shared_writes(node) units += [unit(path, method, shared[method.name]) for method in node.body if isinstance(method, FUNCTION)] return units def unit(path: str, node: ast.FunctionDef | ast.AsyncFunctionDef, shared: int) -> Unit: globals_written = sum(len(statement.names) for statement in ast.walk(node) if isinstance(statement, ast.Global | ast.Nonlocal)) return Unit(path, node.name, cognitive(node), (node.end_lineno or node.lineno) - node.lineno + 1, shared + globals_written) def shared_writes(cls: ast.ClassDef) -> collections.Counter[str]: """For each method, how many of the attributes it writes on self another method of the class writes too.""" writers = collections.defaultdict(set) for method in cls.body: if isinstance(method, FUNCTION): for node in ast.walk(method): for target in written(node): if isinstance(target, ast.Attribute) and isinstance(target.value, ast.Name) and target.value.id == "self": writers[target.attr].add(method.name) return collections.Counter(method for methods in writers.values() if len(methods) > 1 for method in methods) def written(node: ast.AST) -> list[ast.expr]: if isinstance(node, ast.Assign): return node.targets return [node.target] if isinstance(node, ast.AugAssign | ast.AnnAssign) else [] # SonarSource's "Cognitive Complexity" white paper (G. Ann Campbell, 2017), read for Python: a comprehension counts as a loop and each of its `if` clauses as a condition. def cognitive(function: ast.FunctionDef | ast.AsyncFunctionDef) -> int: counter = Cognitive(function.name) counter.within(function.body, 0) return counter.score class Cognitive(ast.NodeVisitor): def __init__(self, name: str) -> None: self.name = name self.nesting = 0 self.score = 0 def within(self, nodes: list, deeper: int = 1) -> None: self.nesting += deeper for node in nodes: self.visit(node) self.nesting -= deeper def structure(self) -> None: self.score += 1 + self.nesting def visit_If(self, node: ast.If) -> None: self.structure() self.branch(node) def branch(self, node: ast.If) -> None: self.visit(node.test) self.within(node.body) if len(node.orelse) == 1 and isinstance(node.orelse[0], ast.If): self.score += 1 self.branch(node.orelse[0]) elif node.orelse: self.score += 1 self.within(node.orelse) def loop(self, node: ast.For | ast.AsyncFor | ast.While, head: ast.expr) -> None: self.structure() self.visit(head) self.within(node.body) if node.orelse: self.score += 1 self.within(node.orelse) def visit_For(self, node: ast.For | ast.AsyncFor) -> None: self.loop(node, node.iter) visit_AsyncFor = visit_For def visit_While(self, node: ast.While) -> None: self.loop(node, node.test) def visit_Try(self, node: ast.Try | ast.TryStar) -> None: self.within(node.body, 0) for handler in node.handlers: self.structure() self.within(handler.body) self.within(node.orelse, 0) self.within(node.finalbody, 0) visit_TryStar = visit_Try def visit_Match(self, node: ast.Match) -> None: self.structure() self.visit(node.subject) for case in node.cases: self.within(case.body) def visit_IfExp(self, node: ast.IfExp) -> None: self.structure() self.within([node.test, node.body, node.orelse]) def visit_BoolOp(self, node: ast.BoolOp) -> None: self.score += 1 self.generic_visit(node) def comprehension(self, node: ast.ListComp | ast.SetComp | ast.DictComp | ast.GeneratorExp) -> None: for generator in node.generators: self.structure() self.score += len(generator.ifs) self.within(list(ast.iter_child_nodes(node))) visit_DictComp = visit_GeneratorExp = visit_ListComp = visit_SetComp = comprehension def visit_FunctionDef(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> None: self.within(node.body) visit_AsyncFunctionDef = visit_FunctionDef def visit_Lambda(self, node: ast.Lambda) -> None: self.within([node.body]) def visit_Call(self, node: ast.Call) -> None: if isinstance(node.func, ast.Name) and node.func.id == self.name: self.score += 1 self.generic_visit(node)