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.

feat(graph): resolve a python call by typing its receiver from the body (#1643)

Raghav Chamadiyaf99d5e7e9d4a
diff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.py
index 669160d0a..97d615805 100644
--- a/packages/core/src/repowise/core/ingestion/call_resolver.py
+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py
@@ -32,6 +32,7 @@ from typing import Any
import structlog
from .languages.receiver_types import (
+ IMPLICIT_FIELD_LANGUAGES,
RECEIVER_TYPE_LANGUAGES,
Declaration,
scan_declarations,
@@ -100,6 +101,7 @@ _LANGUAGE_CALL_STRATEGIES: dict[str, _LanguageCallStrategies] = {
"java": replace(_JVM_STRATEGIES, member_fallback=_TYPED_RECEIVER),
"kotlin": _JVM_STRATEGIES,
"csharp": _LanguageCallStrategies(member_fallback=_TYPED_RECEIVER),
+ "python": _LanguageCallStrategies(member_fallback=_TYPED_RECEIVER),
"cpp": _CPP_STRATEGIES,
"c": _CPP_STRATEGIES,
}
@@ -1022,14 +1024,15 @@ class CallResolver:
# stands — including when that answer is "declared twice, no usable
# type". Only a name the body never mentions reaches class scope.
body_types = self._declared_types_in(file_path, caller_id, language)
- from_field = receiver_name not in body_types
+ type_name = body_types.get(receiver_name)
+ from_field = (
+ receiver_name not in body_types and language in IMPLICIT_FIELD_LANGUAGES
+ )
if from_field:
class_id = caller_id.rpartition("::")[0]
type_name = (
self._field_types_in(file_path, language).get(class_id, {}).get(receiver_name)
)
- else:
- type_name = body_types[receiver_name]
if type_name is None:
return None
diff --git a/packages/core/src/repowise/core/ingestion/languages/receiver_types.py b/packages/core/src/repowise/core/ingestion/languages/receiver_types.py
index 0ecae7252..329ae75c1 100644
--- a/packages/core/src/repowise/core/ingestion/languages/receiver_types.py
+++ b/packages/core/src/repowise/core/ingestion/languages/receiver_types.py
@@ -56,18 +56,61 @@ _INFERRED_FROM_NEW = re.compile(
# Truncating at ``//`` also truncates a URL inside a string literal. That can
# only ever lose a declaration, never invent one, which is the safe direction.
_LINE_COMMENT = re.compile(r"//.*")
+_HASH_COMMENT = re.compile(r"#.*")
+
+# Python writes its documentation as a string literal in the body, which a
+# comment strip does not reach. Prose is the one false-positive source the
+# C-family shape never had — ``context: The caller`` reads as an annotation —
+# so triple-quoted runs are blanked, keeping their newlines so line numbers
+# survive.
+_DOCSTRING = re.compile(r"(\"\"\"|''')(?:.|\n)*?\1")
_NEWLINE = re.compile(r"\n")
+# Python annotates after the name, not before it: ``x: T``, ``def f(x: T)``.
+# The closer is what keeps the pattern off prose, and it is the reason a
+# generic annotation is refused rather than mis-read — ``x: Optional[T]`` is
+# followed by ``[``, so it matches nothing, which is right, since the value is
+# an Optional and not a T.
+_PY_TYPE = r"[A-Z]\w*(?:\.\w+)*"
+_PY_ANNOTATED = re.compile(
+ rf"(?<![\w.])(?P<name>[a-z_]\w*)\s*:\s*(?P<type>{_PY_TYPE})\s*(?=(?P<closer>[=,)\]\n]))"
+)
+
+# ``x = T(...)``, the only shape unannotated Python offers. Bare-named on
+# purpose: ``x = Foo.bar(...)`` is a call on a class rather than a
+# construction, and typing ``x`` as ``Foo`` from it would simply be wrong.
+# Anchored to the start of a statement because ``dispatch(logger=Emitter())``
+# is otherwise read as declaring ``logger``, which then answers for a
+# ``logger`` that came from somewhere else entirely. The C family is safe from
+# that shape only because its equivalent needs the ``var`` keyword.
+_PY_CONSTRUCTED = re.compile(
+ r"(?m)^[ \t]*(?P<name>[a-z_]\w*)\s*=\s*(?P<type>[A-Z]\w*)\s*\("
+)
+
_C_FAMILY = (_TYPED_DECLARATION, _INFERRED_FROM_NEW)
_LANGUAGE_PATTERNS: dict[str, tuple[re.Pattern[str], ...]] = {
"csharp": _C_FAMILY,
"java": _C_FAMILY,
+ "python": (_PY_ANNOTATED, _PY_CONSTRUCTED),
}
RECEIVER_TYPE_LANGUAGES = frozenset(_LANGUAGE_PATTERNS)
+# Languages where a field can be named with no qualifier, which is the only
+# thing that lets a class-scope declaration answer for a bare receiver. Python
+# writes ``self.foo.bar()``, whose receiver is dotted and which our grammar
+# queries mint no call site for at all — so class scope has nothing there to
+# answer, and consulting it could only bind a bare local name to a field.
+IMPLICIT_FIELD_LANGUAGES = frozenset({"csharp", "java"})
+
+_LANGUAGE_COMMENTS: dict[str, re.Pattern[str]] = {
+ "csharp": _LINE_COMMENT,
+ "java": _LINE_COMMENT,
+ "python": _HASH_COMMENT,
+}
+
class Declaration(NamedTuple):
"""One name given one type, at one line.
@@ -115,7 +158,12 @@ def scan_declarations(text: str, language: str) -> tuple[Declaration, ...]:
if not patterns:
return ()
- cleaned = _LINE_COMMENT.sub("", text)
+ cleaned = text
+ if language == "python":
+ cleaned = _DOCSTRING.sub(lambda m: "\n" * m.group(0).count("\n"), cleaned)
+ comment = _LANGUAGE_COMMENTS.get(language)
+ if comment is not None:
+ cleaned = comment.sub("", cleaned)
# Scanned by the regex engine rather than a Python loop over characters:
# the loop costs more than the declaration scan it exists to serve.
starts = [0, *(newline.end() for newline in _NEWLINE.finditer(cleaned))]
diff --git a/tests/unit/ingestion/test_call_resolver_strategies.py b/tests/unit/ingestion/test_call_resolver_strategies.py
index 3ad173501..07d87c41f 100644
--- a/tests/unit/ingestion/test_call_resolver_strategies.py
+++ b/tests/unit/ingestion/test_call_resolver_strategies.py
@@ -416,3 +416,77 @@ class TestFieldTypedReceiver:
},
)
assert not [e for e in _edges(parsed, tmp_path) if e[3].startswith("receiver_field_")]
+
+
+class TestPythonTypedReceiver:
+ """Python reaches the same strategy through its own declaration shapes."""
+
+ def test_a_constructed_local_types_its_receiver(self, tmp_path: Path) -> None:
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "run.py": (
+ "python",
+ "def run():\n graph = DependencyGraph()\n graph.add_arc(1)\n",
+ ),
+ "graph.py": (
+ "python",
+ "class DependencyGraph:\n def add_arc(self, obj):\n return obj\n",
+ ),
+ },
+ )
+ assert (
+ "run.py::run",
+ "graph.py::DependencyGraph::add_arc",
+ 0.75,
+ "receiver_typed_global",
+ ) in _edges(parsed, tmp_path)
+
+ def test_an_annotated_parameter_types_its_receiver(self, tmp_path: Path) -> None:
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "run.py": (
+ "python",
+ "def run(graph: DependencyGraph):\n graph.add_arc(1)\n",
+ ),
+ "graph.py": (
+ "python",
+ "class DependencyGraph:\n def add_arc(self, obj):\n return obj\n",
+ ),
+ },
+ )
+ assert (
+ "run.py::run",
+ "graph.py::DependencyGraph::add_arc",
+ 0.75,
+ "receiver_typed_global",
+ ) in _edges(parsed, tmp_path)
+
+ def test_a_python_class_attribute_never_types_a_bare_receiver(
+ self, tmp_path: Path
+ ) -> None:
+ """A Python field is reached as ``self.graph``, never as ``graph``.
+
+ So a bare receiver naming a class attribute is a different name, and
+ binding the two would be a wrong edge rather than a missed one.
+ """
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "api.py": (
+ "python",
+ "class Api:\n"
+ " graph: DependencyGraph\n"
+ " def run(self):\n"
+ " graph.add_arc(1)\n",
+ ),
+ "graph.py": (
+ "python",
+ "class DependencyGraph:\n def add_arc(self, obj):\n return obj\n",
+ ),
+ },
+ )
+ edges = _edges(parsed, tmp_path)
+ assert not [e for e in edges if str(e[3]).startswith("receiver_field_")]
+ assert not [e for e in edges if str(e[3]).startswith("receiver_typed_")]
diff --git a/tests/unit/ingestion/test_receiver_types.py b/tests/unit/ingestion/test_receiver_types.py
index c2088fa98..482a1df43 100644
--- a/tests/unit/ingestion/test_receiver_types.py
+++ b/tests/unit/ingestion/test_receiver_types.py
@@ -9,6 +9,7 @@ from __future__ import annotations
import pytest
from repowise.core.ingestion.languages.receiver_types import (
+ IMPLICIT_FIELD_LANGUAGES,
RECEIVER_TYPE_LANGUAGES,
scan_declarations,
types_by_class,
@@ -99,6 +100,90 @@ class TestCsharpShapes:
assert declared_types(body, "csharp")["p"] == "Placeholder"
+class TestPythonShapes:
+ """Python annotates after the name and constructs without a keyword.
+
+ Neither shape is ``T name``, so nothing the C family matches applies here.
+ """
+
+ def test_annotated_parameter(self) -> None:
+ body = "def run(self, crawler: Crawler):\n crawler.stop()"
+ assert declared_types(body, "python")["crawler"] == "Crawler"
+
+ def test_annotated_assignment(self) -> None:
+ body = "def run():\n record: LogRecord = caplog.records[0]"
+ assert declared_types(body, "python")["record"] == "LogRecord"
+
+ def test_construction(self) -> None:
+ body = "def run():\n graph = DependencyGraph()\n graph.add_arc(x)"
+ assert declared_types(body, "python")["graph"] == "DependencyGraph"
+
+ def test_a_parameter_in_a_multi_line_signature(self) -> None:
+ body = "def run(\n self,\n jar: CookieJar,\n): ..."
+ assert declared_types(body, "python")["jar"] == "CookieJar"
+
+
+class TestPythonRefusals:
+ """Prose is the false-positive source the C-family shape never had."""
+
+ def test_a_docstring_is_not_a_declaration(self) -> None:
+ """``context: The caller`` reads as an annotation until docstrings go."""
+ body = '''def run():
+ """Do the thing.
+
+ Args:
+ context: Crawler that owns this run.
+ """
+ context.stop()'''
+ assert "context" not in declared_types(body, "python")
+
+ def test_a_hash_comment_is_not_a_declaration(self) -> None:
+ body = "def run():\n # loader: SpiderLoader once lived here\n pass"
+ assert declared_types(body, "python") == {}
+
+ def test_a_generic_annotation_is_refused(self) -> None:
+ """``x: Optional[Crawler]`` is an Optional, not a Crawler.
+
+ Unwrapping the payload would type ``x`` as something it provably is
+ not, and the validator would happily accept the result.
+ """
+ body = "def run(a: Optional[Crawler], b: list[Spider]):\n a.stop()"
+ types = declared_types(body, "python")
+ assert "a" not in types
+ assert "b" not in types
+
+ def test_a_factory_call_is_not_a_construction(self) -> None:
+ """``x = Foo.bar()`` calls a class; it does not build a ``Foo``."""
+ body = "def run():\n result = GroupResult.restore(id)\n result.get()"
+ assert "result" not in declared_types(body, "python")
+
+ def test_a_keyword_argument_is_not_a_construction(self) -> None:
+ """``f(logger=Emitter())`` binds a parameter of ``f``, not a local.
+
+ Read as a declaration it answers for a ``logger`` that came from
+ somewhere else, which is a wrong edge rather than a missed one. The C
+ family never had this shape because its equivalent needs ``var``.
+ """
+ body = "def run():\n logger = get_logger()\n dispatch(logger=Emitter())"
+ assert declared_types(body, "python") == {}
+
+ def test_a_lowercase_callee_is_not_a_construction(self) -> None:
+ body = "def run():\n spider = build_spider()\n spider.crawl()"
+ assert declared_types(body, "python") == {}
+
+ def test_two_types_for_one_name_yields_neither(self) -> None:
+ body = "def run():\n thing = Crawler()\n thing = SpiderLoader()"
+ assert declared_types(body, "python") == {"thing": None}
+
+ def test_python_never_reaches_class_scope(self) -> None:
+ """A Python field is written ``self.foo``, so no bare receiver names one.
+
+ The declaration is still found — it is the resolver that declines to
+ ask class scope for a language whose fields are always qualified.
+ """
+ assert "python" not in IMPLICIT_FIELD_LANGUAGES
+
+
class TestRefusals:
def test_a_member_type_of_a_builtin_is_refused(self) -> None:
"""``Map.Entry`` bare-names to ``Entry``, which some repo class has.
@@ -197,9 +282,9 @@ class TestClassScope:
def test_the_language_set_is_what_the_patterns_declare() -> None:
"""Excluding a language means removing its shapes, not gating a caller."""
- assert set(RECEIVER_TYPE_LANGUAGES) == {"csharp", "java"}
+ assert set(RECEIVER_TYPE_LANGUAGES) == {"csharp", "java", "python"}
[email protected]("language", ["java", "csharp"])
[email protected]("language", ["java", "csharp", "python"])
def test_an_empty_body_is_not_an_error(language: str) -> None:
assert declared_types("", language) == {}