Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(refactoring): compare indentation and real parameter names when reusing a function (#3459)

Raghav Chamadiya796ed29400c1
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/reuse.py b/packages/core/src/repowise/core/analysis/health/refactoring/reuse.py
index 45c693b0b..846e764b9 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/reuse.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/reuse.py
@@ -40,11 +40,12 @@ from typing import Any, Literal, NamedTuple, get_args
from ....callable_spans import body_span
from ...graph_view import ImportEdgeView
-from ...signature_effect import parameter_names
+from ...signature_effect import bound_names, parameter_names
from . import render
from .graph_signals import defined_symbols
from .reuse_names import (
binding_count,
+ code_only,
exported_later,
is_builtin,
module_bindings,
@@ -245,6 +246,8 @@ class _Source:
params: tuple[str | None, list[str]] | None
own: frozenset[str]
reads: frozenset[str]
+ #: Python's statement indentation per body line (:func:`_indents`).
+ indents: tuple[int | None, ...]
def _sites(repo: _Repo, fn: _Fn, others: list[Occurrence]) -> tuple[list[Site] | None, str | None]:
@@ -280,7 +283,8 @@ def _source(repo: _Repo, fn: _Fn, lines: Lines) -> _Source | None:
own = given | _assigned(texts, lang) | {"self", "cls", "this", fn.name}
# The header's names too: an annotation's type must mean the same at the site.
reads = read_names([*texts, *header], lang)
- return _Source(fn, body, header, params, frozenset(own), frozenset(reads))
+ indents = _indents(lines, body, lang)
+ return _Source(fn, body, header, params, frozenset(own), frozenset(reads), indents)
def _source_refusal(repo: _Repo, src: _Source, lines: Lines) -> str | None:
@@ -301,7 +305,7 @@ def _site(repo: _Repo, src: _Source, occ: Occurrence) -> tuple[Site | None, str
if not lines:
return None, "site_unread"
fn = src.fn
- span = _match(lines, src.body, occ, repo.language, 3 + (fn.end - fn.start) // 10)
+ span = _match(lines, src, occ, repo.language, 3 + (fn.end - fn.start) // 10)
if span is None:
return None, "text_differs"
refused = _unreachable(repo, fn, path) or _foreign_names(repo, src, path)
@@ -493,11 +497,11 @@ def _enclosing_names(repo: _Repo, path: str, lines: Lines, span: tuple[int, int]
for host in repo.functions(path):
if not (host.start < span[0] and host.end >= span[1]):
continue
- params = parameter_names(host.signature, lang, host.kind)
- if params is not None:
- out |= set(params[1])
- else:
- out |= set(_IDENTS.findall(" ".join(_header(lines, host, lang))))
+ bound = bound_names(host.signature, lang, host.kind)
+ if bound is None:
+ # Destructured: every name the header holds may be one it binds.
+ bound = set(_IDENTS.findall(" ".join(_header(lines, host, lang))))
+ out |= bound
outside = _code(lines, host.start, span[0] - 1, lang) + _code(lines, span[1] + 1, host.end, lang)
out |= _assigned([t for _n, t in outside], lang)
return out
@@ -614,17 +618,48 @@ def _renamed(text: str, old: str, new: str) -> str:
return re.sub(rf"(?<![\w$]){re.escape(old)}(?![\w$])", new, text, count=1)
-def _match(lines: Lines, body: Code, occ: Occurrence, language: str, slack: int) -> tuple[int, int] | None:
- """Where *body* appears line for line near the occurrence, else None."""
+def _match(lines: Lines, src: _Source, occ: Occurrence, language: str, slack: int) -> tuple[int, int] | None:
+ """Where ``F``'s body appears line for line near the occurrence, Python's
+ statement indentation included, else None."""
window = _code(lines, occ[1] - slack, occ[2] + slack, language)
- want = [t for _n, t in body]
+ want = [t for _n, t in src.body]
texts = [t for _n, t in window]
for i in range(len(texts) - len(want) + 1):
- if texts[i : i + len(want)] == want:
- return window[i][0], window[i + len(want) - 1][0]
+ part = window[i : i + len(want)]
+ if texts[i : i + len(want)] == want and _indents(lines, part, language) == src.indents:
+ return part[0][0], part[-1][0]
return None
+def _indents(lines: Lines, code: Code, language: str) -> tuple[int | None, ...]:
+ """Each Python line's indentation relative to the first, None on a line
+ continuing a bracket (its alignment is free); empty for other languages,
+ whose braces carry the structure the text comparison already checks.
+
+ Brackets are counted outside strings and comments; a line at or left of
+ the first line's indentation starts a statement whatever the count says
+ (a bracket in a multi-line string cannot hide the rest). Indentation is
+ counted in characters, so tabs against spaces never match; a backslash
+ continuation is compared like a statement."""
+ if language != "python" or not code:
+ return ()
+ base = _indent(lines[code[0][0] - 1])
+ out: list[int | None] = []
+ depth = 0
+ for n, text in code:
+ indent = _indent(lines[n - 1])
+ if indent <= base:
+ depth = 0
+ out.append(indent - base if depth == 0 else None)
+ bare = code_only(text, language)
+ depth = max(0, depth + sum(bare.count(o) - bare.count(c) for o, c in ("()", "[]", "{}")))
+ return tuple(out)
+
+
+def _indent(line: str) -> int:
+ return len(line) - len(line.lstrip())
+
+
def _assigned(texts: list[str], language: str) -> set[str]:
"""Names the lines bind: assignments, loop, ``as`` and walrus targets,
lambda / arrow / catch parameters, local imports, JS declarations.
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/reuse_names.py b/packages/core/src/repowise/core/analysis/health/refactoring/reuse_names.py
index 4b37f368d..6cf6d0d27 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/reuse_names.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/reuse_names.py
@@ -83,8 +83,7 @@ def read_names(texts: list[str], language: str) -> set[str]:
string's or template's expressions kept."""
out: set[str] = set()
for text in texts:
- bare = _strip_strings(text, language)
- bare = re.sub(r"#.*$" if language == "python" else r"//.*$", "", bare)
+ bare = code_only(text, language)
skip = set(_PY_KWARG.findall(bare)) if language == "python" else set(
_JS_KEY.findall(bare) + _JSX_ATTR.findall(" " + bare) + _JSX_TAG.findall(bare)
)
@@ -92,6 +91,13 @@ def read_names(texts: list[str], language: str) -> set[str]:
return out - (_PY_WORDS if language == "python" else _JS_WORDS)
+def code_only(text: str, language: str) -> str:
+ """*text* with string contents and a trailing comment removed (a
+ formatted string's or template's expressions kept)."""
+ bare = _strip_strings(text, language)
+ return re.sub(r"#.*$" if language == "python" else r"//.*$", "", bare)
+
+
def is_builtin(name: str, language: str) -> bool:
return name in (_PY_BUILTINS if language == "python" else _JS_GLOBALS)
@@ -298,4 +304,12 @@ def module_of(path: str, language: str) -> str:
return _js_module("./" + posixpath.basename(path), path)
-__all__ = ["binding_count", "exported_later", "is_builtin", "module_bindings", "module_of", "read_names"]
+__all__ = [
+ "binding_count",
+ "code_only",
+ "exported_later",
+ "is_builtin",
+ "module_bindings",
+ "module_of",
+ "read_names",
+]
diff --git a/packages/core/src/repowise/core/analysis/signature_effect.py b/packages/core/src/repowise/core/analysis/signature_effect.py
index d417043e4..2cfedc661 100644
--- a/packages/core/src/repowise/core/analysis/signature_effect.py
+++ b/packages/core/src/repowise/core/analysis/signature_effect.py
@@ -329,6 +329,11 @@ def _parse_signature(sig: str, lang: str | None, kind: str | None) -> _Signature
return _Signature(params=params, returns=returns, receiver=receiver)
+def _parsed(signature: str, language: str | None, kind: str | None) -> _Signature | None:
+ lang = (language or "").lower() or None
+ return _parse_signature(_normalize(signature, lang), lang, kind)
+
+
def parameter_names(
signature: str, language: str | None = None, kind: str | None = None
) -> tuple[str | None, list[str]] | None:
@@ -339,8 +344,7 @@ def parameter_names(
position alone: a variadic, a keyword-only marker or argument, or a
destructured parameter.
"""
- lang = (language or "").lower() or None
- parsed = _parse_signature(_normalize(signature, lang), lang, kind)
+ parsed = _parsed(signature, language, kind)
if parsed is None:
return None
names: list[str] = []
@@ -351,6 +355,26 @@ def parameter_names(
return parsed.receiver, names
+def bound_names(
+ signature: str, language: str | None = None, kind: str | None = None
+) -> set[str] | None:
+ """Every name the parameter list binds, variadic and keyword-only ones
+ and an identifier receiver included, in any language
+ :func:`_parse_signature` reads (typed ``Go`` / ``Java`` lists too); None
+ when the list cannot be parsed or a parameter is destructured."""
+ parsed = _parsed(signature, language, kind)
+ if parsed is None:
+ return None
+ out = {parsed.receiver} if parsed.receiver and parsed.receiver.isidentifier() else set()
+ for param in parsed.params:
+ if param.kind in ("kw_marker", "pos_marker"):
+ continue
+ if not param.name.isidentifier():
+ return None
+ out.add(param.name)
+ return out
+
+
def _split_default(text: str, lang: str | None, param: _Param) -> str:
at = _find_top(text, lang, "=")
if at is None:
diff --git a/tests/unit/health/test_refactoring_reuse.py b/tests/unit/health/test_refactoring_reuse.py
index f1001fabe..3e1d452da 100644
--- a/tests/unit/health/test_refactoring_reuse.py
+++ b/tests/unit/health/test_refactoring_reuse.py
@@ -479,3 +479,56 @@ def test_a_block_comment_binds_nothing() -> None:
lines = ["/*", "import a from './a';", "*/", "import b from './b';"]
assert set(module_bindings(lines, "ui/x.ts", "typescript")) == {"b"}
+
+
+def test_python_indentation_must_match() -> None:
+ # The same lines, the last one moved into the ``if``: not the same body.
+ moved = [*_BODY[:5], " return joined.lower()"]
+ util = ["import json", "", "", *_fn()]
+ host = ["from pkg.util import normalize", "", "", *_host(moved)]
+ repo = (
+ _Repo()
+ .file("pkg/util.py", util)
+ .file("pkg/b.py", host)
+ .symbol("pkg/util.py", "normalize", 4, len(util), "def normalize(text, sep)")
+ .symbol("pkg/b.py", "run", 4, len(host), "def run(text, sep)")
+ .imports("pkg/b.py", "pkg/util.py")
+ )
+ assert repo.reuse(_OCCURRENCES).refused == "text_differs"
+ # A bracket continuation may align anywhere.
+ body = [" parts = text.split(", " sep)", *_BODY[1:5]]
+ other = [" parts = text.split(", " sep)", *_BODY[1:5]]
+ repo.files["pkg/util.py"] = ["import json", "", "", *_fn(body=body)]
+ repo.files["pkg/b.py"] = ["from pkg.util import normalize", "", "", *_host(other)]
+ assert repo.reuse(_OCCURRENCES).plan is not None
+
+
+def test_a_bracket_in_a_string_or_comment_does_not_hide_indentation() -> None:
+ for opener in (' parts = text.split("(")', " parts = text.split(sep) # (x"):
+ body = [opener, *_BODY[1:5], " return joined.lower()"]
+ moved = [opener, *_BODY[1:5], " return joined.lower()"]
+ util = ["import json", "", "", *_fn(body=body)]
+ host = ["from pkg.util import normalize", "", "", *_host(moved)]
+ repo = (
+ _Repo()
+ .file("pkg/util.py", util)
+ .file("pkg/b.py", host)
+ .symbol("pkg/util.py", "normalize", 4, len(util), "def normalize(text, sep)")
+ .symbol("pkg/b.py", "run", 4, len(host), "def run(text, sep)")
+ .imports("pkg/b.py", "pkg/util.py")
+ )
+ assert repo.reuse(_OCCURRENCES).refused == "text_differs"
+
+
+def test_an_annotation_in_a_keyword_only_host_header_binds_nothing() -> None:
+ from repowise.core.analysis.signature_effect import bound_names
+
+ repo = _pair(params="text: str, sep: str", host_params="text: str, *, sep: str")
+ (site,) = repo.reuse(_OCCURRENCES).plan["sites"]
+ assert site["new_text"] == "return normalize(text, sep)"
+ signature = "def f(a: int, *, b: str = '', **kw) -> None"
+ assert bound_names(signature, "python", "function") == {"a", "b", "kw"}
+ assert bound_names("def m(self, x)", "python", "method") == {"self", "x"}
+ assert bound_names("function f({ a, b }: P)", "typescript", "function") is None
+ assert bound_names("func (s *S) f(a int, b ...string)", "go", "method") == {"a", "b"}
+ assert bound_names("void f(int a, String... rest)", "java", "method") == {"a", "rest"}