diff --git a/bin/metric_bench/bench.py b/bin/metric_bench/bench.py index 70f54d8d..038df020 100644 --- a/bin/metric_bench/bench.py +++ b/bin/metric_bench/bench.py @@ -34,7 +34,7 @@ class RunScores: def co_changed(log: str) -> dict[str, int]: - """For each file, how many other files changed in a commit with it, commits of more than LARGEST_CHANGESET files left out; log is `git log --format=format:@@ --name-only`.""" + """For each file, how many other files changed in a commit with it, commits of more than LARGEST_CHANGESET files left out; log is `git log --format=tformat:@@ --name-only`.""" partners: dict[str, set[str]] = collections.defaultdict(set) for commit in log.split("@@\n"): files = set(filter(None, commit.split("\n"))) diff --git a/bin/metric_bench/main.py b/bin/metric_bench/main.py index c69199f9..cf10f700 100644 --- a/bin/metric_bench/main.py +++ b/bin/metric_bench/main.py @@ -68,7 +68,7 @@ def read_history(given: pathlib.Path | None) -> tuple[str, pathlib.Path, str] | if isinstance(found, Failure): return found history = given or pathlib.Path(found.strip(), "docs", "comprehension-history.md") - if not history.exists(): + if not given and not history.exists(): return Failure(f"no comprehension history at {history}; larv-review writes one at a project's first paired ruling or scoring run") try: return found.strip(), history, history.read_text(encoding="utf-8") @@ -117,7 +117,7 @@ def run_scores(run: ScoringRun, top: str) -> tuple[RunScores | None, list[str]] unparsed.append(found.reason) else: units += found - log = git(["-c", "core.quotePath=false", "log", "--no-merges", "--format=format:@@", "--name-only", run.commit], top) + log = git(["-c", "core.quotePath=false", "log", "--no-merges", "--format=tformat:@@", "--name-only", run.commit], top) if isinstance(log, Failure): return log places, dropped = resolved(run.places, tracked) diff --git a/bin/metric_bench/python_units.py b/bin/metric_bench/python_units.py index fdde70dd..c7ca8e35 100644 --- a/bin/metric_bench/python_units.py +++ b/bin/metric_bench/python_units.py @@ -65,7 +65,16 @@ def written(node: ast.AST) -> list[ast.expr]: targets = [node.optional_vars] else: return [] - return [leaf for target in targets for leaf in ast.walk(target) if isinstance(leaf, ast.Attribute)] + return [leaf for target in targets for leaf in assigned(target)] + + +def assigned(target: ast.expr) -> list[ast.expr]: + """What a target writes: each part of an unpacking, and for `x[i] = …` the x.""" + if isinstance(target, ast.Tuple | ast.List): + return [leaf for element in target.elts for leaf in assigned(element)] + if isinstance(target, ast.Starred | ast.Subscript): + return assigned(target.value) + return [target] # 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. diff --git a/bin/test_metric_bench.py b/bin/test_metric_bench.py index 57ecad0a..e922279c 100644 --- a/bin/test_metric_bench.py +++ b/bin/test_metric_bench.py @@ -102,8 +102,9 @@ class Units(unittest.TestCase): def rename(self): self.name, self.other = "y", 1 + self.buf[self.size] = 0 """)) - self.assertEqual(units, [Unit("a.py", "bump", 0, 3, 1), Unit("a.py", "__init__", 0, 2, 1), Unit("a.py", "grow", 0, 3, 2), Unit("a.py", "rename", 0, 2, 1)]) + self.assertEqual(units, [Unit("a.py", "bump", 0, 3, 1), Unit("a.py", "__init__", 0, 2, 1), Unit("a.py", "grow", 0, 3, 2), Unit("a.py", "rename", 0, 3, 1)]) def test_measures_python_files_outside_the_tests(self): self.assertEqual([path for path in ["a.py", "bin/test_a.py", "a_test.py", "tests/b.py", "e2e-tests/c.py", "README.md"] if measured(path)], ["a.py", "e2e-tests/c.py"]) @@ -224,7 +225,9 @@ class BlackBox(unittest.TestCase): def test_exits_1_naming_a_history_it_cannot_read_and_unknown_arguments(self): top = self.project("") - self.assertIn("no comprehension history at", self.run_tool(top, str(pathlib.Path(top, "missing.md"))).stderr) + self.assertIn("could not read", self.run_tool(top, str(pathlib.Path(top, "missing.md"))).stderr) + pathlib.Path(top, "docs", "comprehension-history.md").unlink() + self.assertIn("no comprehension history at", self.run_tool(top).stderr) self.assertIn("could not read", self.run_tool(top, str(pathlib.Path(top, "gone", "x.md"))).stderr) self.assertEqual(self.run_tool(top, "--x").returncode, 1)