Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions graphify/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,14 @@ def finalize_raw_graph_endpoints(data: dict) -> None:
e.get("source_file"),
(node_by_id.get(alias_target) or {}).get("source_file"),
):
canonical = alias_target
target_attrs = node_by_id.get(alias_target) or {}
target_sf = target_attrs.get("source_file", "")
is_file_node = (
target_attrs.get("label") == Path(target_sf).name
if target_sf else False
)
if not (e.get("relation") in ("calls", "indirect_call") and is_file_node):
canonical = alias_target
if canonical is not None:
e[field] = canonical

Expand Down Expand Up @@ -1982,7 +1989,7 @@ def build_from_json(extraction: dict, *, directed: bool = False, root: str | Pat
# normalized-id and ghost-remap entries norm_to_id already carries.
alias_to_id = _alias_index((nid, G.nodes[nid]) for nid in node_set)

def _resolve_endpoint(raw_id: str, edge_source_file: object) -> str:
def _resolve_endpoint(raw_id: str, edge_source_file: object, relation: str = "") -> str:
norm = _normalize_id(raw_id)
resolved = norm_to_id.get(norm)
if resolved is not None:
Expand All @@ -1991,7 +1998,14 @@ def _resolve_endpoint(raw_id: str, edge_source_file: object) -> str:
if alias_target is not None and _alias_lang_compatible(
edge_source_file, G.nodes[alias_target].get("source_file")
):
return alias_target
target_attrs = G.nodes[alias_target]
target_sf = target_attrs.get("source_file", "")
is_file_node = (
target_attrs.get("label") == Path(target_sf).name
if target_sf else False
)
if not (relation in ("calls", "indirect_call") and is_file_node):
return alias_target
return raw_id

# Iterate edges in a deterministic order. The graph is undirected and stores
Expand Down Expand Up @@ -2028,10 +2042,11 @@ def _resolve_endpoint(raw_id: str, edge_source_file: object) -> str:
)
continue
# Remap mismatched IDs via normalization before dropping the edge.
_edge_rel = str(edge.get("relation", ""))
if src not in node_set:
src = _resolve_endpoint(src, edge.get("source_file"))
src = _resolve_endpoint(src, edge.get("source_file"), _edge_rel)
if tgt not in node_set:
tgt = _resolve_endpoint(tgt, edge.get("source_file"))
tgt = _resolve_endpoint(tgt, edge.get("source_file"), _edge_rel)
if src not in node_set or tgt not in node_set:
# An import/re-export whose target is not a declared node points at
# an external module (stdlib / third-party / another repo). Mint a
Expand Down
149 changes: 149 additions & 0 deletions graphify/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,124 @@ def _repoint_python_sibling_imports(paths, all_nodes, all_edges, root) -> None:
e["target"] = repointed


def _repoint_python_sys_path_imports(per_file, all_nodes, all_edges, root) -> None:
"""Repoint Python plain-import edges targeting modules in directories dynamically
added to sys.path via sys.path.insert / sys.path.append (#5253).

When a script in a subdirectory (e.g. `src/skills/s/scripts/c.py`) adds another
non-package directory to sys.path (`sys.path.insert(0, '../../bin')`), a subsequent
plain import (`import b`) resolves at runtime to `src/bin/b.py`. Repointing the
import edge here to the canonical file node ID ensures downstream member calls
(`b.f()`) bind to the function node (`b.f`) in `_resolve_python_member_calls`,
preventing callers from collapsing onto the file node itself.
"""
if not per_file or not any(r and r.get("_python_sys_path_entries") for r in per_file):
return

node_ids = {n.get("id") for n in all_nodes if isinstance(n, dict)}
file_node_by_path: dict[Path, str] = {}
for n in all_nodes:
if not isinstance(n, dict):
continue
sf = n.get("source_file") or ""
if not sf or effective_suffix(sf).lower() not in (".py", ".pyi"):
continue
nid = n.get("id")
if not isinstance(nid, str):
continue
lbl = str(n.get("label", ""))
rel_name = Path(sf).name
if lbl != rel_name:
continue
try:
p = Path(sf)
if not p.is_absolute():
p = (root / p).resolve()
else:
p = p.resolve()
file_node_by_path[p] = nid
except (ValueError, OSError):
continue

try:
root_path = Path(root).resolve()
except OSError:
root_path = Path(root)

for r in per_file:
if not r or not r.get("_python_sys_path_entries"):
continue
sf = ""
for n in r.get("nodes", []):
if n.get("source_file"):
sf = str(n["source_file"])
break
if not sf:
continue
try:
sf_p = Path(sf)
if not sf_p.is_absolute():
sf_p = (root_path / sf_p).resolve()
else:
sf_p = sf_p.resolve()
except OSError:
continue

