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): treat cut-off signatures and stub bodies as declarations (#3950)
Wynand van Stadenb86a68aff357
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/extract_helper.py b/packages/core/src/repowise/core/analysis/health/refactoring/extract_helper.pyindex fc751313b..3e8739ffa 100644--- a/packages/core/src/repowise/core/analysis/health/refactoring/extract_helper.py+++ b/packages/core/src/repowise/core/analysis/health/refactoring/extract_helper.py@@ -139,11 +139,28 @@ _DECL_LINE = re.compile(| (async\s+)?(def|function|fn|func|class|interface|type|struct)\s+[\w<>]+\s*[(<{:]?\s*| (async\s+)?(def|function|fn|func|class|interface|type|struct)\s+[\w<>]+\s*\( (?: [^()] | \([^()]*\) )* \) # its parameter list, and nothing past it- \s* (->[^:{;]*)? \s* [:{]? \s* # a signature joined back whole+ \s* (->[^:{;]*)? \s* # a signature joined back whole,+ (: \s* \.\.\. | [:{])? \s* # or a stub whose whole body is `...`| @[\w.]+ \s* \(? # decorator / annotation)$""",re.VERBOSE,)+# A declaration head whose parameter list is still open where the block ends: a+# clone window can stop at the last parameter two signatures share, before the+# closing `) -> T:` line. Only the parameters themselves may follow the `(`.+_OPEN_DECL_HEAD = re.compile(+ r"^\s*(async\s+)?(def|function|fn|func|class|interface)\s+[\w<>]+\s*\((?P<params>.*)$"+)+# One parameter: `name`, `name: type`, `name: type = default`, `*args`, `**kw`,+# a bare `*` or `/`, an optional `name?: type`, or Go's `name type`.+_PARAM = re.compile(+ r"""^(+ (\.\.\.)? \*{0,2} &? (mut\s+)? [\w$]+ \?? \s* (: [^=]*)? (= .+)?+ | [\w$]+ \s+ [\w.*\[\]&]++ | [*/]+ )$""",+ re.VERBOSE,+)_CONTROL_FLOW = re.compile(r"\b(if|else|elif|for|while|try|except|catch|finally|return|yield|raise|throw|"r"with|switch|case|await|match|break|continue|next|fallthrough|defer|goto|redo|retry)\b"@@ -191,6 +208,50 @@ def _continuation_depth(line: str) -> int:return sum(bare.count(o) - bare.count(c) for o, c in ("()", "[]"))+def _is_open_declaration_head(line: str) -> bool:+ """True when *line* is a ``def``/``class``/... head cut off inside its+ parameter list, with nothing but parameters after the ``(``.++ Only a line still open at the end of the block qualifies, so a statement+ folded into it by ``_logical_lines`` (``def f( emit(a)``) has to read as a+ parameter to pass, and does not.+ """+ if _continuation_depth(line) <= 0:+ return False+ m = _OPEN_DECL_HEAD.match(line)+ if m is None:+ return False+ params = _split_top_level(m.group("params"))+ return all(_PARAM.match(p) for p in params if p)+++def _strip_trailing_comment(line: str) -> str:+ """*line* without a trailing ``#``/``//`` comment; one inside a string stays."""+ masked = _STRING_LITERAL.sub(lambda s: "_" * len(s.group(0)), line)+ found = re.search(r"#|//", masked)+ return line[: found.start()].rstrip() if found else line+++def _split_top_level(text: str) -> list[str]:+ """*text* split on the commas outside any bracket or string, each piece+ stripped. A string's contents are blanked first (``"a,b"`` -> ``""``), so+ a comma or bracket in a default value is not read as syntax."""+ text = _STRING_LITERAL.sub('""', text)+ pieces: list[str] = []+ depth = 0+ start = 0+ for i, ch in enumerate(text):+ if ch in "([{":+ depth += 1+ elif ch in ")]}":+ depth -= 1+ elif ch == "," and depth == 0:+ pieces.append(text[start:i].strip())+ start = i + 1+ pieces.append(text[start:].strip())+ return pieces++def _code_lines(block: list[str]) -> list[str]:"""*block* minus blanks and whole-line comments.@@ -238,10 +299,14 @@ def _is_declaration_only(block: list[str]) -> bool:and the defect here is the advice, not the measurement. The block stillcounts as duplication; it just no longer produces a plan to extract it."""- lines = _logical_lines(_code_lines(block))+ code = _code_lines(block)+ lines = _logical_lines(code)if not lines:return True- for line in lines:+ # Only the last logical line can still be open. It is read for parameters+ # without its trailing comments, whose commas do not separate anything.+ open_tail = _logical_lines([_strip_trailing_comment(c) for c in code])[-1]+ for i, line in enumerate(lines):if _CONTROL_FLOW.search(line):return Falseif line.startswith(_IMPORT_PREFIXES) or line in ("(", ")", "):", "}", "];", "{"):@@ -250,6 +315,8 @@ def _is_declaration_only(block: list[str]) -> bool:continueif _DECL_LINE.match(line):continue+ if i == len(lines) - 1 and _is_open_declaration_head(open_tail):+ continue# An assignment is a declaration only by what it assigns. A constant# RHS is inert either way; an annotated line may additionally use a# field-declaration constructor (``field(default_factory=list)``).diff --git a/tests/unit/health/test_refactoring_extract_helper.py b/tests/unit/health/test_refactoring_extract_helper.pyindex bd540ca94..07a93f08c 100644--- a/tests/unit/health/test_refactoring_extract_helper.py+++ b/tests/unit/health/test_refactoring_extract_helper.py@@ -605,6 +605,21 @@ _SIGNATURE_BLOCK = [")",]+# `detect_clones` in duplication/detector.py, lines 192-201: the clone window+# it shares with `detect_clones_with_isolation` stops before `) -> ...:`.+_OPEN_SIGNATURE_BLOCK = [+ "def detect_clones(",+ " parsed_files: Iterable[Any],",+ " git_meta_map: dict[str, dict[str, Any]] | None = None,",+ " *,",+ " window_tokens: int = DEFAULT_WINDOW_TOKENS,",+ " min_lines: int = DEFAULT_MIN_LINES,",+ " limits: DuplicationLimits | None = None,",+ " cache_dir: Path | None = None,",+ " changed_files: set[str] | None = None,",+ " source_reader: Any | None = None,",+]+_FIELD_BLOCK = [" name: str = ''"," items: list[str] = field(default_factory=list)",@@ -651,6 +666,12 @@ def test_function_signature_clone_emits_no_plan():assert _plans_for(_SIGNATURE_BLOCK) == []+def test_open_function_signature_clone_emits_no_plan():+ """A clone window that ends inside a parameter list is still only a+ signature, so it has nothing to extract either."""+ assert _plans_for(_OPEN_SIGNATURE_BLOCK) == []++def test_dataclass_field_run_emits_no_plan():"""Unrelated dataclasses sharing a run of `field(default_factory=...)`declarations share a shape, not code. This is the case filed as C6."""@@ -729,6 +750,26 @@ def test_declaration_only_unit_cases():assert not _is_declaration_only([" break", " break"])assert not _is_declaration_only([" defer f.Close()", " defer g.Close()"])+ # A signature cut off before its closing line is still only a signature,+ # and so is a stub whose whole body is `...`.+ assert _is_declaration_only(_OPEN_SIGNATURE_BLOCK)+ assert _is_declaration_only(+ [" async def get_repository(self, repo_id: str) -> Repository | None: ..."]+ )+ assert _is_declaration_only(+ [" def a(self) -> int: ...", " def b(self, x: str) -> None: ..."]+ )+ assert _is_declaration_only(["function render(", " node: Node,", " opts?: Options,"])+ assert _is_declaration_only(["func Detect(", "\tfiles []File,", "\twindow int,"])+ # A comma in a string default or a trailing comment separates no parameters.+ assert _is_declaration_only(["def f(", ' label: str = "a,b",', " x: int = 1,"])+ assert _is_declaration_only(["def f(", " x: int, # rows, not bytes", " y: int,"])+ assert _is_declaration_only(["def f(", " sep: str = '#',", " y: int,"])+ # An open head followed by a statement is not a signature.+ assert not _is_declaration_only(["def f(", " emit(a), # one, two", " y: int,"])+ assert not _is_declaration_only(["def f(", " x: int,", " return g(x)"])+ assert not _is_declaration_only(["def f(", " emit(a)", " emit(b)"])+# ---- symbol-boundary gating ----------------------------------------------