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(graph): type a Kotlin extension's implicit receiver properties (#4830)
Swati Ahujac798bd5c3817
diff --git a/packages/core/src/repowise/core/ingestion/call_overload_selection.py b/packages/core/src/repowise/core/ingestion/call_overload_selection.pyindex d53093033..32dc17cc3 100644--- a/packages/core/src/repowise/core/ingestion/call_overload_selection.py+++ b/packages/core/src/repowise/core/ingestion/call_overload_selection.py@@ -302,12 +302,16 @@ def _java_method_refusing(symbol: Symbol | None, argument_count: int | None) ->def _kotlin_function_refusing(symbol: Symbol | None, argument_count: int | None) -> bool:"""Whether *symbol* is a Kotlin function whose parameters cannot take *argument_count*.- Never an ``override``: it inherits defaults its own list does not write.+ An ``override`` inherits defaults its own list does not write, but never a+ parameter, so only more arguments than it lists refuse it."""if symbol is None or argument_count is None or symbol.language != "kotlin":return False- if symbol.kind not in _FUNCTION_KINDS or "override" in symbol.modifiers:+ if symbol.kind not in _FUNCTION_KINDS:return False+ if "override" in symbol.modifiers:+ admitted = signature_parameter_range(symbol.signature or "", "kotlin")+ return admitted is not None and admitted[1] is not None and argument_count > admitted[1]return not _takes(symbol, argument_count)diff --git a/packages/core/src/repowise/core/ingestion/call_receiver_typing/__init__.py b/packages/core/src/repowise/core/ingestion/call_receiver_typing/__init__.pyindex 8d4c7156d..9b889e62d 100644--- a/packages/core/src/repowise/core/ingestion/call_receiver_typing/__init__.py+++ b/packages/core/src/repowise/core/ingestion/call_receiver_typing/__init__.py@@ -594,9 +594,56 @@ class ReceiverTypingMixin(return None if builtin_local and external_type_name(field) else (field, "field", class_file)if builtin_local:return None+ extended = self._extended_class_field(file_path, caller_id, name, language)+ if extended is not None:+ return extendedinherited = self._inherited_field(file_path, caller_id, class_id, name, language)return None if inherited is None else (inherited[1], "field", inherited[0])+ def _extended_class_field(+ self, file_path: str, caller_id: str, name: str, language: str+ ) -> tuple[str | None, str, str] | None:+ """The property *name* of the class a Kotlin extension function extends.++ The extended class is the function's implicit ``this``, so ``buffer``+ in ``fun Source.f() = buffer.size()`` is ``Source``'s property. A+ ``private`` or ``protected`` one is not visible outside its class.+ Ceiling: a lambda with receiver (``with(other) { .. }``) rebinds+ ``this`` and is not read, the same as for the caller class's own fields.+ """+ caller = self._symbols_by_id.get(caller_id)+ if caller is None or caller.language != "kotlin" or not caller.extension_receiver:+ return None+ class_id = self._extended_class_id(file_path, caller)+ if class_id is None:+ return None+ class_file = self._symbol_paths_by_id[class_id]+ fields = self._field_types_in(class_file, language).get(class_id, {})+ if name not in fields or self._is_hidden_member(class_file, class_id, name, language):+ return None+ return fields[name], "field", class_file++ def _extended_class_id(self, file_path: str, caller: Symbol) -> str | None:+ """The class *caller*'s receiver names: imported or declared here, else a package sibling."""+ receiver = caller.extension_receiver or ""+ found = self._class_named(file_path, receiver)+ if found is not None:+ return found[1]+ hit = self._class_seen_from(file_path, caller.id, receiver, caller.start_line)+ return None if hit is None else hit.callee_id++ def _is_hidden_member(self, class_file: str, class_id: str, name: str, language: str) -> bool:+ """Is the property *name* of *class_id* ``private`` or ``protected``, a constructor one included?"""+ start, end = self._spans_for(class_file).get(class_id, (1, 0))+ lines = self._text_of(class_file).splitlines()+ hidden = re.compile(+ rf"\b(?:private|protected)\b[^,(){{}}=]*\b(?:val|var)\s+`?{re.escape(name)}\b"+ )+ return any(+ d.name == name and start <= d.line <= end and hidden.search(lines[d.line - 1])+ for d in self._declarations_for(class_file, language)+ )+def _inherited_field(self, file_path: str, caller_id: str, class_id: str, name: str, language: str) -> tuple[str, str | None] | None:diff --git a/tests/unit/ingestion/test_kotlin_extension_calls.py b/tests/unit/ingestion/test_kotlin_extension_calls.pyindex fea5dabfc..536714e57 100644--- a/tests/unit/ingestion/test_kotlin_extension_calls.py+++ b/tests/unit/ingestion/test_kotlin_extension_calls.py@@ -390,3 +390,44 @@ def test_an_aliased_library_import_leaves_the_name_to_the_repo(tmp_path: Path) -"src/a/Use.kt": "package a\n\nfun use(request: Request) {\n request.ext()\n}\n",}assert _resolve_files(tmp_path, files, "src/a/Use.kt") == {"src/a/Ext.kt::ext"}+++_BUF = "package a\n\nclass Buf {\n fun size(): Int = 0\n}\n"+_SRC = (+ "package a\n\nopen class Src(val buffer: Buf, private val cached: Buf) {\n"+ " protected val inner: Buf = Buf()\n}\n"+)+++ ("package", "imports"), [("a", ""), ("a.internal", "import a.Src\n\n")]+)+def test_an_extension_reads_its_receivers_properties_through_this(+ tmp_path: Path, package: str, imports: str+) -> None:+ """`buffer` in `fun Src.total()` is `Src.buffer`; a private or protected one is not visible there."""+ files = {+ "src/a/Buf.kt": _BUF,+ "src/a/Src.kt": _SRC,+ _EXT: (+ f"package {package}\n\n{imports}"+ "fun Src.total(): Int = buffer.size()\n"+ "fun Src.hidden(): Int = inner.size() + cached.size()\n"+ "fun Src.shadowed(buffer: String): Int = buffer.size()\n"+ ),+ }+ assert _resolve_files(tmp_path, files, _EXT) == {"src/a/Buf.kt::Buf::size"}+++def test_an_override_never_takes_more_arguments_than_it_lists(tmp_path: Path) -> None:+ """`buffer.line(n)` is not `Buf.line()`: an override inherits defaults, never a parameter."""+ files = {+ "src/a/Buf.kt": (+ "package a\n\ninterface Lines {\n fun line(): String?\n fun size(n: Int = 0): Int\n}\n\n"+ "class Buf : Lines {\n override fun line(): String? = null\n"+ " override fun size(n: Int): Int = n\n}\n"+ ),+ "src/a/Src.kt": "package a\n\nclass Src(val buffer: Buf)\n",+ _EXT: "package a\n\nfun Src.go(n: Long): Int {\n buffer.line(n)\n return buffer.size()\n}\n",+ }+ assert _resolve_files(tmp_path, files, _EXT) == {"src/a/Buf.kt::Buf::size"}