Answer stability, product-owner and architecture round 3: resolve every place in order and count the dropped ones once, let a full path win, name unparsed files and unscored runs below the table, show chance and constant metrics, leave sweeps of more than 30 files out of change spread, count tuple and loop writes and self recursion, and say what a missing history or folder means

This commit is contained in:
2026-10-08 23:04:12 +02:00
parent 1fca6137c2
commit 527aef3495
6 changed files with 135 additions and 65 deletions
+22 -6
View File
@@ -24,11 +24,11 @@ def measured(path: str) -> bool:
def python_units(path: str, source: str) -> list[Unit] | Failure:
"""Each module-level function and each method in source, with its metrics."""
"""Each module-level function and each method of a module-level class in source, with its metrics."""
try:
tree = ast.parse(source)
except (SyntaxError, ValueError) as failure:
return Failure(f"could not parse {path} on Python {sys.version_info[0]}.{sys.version_info[1]}: {failure}")
return Failure(f"{path} does not parse on Python {sys.version_info[0]}.{sys.version_info[1]}: {failure}")
units = []
for node in tree.body:
if isinstance(node, FUNCTION):
@@ -58,8 +58,14 @@ def shared_writes(cls: ast.ClassDef) -> collections.Counter[str]:
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 []
targets = node.targets
elif isinstance(node, ast.AugAssign | ast.AnnAssign | ast.For | ast.AsyncFor | ast.comprehension):
targets = [node.target]
elif isinstance(node, ast.withitem) and node.optional_vars:
targets = [node.optional_vars]
else:
return []
return [leaf for target in targets for leaf in ast.walk(target) if isinstance(leaf, ast.Attribute)]
# 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.
@@ -86,8 +92,18 @@ def score(node: ast.AST, nesting: int, name: str) -> int:
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], 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)
return (isinstance(node, ast.BoolOp) or called(node) == name) + inside(list(ast.iter_child_nodes(node)), nesting, name)
def called(node: ast.AST) -> str | None:
"""The function a call names, or the method it calls on self."""
if not isinstance(node, ast.Call):
return None
if isinstance(node.func, ast.Name):
return node.func.id
if isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast.Name) and node.func.value.id == "self":
return node.func.attr
return None
def inside(nodes: list, nesting: int, name: str) -> int: