Python AST import edges to in-repo modules emit bare module names, silently dropping real dependencies
Version: first observed on graphifyy 0.9.17, re-verified still present on 0.9.29 Platform: Windows 11, Python 3.11
Summary
The Python AST extractor emits import edges whose endpoint is the bare module name. For external modules (os, math, yaml) that is correct: they were never nodes, and the edge is dropped at build time. But for modules inside the scanned repo, the node exists under its fully-qualified id, the bare-name endpoint matches nothing, and the edge is silently dropped. A real intra-repo dependency vanishes from the graph.
Reproduction
A repo with ha/pyscript/solar_coordinator.py importing sibling modules:
import solar_allocator
import ev_actuator
import hws_intent
Extraction produces nodes like ha_pyscript_modules_solar_allocator, and edges whose targets are solar_allocator / ev_actuator / hws_intent. Those targets match no node id, so build_from_json drops all of them. Re-tested on 0.9.29: all three bare endpoints still appear.
Measured impact (clean extraction, ~3.1k-node graph)
49 distinct dangling endpoint names total:
- 25 external (stdlib/third-party), which is correct behaviour, not a defect
- 4 in-repo module names, silently losing 7 real edges. The importing file's own module structure is missing from the graph
- 20 from a separate JS id defect (reported separately)
Suggested fix (validated locally)
We wrote a post-pass resolver and learned two things the hard way:
- Matching by id suffix alone fabricates dependencies. Our first attempt bound
solar_allocatorto an unrelated node whose id happened to end in_solar_allocator. The reliable predicate is: candidate node'sPath(source_file).stem == module_name. - Node ids legitimately repeat across extraction passes (AST and semantic reuse the same id with different
source_filevalues), so the index must be id to list of nodes, qualifying if any entry has the matching stem. A dict keyed by id keeps the last writer and can shadow the real module node with a doc-mention node.
Exactly one qualifying candidate wins; zero or several stays unresolved. Conservative on purpose: a fabricated edge is worse than a missing one.
Related: #949 (same-basename import-target ambiguity in update), #2191 (diagnostics counting external refs as dangling; splitting the dangling count into external vs in-repo would have surfaced this class immediately).
Corroborating on 0.9.30 (Windows 11, Python + SQL mixed repo, uv tool install):
Ran the full pipeline on a ~158-file repo (130 code, 28 docs, mostly Python + some SQL). diagnose_extraction reported:
- raw_edges: 5507
- dangling_endpoint_edges: 473
- valid_candidate_edges: 5034
- final graph: 1994 nodes, 4847 edges
Same symptom as described here: Node 1987 (id='langgraph') and similar bare-name endpoints for in-repo imports don't resolve to the fully-qualified node id, so the edge is silently dropped at build time. Health check surfaces the count but not which edges, so tracking down the affected files currently requires manual diffing of raw vs valid edge lists.
Not blocking (graph is still usable), but the dropped-edge rate here is noticeably higher (~9% of raw edges) than the ~7/49 dangling-name ratio reported in the original issue, for whatever that's worth as a second data point.
Still present on 0.9.55 (current latest on PyPI), macOS 15 / Python 3.13, uv-installed. Third data point after 0.9.29 and 0.9.30, and the first non-Windows one.
While reproducing this I found the severity splits cleanly by import form, which may be useful for scoping the fix.
Minimal repro
Three flat sibling modules, no packages, no __init__.py:
# src/db.py
def find_user(email):
return {"email": email}
# src/fromimport.py
from db import find_user
def lookup(email):
return find_user(email)
# src/plain.py
import db
def lookup(email):
return db.find_user(email)
extract(collect_files(root / "src"), cache_root=root) yields nodes
src_db, src_db_find_user, src_fromimport, src_fromimport_lookup, src_plain, src_plain_lookup and these edges:
[ok ] src_db --contains --> src_db_find_user
[DANGLING] src_fromimport --imports_from--> db
[ok ] src_fromimport --contains --> src_fromimport_lookup
[DANGLING] src_plain --imports --> db
[ok ] src_plain --contains --> src_plain_lookup
[ok ] src_fromimport --imports --> src_db_find_user
[ok ] src_fromimport_lookup --calls --> src_db_find_user
The two cases differ in impact
from db import find_user — the bare-name imports_from edge dangles as described, but the extractor also emits a fully-qualified imports --> src_db_find_user plus a calls --> src_db_find_user. The dependency still reaches the graph through the symbol level; what is lost is the module-to-module edge. Cosmetic in a dependency sense, but it does inflate the dangling count and trip the health warning.
import db — strictly worse. The imports --> db edge dangles and there is no symbol-level fallback, so src_plain ends up with zero edges to src_db. Note also that db.find_user(email) produces no calls edge at all: src_plain_lookup --calls--> src_db_find_user is absent from the output above. So it is not only the import edge that is lost — the attribute call that depends on the import binding goes with it, and the two files are fully disconnected.
The original report's repro (import solar_allocator) is the second form, which matches "a real intra-repo dependency vanishes" exactly. For the first form the current title slightly overstates it, since the symbol edges survive.
Note on the suggested fix
The Path(source_file).stem == module_name post-pass proposed in the issue body would resolve the dangling module endpoint in both cases. It would not by itself restore the missing src_plain_lookup --calls--> src_db_find_user edge, since that one is never emitted rather than emitted-and-dropped — that looks like a separate gap in the call resolver for mod.attr() when the binding comes from a plain import. Possibly the same root cause as #3272, though that one is scoped to packages with __init__.py and an importer outside the package tree, whereas this repro is a flat directory.
Happy to test a patch against either import form.
Fourth data point, and a third import form: from package import submodule (src layout). Seen on the hosted service (graphify.com MCP, pipeline production-v1) rather than the CLI, on a private ~25-module Python repo; re-index at a newer commit did not change it.
Shape
src/pkg/__init__.py # empty
src/pkg/store.py # def value_asof(conn, sid, period, asof): ...
src/pkg/freeze.py # from pkg import baselines, store
# ... return store.value_asof(conn, sid, period, asof)
jobs/pull.py # from pkg import ingest, store
Observed
graphify_file_neighbors("src/pkg/store.py")→ 0 neighbour files, although 3 library modules and 3 job scripts import it.graphify_callers("value_asof", strict_calls=false)→ 0 callers, although it is called asstore.value_asof(...).- No Python file has any cross-file
imports/callsedge to another in-repo module. Test → functioncallsedges do survive (tests call functions by bare name afterfrom pkg.freeze import find_release). - The TypeScript half of the same repo resolves fully, so it is Python-specific.
Why it is worse than the from db import find_user case
Here the imported name is a module, not a symbol, so there is no symbol-level fallback edge; and every use is an attribute call store.fn(), which (per the comment above) emits no calls edge. Net effect matches the import db case: importing and imported files are fully disconnected. In a codebase that consistently uses from pkg import module + module.fn(), the Python dependency graph is effectively empty.
Resolving from <pkg> import <name> to <pkg>/<name>.py when that file exists (before treating <name> as a symbol of <pkg>/__init__.py), and binding <name>.attr() calls through it, would cover this form.