Answer architecture round 2: split main into read_history and bench_runs, state why score dispatches in one function and lift inside out of it, count unresolved places once, name git's reason for a skipped run, and say to call each tool by its path
This commit is contained in:
@@ -64,37 +64,39 @@ def written(node: ast.AST) -> list[ast.expr]:
|
||||
|
||||
# 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:
|
||||
return sum(score(node, 0, function.name) for node in function.body)
|
||||
return inside(function.body, 0, function.name)
|
||||
|
||||
|
||||
# One case per node type, as the white paper's table has one row per structure: split, the rules would scatter.
|
||||
def score(node: ast.AST, nesting: int, name: str) -> int:
|
||||
"""The cognitive complexity node adds at nesting depth nesting, its children included; name is the function's own, a call to it counting as recursion."""
|
||||
def inside(nodes: list, depth: int = nesting + 1) -> int:
|
||||
return sum(score(child, depth, name) for child in nodes)
|
||||
|
||||
"""The cognitive complexity node adds at depth nesting, its children included; name is the function's own, a call to it counting as recursion."""
|
||||
if isinstance(node, ast.If):
|
||||
return 1 + nesting + branches(node, nesting, name)
|
||||
if isinstance(node, ast.For | ast.AsyncFor | ast.While):
|
||||
head = node.test if isinstance(node, ast.While) else node.iter
|
||||
return 1 + nesting + score(head, nesting, name) + inside(node.body) + (1 + inside(node.orelse) if node.orelse else 0)
|
||||
return 1 + nesting + score(head, nesting, name) + inside(node.body, nesting + 1, name) + (1 + inside(node.orelse, nesting + 1, name) if node.orelse else 0)
|
||||
if isinstance(node, ast.Try | ast.TryStar):
|
||||
handlers = sum(1 + nesting + inside(handler.body) for handler in node.handlers)
|
||||
return inside(node.body, nesting) + handlers + inside(node.orelse, nesting) + inside(node.finalbody, nesting)
|
||||
handlers = sum(1 + nesting + inside(handler.body, nesting + 1, name) for handler in node.handlers)
|
||||
return inside(node.body + node.orelse + node.finalbody, nesting, name) + handlers
|
||||
if isinstance(node, ast.Match):
|
||||
return 1 + nesting + score(node.subject, nesting, name) + sum(inside(case.body) for case in node.cases)
|
||||
return 1 + nesting + score(node.subject, nesting, name) + sum(inside(case.body, nesting + 1, name) for case in node.cases)
|
||||
if isinstance(node, ast.IfExp):
|
||||
return 1 + nesting + inside([node.test, node.body, node.orelse])
|
||||
return 1 + nesting + inside([node.test, node.body, node.orelse], nesting + 1, name)
|
||||
if isinstance(node, ast.ListComp | ast.SetComp | ast.DictComp | ast.GeneratorExp):
|
||||
return sum(1 + nesting + len(generator.ifs) for generator in node.generators) + inside(list(ast.iter_child_nodes(node)))
|
||||
return sum(1 + nesting + len(generator.ifs) for generator in node.generators) + inside(list(ast.iter_child_nodes(node)), nesting + 1, name)
|
||||
if isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef | ast.Lambda):
|
||||
return inside(node.body if isinstance(node.body, list) else [node.body])
|
||||
own = 1 if isinstance(node, ast.BoolOp) or isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == name else 0
|
||||
return own + inside(list(ast.iter_child_nodes(node)), nesting)
|
||||
return inside(node.body if isinstance(node.body, list) else [node.body], nesting + 1, name)
|
||||
recursion = isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == name
|
||||
return (isinstance(node, ast.BoolOp) or recursion) + inside(list(ast.iter_child_nodes(node)), nesting, name)
|
||||
|
||||
|
||||
def inside(nodes: list, nesting: int, name: str) -> int:
|
||||
return sum(score(node, nesting, name) for node in nodes)
|
||||
|
||||
|
||||
def branches(node: ast.If, nesting: int, name: str) -> int:
|
||||
"""An if's test and bodies; an elif or else adds 1 at no extra depth."""
|
||||
total = score(node.test, nesting, name) + sum(score(child, nesting + 1, name) for child in node.body)
|
||||
total = score(node.test, nesting, name) + inside(node.body, nesting + 1, name)
|
||||
if len(node.orelse) == 1 and isinstance(node.orelse[0], ast.If):
|
||||
return total + 1 + branches(node.orelse[0], nesting, name)
|
||||
return total + (1 + sum(score(child, nesting + 1, name) for child in node.orelse) if node.orelse else 0)
|
||||
return total + (1 + inside(node.orelse, nesting + 1, name) if node.orelse else 0)
|
||||
|
||||
Reference in New Issue
Block a user