fix(extract): only redirect a file-stem forward ref to a local class (#4334)
Keep v8's sourceless stub and resolve after the walk: rewrite to the local
id only when that id is a class defined in this file. An imported name (e.g.
pkg/widget.py doing `from .base import Widget`) or a same-id function like
`def widget()` now falls through to the cross-file rewire exactly as on v8.
Co-Authored-By: Grok Bot
(cherry picked from commit 1511543938)
This commit is contained in:
1 parent
672441393e
commit
75e9f102bf
2 files changed
+42
-5
No files matched your search
@@ -5035,11 +5035,9 @@ def _extract_generic(
|
||||
return nid
|
||||
nid = _make_id(name)
|
||||
if nid == _make_id(path.stem):
|
||||
# #4334: `Widget` in widget.py slugs to the same id the file node gets
|
||||
# once paths are made relative, so a stub here swallows the file node.
|
||||
# It's a forward reference to the class defined later in this file, so
|
||||
# point at that local id instead.
|
||||
return _make_id(stem, ".".join(namespace_stack), name)
|
||||
# #4334: `Widget` in widget.py slugs to the file-stem id, so the stub
|
||||
# can swallow the file node. Decide after the walk (see below).
|
||||
stem_forward_refs[nid] = _make_id(stem, name)
|
||||
if nid not in seen_ids:
|
||||
# The name isn't defined in this file, so this is a cross-file reference
|
||||
# (e.g. a `Thing` type annotation imported from another module). Emit a
|
||||
@@ -5059,6 +5057,7 @@ def _extract_generic(
|
||||
})
|
||||
return nid
|
||||
|
||||
stem_forward_refs: dict[str, str] = {}
|
||||
file_nid = _make_id(str(path))
|
||||
add_node(file_nid, path.name, 1)
|
||||
if not source:
|
||||
@@ -7374,6 +7373,18 @@ def _extract_generic(
|
||||
|
||||
walk(root)
|
||||
|
||||
# #4334: a forward reference to a same-named class defined later in this
|
||||
# file resolves to that class. Anything else (an imported name, a function
|
||||
# with the same id) keeps the sourceless stub for the cross-file rewire.
|
||||
for stub_nid, local_nid in stem_forward_refs.items():
|
||||
if local_nid not in callable_class_nids:
|
||||
continue
|
||||
for edge in edges:
|
||||
if edge["target"] == stub_nid:
|
||||
edge["target"] = local_nid
|
||||
nodes[:] = [n for n in nodes if not (n["id"] == stub_nid and not n["source_file"])]
|
||||
seen_ids.discard(stub_nid)
|
||||
|
||||
# ── Call-graph pass ───────────────────────────────────────────────────────
|
||||
label_to_nid: dict[str, str] = {} # case-sensitive (Ruby, C#, Java, Kotlin, etc.)
|
||||
label_to_nid_ci: dict[str, str] = {} # case-insensitive (PHP functions/classes)
|
||||
|
||||
@@ -52,3 +52,29 @@ def test_forward_ref_keeps_file_node_with_importer(tmp_path):
|
||||
_check(result, labels, edges)
|
||||
assert ("app.py", "imports_from", "widget.py") in edges
|
||||
assert ("app.py", "imports_from", "Widget") not in edges
|
||||
|
||||
|
||||
def test_imported_same_name_in_subdirectory_keeps_cross_file_rewire(tmp_path):
|
||||
pkg = tmp_path / "pkg"
|
||||
pkg.mkdir()
|
||||
result, labels, edges = _extract(tmp_path, {
|
||||
"pkg/__init__.py": "",
|
||||
"pkg/base.py": "class Widget:\n def draw(self):\n return 1\n",
|
||||
"pkg/widget.py": "from .base import Widget\n\ndef use(w: Widget):\n w.draw()\n",
|
||||
})
|
||||
ids = {n["id"] for n in result["nodes"]}
|
||||
assert all(e["source"] in ids and e["target"] in ids for e in result["edges"])
|
||||
base_widget = next(n["id"] for n in result["nodes"]
|
||||
if n["label"] == "Widget" and n["source_file"].endswith("base.py"))
|
||||
use = next(n["id"] for n in result["nodes"] if n["label"] == "use()")
|
||||
assert any(e["source"] == use and e["target"] == base_widget for e in result["edges"])
|
||||
assert [n for n in result["nodes"] if n["label"] == "Widget"] == [
|
||||
n for n in result["nodes"] if n["id"] == base_widget
|
||||
]
|
||||
|
||||
|
||||
def test_same_named_function_is_not_a_type_reference_target(tmp_path):
|
||||
src = "class Helper:\n def make(self, w: Widget) -> None:\n w.draw()\n\ndef widget():\n return 1\n"
|
||||
result, labels, edges = _extract(tmp_path, {"widget.py": src})
|
||||
assert (".make()", "references", "widget()") not in edges
|
||||
assert (".make()", "uses", "widget()") not in edges
|
||||
Reference in new issue
Block a user