file_dir = sf_p.parent
target_dirs: list[Path] = []
for entry in r["_python_sys_path_entries"]:
curr = file_dir
while True:
try:
cand_dir = (curr / entry).resolve()
if cand_dir.is_dir() and cand_dir not in target_dirs:
target_dirs.append(cand_dir)
except OSError:
pass
if curr == root_path or curr == curr.parent:
break
curr = curr.parent
try:
cand_dir = (root_path / entry).resolve()
if cand_dir.is_dir() and cand_dir not in target_dirs:
target_dirs.append(cand_dir)
except OSError:
pass

if not target_dirs:
continue

for e in all_edges:
if not (
isinstance(e, dict)
and e.get("relation") in ("imports", "imports_from")
and str(e.get("source_file", "")) == sf
):
continue
tgt = e.get("target")
if tgt in node_ids:
continue
if not isinstance(tgt, str):
continue
if tgt in _PYTHON_STDLIB_MODULE_NAMES:
continue

hits = set()
for td in target_dirs:
py_cand = td / f"{tgt}.py"
pyi_cand = td / f"{tgt}.pyi"
pkg_cand = td / tgt / "__init__.py"
hit_nid = (
file_node_by_path.get(py_cand)
or file_node_by_path.get(pyi_cand)
or file_node_by_path.get(pkg_cand)
)
if hit_nid is not None and hit_nid != e.get("source"):
hits.add(hit_nid)
if len(hits) == 1:
e["target"] = next(iter(hits))



