graphify icon indicating copy to clipboard operation
graphify copied to clipboard

Package-form imports (`from package import module`) are not resolved to import edges — creates disconnected test-file islands

Open janreview opened this issue 5 months ago • 4 comments

Version: graphifyy 0.8.32 (pip, Windows, Python 3.12)

What happened The AST extractor resolves from src.services.ai_blog_enricher import X to an import edge, but NOT the package-form from src.services import ai_blog_enricher. Files using the package form get zero edges to the module they import.

Example (measured)

# tests/test_ai_blog_enricher.py line 7
from src.services import ai_blog_enricher

Result: the test file's 141 nodes form a fully disconnected component with 0 edges to src/services/ai_blog_enricher.py (60 nodes) — despite the test calling ai_blog_enricher.analyze_article(...) etc. throughout.

Impact On a ~6,000-node graph of a Python repo: 76 connected components; 799 nodes outside the giant component, of which 524 (66%) were test files using this import style (from src.services import X is a common pattern for tests that monkeypatch module attributes). GRAPH_REPORT.md then flags them as "possible documentation gaps or missing edges", which sends users hunting for gaps that are actually extractor blind spots.

Suggested fix When resolving from <pkg> import <name>, check whether <pkg>/<name>.py (or <pkg>/<name>/__init__.py) exists in the corpus and emit the same import edge as for the dotted form.

janreview avatar Jun 06 '26 06:06 janreview

Fixed in v0.8.33 (a380b34).

from pkg import submod now resolves to a file-level imports_from edge when pkg/submod.py or pkg/submod/__init__.py exists on disk. The fix lives in _collect_python_symbol_resolution_facts — when the resolved target is a package (__init__.py), each imported name is checked as a potential submodule file. Matched submodules are stored in a new module_imports field and emitted as imports_from edges in _apply_symbol_resolution_facts using canonical stem-based IDs.

Upgrade: uv tool upgrade graphifyy

safishamsi avatar Jun 06 '26 08:06 safishamsi

Confirmed fixed in v0.8.33 on the same repo that produced the report: components 76->61, nodes outside the giant component 799->339 (the remainder are genuinely standalone archive/scripts), and the 141-node test island now has its imports_from edge to the service. The builtin uses@0.5 edges from #1147 are gone too (0 remaining; str dropped out of the god-node top-10). Thanks for the fast turnaround!

janreview avatar Jun 06 '26 09:06 janreview

Haha always fun working on weekends

safishamsi avatar Jun 06 '26 10:06 safishamsi

Yes, always good fun on weekends ;)

janreview avatar Jun 06 '26 10:06 janreview