Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(health): do not charge a sink in a generator or iterator closure to its caller (#3003)
Aman7331a8340944
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py b/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.pyindex 9e44896c8..78246336a 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py@@ -362,6 +362,14 @@ def _is_block_loop_body_scope(return False+def _in_deferred_body(node: Node, dialect: BasePerfDialect, scope_kinds: frozenset[str]) -> bool:+ """Whether *node* sits in a body that runs when its result is iterated."""+ cur = node.parent+ while cur is not None and cur.type not in scope_kinds:+ cur = cur.parent+ return cur is not None and dialect.defers_body(cur)++def perf_pass_runs(language: str, lmap: LanguageNodeMap) -> bool:"""Whether ``_collect_perf_hits`` does anything for *language*."""return bool(lmap.call_kinds) and language in PERF_DIALECTS@@ -416,6 +424,7 @@ def _collect_perf_hits(loop_kinds = lmap.loop_kindsfn_kinds = lmap.function_kindslambda_kinds = lmap.lambda_kinds+ scope_kinds = fn_kinds | lambda_kindsasync_fn_kinds = lmap.async_function_kindsbare_call_wrapper_kinds = lmap.bare_call_wrapper_kinds# Block-iteration loops (Ruby ``items.each do … end``): only pay for the@@ -667,6 +676,7 @@ def _collect_perf_hits(and misc[1] is Noneand kind in _HOT_PATH_SINK_KINDSand method not in dialect.hot_path_excluded_methods+ and not _in_deferred_body(call_node, dialect, scope_kinds)):# An inherently-blocking (non-awaited subprocess / fs /# sync-network) sink outside any loop. Noisy everywhere,diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/base.pyindex 71b62e11f..a8f1a4967 100644--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py@@ -226,6 +226,11 @@ class BasePerfDialect:has_db_import=has_db_import,)+ def defers_body(self, fn: Node) -> bool:+ """*fn*'s body runs when its result is iterated, not when it is called+ (a generator, or an iterator closure that takes a ``yield`` callback)."""+ return False+def runs_in_place(self, closure: Node) -> bool:"""*closure* runs to completion where it is written, like the loop bodyaround it (``await limit(() => call())``), rather than being stored for later."""diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/go.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/go.pyindex 76bd3b250..b00481b8b 100644--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/go.py+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/go.py@@ -236,6 +236,20 @@ class GoPerfDialect(BasePerfDialect):return "lock_in_loop"return None+ def defers_body(self, fn: Node) -> bool:+ """A ``func(yield func(...) bool)`` literal is an ``iter.Seq`` body: it runs+ when the caller ranges over it, not when the function returning it is called."""+ if fn.type != "func_literal":+ return False+ params = fn.child_by_field_name("parameters")+ if params is None:+ return False+ return any(+ decl.type == "parameter_declaration"+ and any(c.type == "identifier" and c.text == b"yield" for c in decl.children)+ for decl in params.children+ )+def loop_iterable_name(self, node: Node) -> str | None:"""The collection a ``for _, x := range coll`` loop iterates (``coll``)."""if node.type != "for_statement":diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/python.pyindex d43a601fc..e346d4a3d 100644--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py@@ -445,6 +445,21 @@ class PythonPerfDialect(BasePerfDialect):# iterates ``items``._ITER_WRAPPERS: frozenset[str] = frozenset({"enumerate", "sorted", "reversed", "list", "set"})+ def defers_body(self, fn: Node) -> bool:+ """A function containing its own ``yield`` is a generator: calling it+ runs none of the body, so a read there happens per item, not per call."""+ if fn.type != "function_definition":+ return False+ stack = list(fn.children)+ while stack:+ node = stack.pop()+ if node.type in ("function_definition", "lambda", "class_definition"):+ continue+ if node.type == "yield":+ return True+ stack.extend(node.children)+ return False+def loop_iterable_name(self, node: Node) -> str | None:if node.type != "for_statement":return Nonediff --git a/tests/unit/health/test_hot_path_deferred_body.py b/tests/unit/health/test_hot_path_deferred_body.pynew file mode 100644index 000000000..b9d6fcbfd--- /dev/null+++ b/tests/unit/health/test_hot_path_deferred_body.py@@ -0,0 +1,66 @@+"""A sink inside a deferred body is not charged to the function that defines it.++A generator's body and an ``iter.Seq`` closure run when the caller iterates+them, not when the function is called, so a blocking read there is not a+per-call wait. A plain function that reads on every call stays a candidate.+"""++from __future__ import annotations++from repowise.core.analysis.health.complexity import walk_file+++def _hot_sinks(lang: str, src: str) -> list[str]:+ fc = walk_file(f"t.{lang}", lang, src.encode())+ return [f.blocking_sink_kind for f in fc.perf_fn_facts if f.blocking_sink_kind]+++class TestPythonGenerators:+ def test_a_generator_reading_a_file_is_not_a_hot_path_candidate(self) -> None:+ src = (+ "def iter_lines(path):\n"+ " with open(path) as fh:\n"+ " for line in fh:\n"+ " yield line.rstrip('\\n')\n"+ )+ assert _hot_sinks("python", src) == []++ def test_a_plain_function_reading_the_whole_file_is_still_a_candidate(self) -> None:+ src = "def read_all(path):\n return open(path).read()\n"+ assert _hot_sinks("python", src) == ["filesystem"]++ def test_a_read_before_the_first_yield_is_deferred_too(self) -> None:+ src = "def lines(path):\n cfg = open(path).read()\n yield cfg\n"+ assert _hot_sinks("python", src) == []++ def test_a_nested_plain_function_does_not_inherit_the_generator_exemption(self) -> None:+ src = "def outer(path):\n def gen():\n yield 1\n return open(path).read()\n"+ assert _hot_sinks("python", src) == ["filesystem"]+++class TestGoIteratorClosures:+ def test_an_iter_seq_closure_reading_a_file_is_not_a_hot_path_candidate(self) -> None:+ src = (+ "package gopkg\n"+ 'import (\n\t"bufio"\n\t"iter"\n\t"os"\n)\n'+ "func Lines(path string) iter.Seq[string] {\n"+ "\treturn func(yield func(string) bool) {\n"+ "\t\tf, err := os.Open(path)\n"+ "\t\tif err != nil {\n\t\t\treturn\n\t\t}\n"+ "\t\tdefer f.Close()\n"+ "\t\ts := bufio.NewScanner(f)\n"+ "\t\tfor s.Scan() {\n\t\t\tif !yield(s.Text()) {\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n"+ "\t}\n"+ "}\n"+ )+ assert _hot_sinks("go", src) == []++ def test_a_plain_go_function_reading_a_file_is_still_a_candidate(self) -> None:+ src = (+ 'package gopkg\nimport "os"\n'+ "func ReadAll(path string) []byte {\n"+ "\tb, _ := os.ReadFile(path)\n"+ "\treturn b\n"+ "}\n"+ )+ assert _hot_sinks("go", src) == ["filesystem"]