SEMANTIC_RELATIONS = frozenset({
"inherits", "implements", "mixes_in", "embeds", "references",
Expand Down Expand Up @@ -1894,6 +2012,36 @@ def _extract_python_rationale(path: Path, result: dict) -> None:
except Exception:
return

# Extract dynamic sys.path modifications (#5253): sys.path.insert / sys.path.append
sys_path_entries: list[str] = []
for stmt in root.children:
if stmt.type != "expression_statement":
continue
for child in stmt.children:
if child.type == "call":
fn = child.child_by_field_name("function")
if fn and fn.type == "attribute":
attr = fn.child_by_field_name("attribute")
obj = fn.child_by_field_name("object")
if attr and obj and obj.type == "attribute":
sub_attr = obj.child_by_field_name("attribute")
sub_obj = obj.child_by_field_name("object")
if (
source[sub_obj.start_byte:sub_obj.end_byte] == b"sys"
and source[sub_attr.start_byte:sub_attr.end_byte] == b"path"
and source[attr.start_byte:attr.end_byte] in (b"insert", b"append")
):
args = child.child_by_field_name("arguments")
if args:
for a in args.children:
if a.type in ("string", "concatenated_string"):
val = source[a.start_byte:a.end_byte].decode("utf-8", errors="replace")
val = val.strip("\"'")
if val:
sys_path_entries.append(val)
if sys_path_entries:
result["_python_sys_path_entries"] = sys_path_entries

stem = _file_stem(path)
str_path = str(path)
nodes = result["nodes"]
Expand Down Expand Up @@ -12643,6 +12791,7 @@ def _learn(e: dict) -> None:
# graph is identical regardless of scan root (#2072).
_repoint_python_package_imports(paths, all_nodes, all_edges, root)
_repoint_python_sibling_imports(paths, all_nodes, all_edges, root)
_repoint_python_sys_path_imports(per_file, all_nodes, all_edges, root)
_merge_swift_extensions(per_file, all_nodes, all_edges)
# Salt colliding ids apart BEFORE the C# partial merge. The merge rewires the
# edges of every half onto the canonical half's id; if that id collides with
Expand Down
173 changes: 173 additions & 0 deletions tests/test_plain_import_member_calls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
"""Tests for plain-import member call resolution (#5253).

A call on a plainly-imported module (`import b` then `b.f()`) where `b.py` lives
in a non-package directory outside the caller's directory (e.g. `bin/b.py`)
should bind the call to `b.f()`, not fall back to the file node `b.py`.
"""

from pathlib import Path
from graphify.extract import extract
from graphify.build import build_from_json


def test_plain_import_member_call_binds_to_function_node(tmp_path):
bin_dir = tmp_path / "bin"
scripts_dir = tmp_path / "skills" / "s" / "scripts"
bin_dir.mkdir(parents=True)
scripts_dir.mkdir(parents=True)

b_file = bin_dir / "b.py"
b_file.write_text("def f():\n return 1\n", encoding="utf-8")

c_file = scripts_dir / "c.py"
c_file.write_text(
"import sys\n"
"sys.path.insert(0, '../../bin')\n"
"import b\n\n"
"def other():\n"
" return b.f()\n",
encoding="utf-8",
)

result = extract([b_file, c_file], cache_root=tmp_path)
edges = result["edges"]

# The call from other() must target f(), NOT the file node b.py
calls = [
e for e in edges
if e.get("relation") == "calls"
and e.get("source_file", "").endswith("c.py")
]
assert len(calls) == 1, f"Expected 1 call edge, got: {calls}"
call = calls[0]

nodes_by_id = {n["id"]: n for n in result["nodes"]}
target_node = nodes_by_id.get(call["target"])
assert target_node is not None, f"Target node {call['target']} not found in graph nodes"
assert target_node.get("label") == "f()", f"Expected target f(), got {target_node}"

# Import edge must target b.py file node
imports = [
e for e in edges
if e.get("relation") in ("imports", "imports_from")
and e.get("source_file", "").endswith("c.py")
and e.get("target") != "sys"
]
assert len(imports) == 1, f"Expected 1 import edge to b, got: {imports}"
assert imports[0]["target"] == target_node["source_file"].replace("/", "_").replace(".py", "") or \
nodes_by_id[imports[0]["target"]].get("label") == "b.py"

# End-to-end graph build must preserve call -> f() and not collapse onto file node
G = build_from_json(result)
call_edges = [
d for _, _, d in G.edges(data=True)
if d.get("relation") == "calls"
and str(d.get("source_file", "")).endswith("c.py")
]
assert len(call_edges) == 1
assert call_edges[0].get("_tgt") == target_node["id"]
assert G.nodes[call_edges[0]["_tgt"]].get("label") == "f()"


def test_plain_import_with_alias_member_call(tmp_path):
bin_dir = tmp_path / "bin"
scripts_dir = tmp_path / "scripts"
bin_dir.mkdir(parents=True)
scripts_dir.mkdir(parents=True)

b_file = bin_dir / "b.py"
b_file.write_text("def f():\n return 1\n", encoding="utf-8")

c_file = scripts_dir / "c.py"
c_file.write_text(
"import sys\n"
"sys.path.insert(0, '../bin')\n"
"import b as helper\n\n"
"def other():\n"
" return helper.f()\n",
encoding="utf-8",
)

result = extract([b_file, c_file], cache_root=tmp_path)
edges = result["edges"]
nodes_by_id = {n["id"]: n for n in result["nodes"]}

calls = [
e for e in edges
if e.get("relation") == "calls"
and e.get("source_file", "").endswith("c.py")
]
assert len(calls) == 1
assert nodes_by_id[calls[0]["target"]].get("label") == "f()"


def test_plain_import_ambiguous_stem_does_not_bind(tmp_path):
bin_dir = tmp_path / "bin"
tools_dir = tmp_path / "tools"
scripts_dir = tmp_path / "scripts"
bin_dir.mkdir(parents=True)
tools_dir.mkdir(parents=True)
scripts_dir.mkdir(parents=True)

(bin_dir / "b.py").write_text("def f():\n return 1\n", encoding="utf-8")
(tools_dir / "b.py").write_text("def f():\n return 2\n", encoding="utf-8")

c_file = scripts_dir / "c.py"
c_file.write_text(
"import sys\n"
"sys.path.insert(0, '../bin')\n"
"sys.path.insert(0, '../tools')\n"
"import b\n\n"
"def other():\n"
" return b.f()\n",
encoding="utf-8",
)

result = extract([bin_dir / "b.py", tools_dir / "b.py", c_file], cache_root=tmp_path)
edges = result["edges"]
nodes_by_id = {n["id"]: n for n in result["nodes"]}

# Ambiguous module name should NOT bind to either file's f()
calls = [
e for e in edges
if e.get("relation") == "calls"
and e.get("source_file", "").endswith("c.py")
]
for call in calls:
target_node = nodes_by_id.get(call["target"])
if target_node:
assert target_node.get("label") != "f()", "Ambiguous import must not resolve to f()"


def test_plain_import_stdlib_module_does_not_capture_local_file(tmp_path):
scripts_dir = tmp_path / "scripts"
scripts_dir.mkdir(parents=True)

# Local file with stdlib name
log_file = scripts_dir / "logging.py"
log_file.write_text("def info(msg):\n pass\n", encoding="utf-8")

c_file = scripts_dir / "c.py"
c_file.write_text(
"import logging\n\n"
"def other():\n"
" return logging.info('test')\n",
encoding="utf-8",
)

result = extract([log_file, c_file], cache_root=tmp_path)
edges = result["edges"]
nodes_by_id = {n["id"]: n for n in result["nodes"]}

# `import logging` refers to stdlib, not the local logging.py
calls = [
e for e in edges
if e.get("relation") == "calls"
and e.get("source_file", "").endswith("c.py")
]
# Call to stdlib logging.info is an external module call or pruned, not local logging.py::info
for call in calls:
target_node = nodes_by_id.get(call["target"])
if target_node and target_node.get("source_file", "").endswith("logging.py"):
assert target_node.get("label") != "info()", "Stdlib import must not bind to local logging.py"

Loading