Skip to content

Python: from <pkg> import <submodule> binds to the package __init__.py and drops the submodule + all mod.attr() calls when the importer is outside the package tree #3272

Open

Description

@NigelClinton

graphifyy==0.9.53, Python 3.13, macOS. AST only (--code-only), no LLM, no API key.

Summary

from <pkg> import <submodule> resolves the package to its __init__.py but never resolves the imported name as a submodule when the importing file lives outside the package's directory tree. The imports_from edge to the submodule file is missing, and — because the receiver is never bound to a module node — every downstream submodule.attr() call also loses its calls edge.

There is no dangling edge to notice: the emitted imports_from lands on the package's __init__.py, which is a real node. The graph looks well-formed, so auditing graph.json for dangling endpoints does not surface it.

The same import from a file inside the package tree resolves correctly, so this is positional, not syntactic.

Minimal reproducer (stock 0.9.53, no LLM)

src/towmo/__init__.py                  (empty)
src/towmo/core/__init__.py             (empty)
src/towmo/domains/__init__.py          (empty)
src/towmo/core/helper.py
src/towmo/domains/inside_pkg_form.py
tests/test_pkg_form.py
# src/towmo/core/helper.py
def do_work(value):
    return value * 2

# src/towmo/domains/inside_pkg_form.py   -- INSIDE the package tree
from towmo.core import helper

def run():
    return helper.do_work(21)

# tests/test_pkg_form.py                 -- OUTSIDE the package tree
from towmo.core import helper

def test_it():
    assert helper.do_work(21) == 42
graphify extract . --code-only

Observed

Edges emitted, by source file:

src/towmo/domains/inside_pkg_form.py
   imports_from -> src_towmo_core_init      (core/__init__.py)
   imports_from -> src_towmo_core_helper    (helper.py)      <-- present
   calls        -> src_towmo_core_helper_do_work (do_work()) <-- present

tests/test_pkg_form.py
   imports_from -> src_towmo_core_init      (core/__init__.py)
   (no edge to helper.py, no calls edge to do_work())

Both targets exist as nodes; nothing is dangling. The outside file simply binds helper to core/__init__.py and stops.

Expected

tests/test_pkg_form.py should emit imports_from -> helper.py and calls -> do_work(), exactly as the inside file does.

The distinguishing variable is the importer's location, not the import form

importer location from pkg.helper import do_work from pkg import helper + helper.do_work()
inside the package tree connected connected
outside (sibling tests/) connected no edge
flat layout, same directory connected connected

The module-form (from towmo.core.helper import do_work) works from anywhere, presumably because the dotted path maps onto a file-path suffix directly. Only the package-form needs to decide whether the imported name is a submodule or a symbol, and that decision appears to be made relative to the importer's own tree.

Impact on a real repository

Measured on a private ~3,200-file Python/TypeScript monorepo using a src/ layout with tests/ and scripts/ as sibling trees — a very common shape:

importer location connected unconnected
inside src/<pkg>/ 29 0
outside (tests/, scripts/) 2 28

28 of 59 package-form imports (47%) produce no edge at all. The rule "importer outside the package root ⇒ no edge" predicts 57 of 59 outcomes (97%).

One module in that repo is imported this way by six test files and, as a result, is 100% invisible: its set_verifier_for_testing() node has only self-edges (contains, rationale_for) from its own file, despite six real call sites.

The practical consequence is that graphify affected "<symbol>" silently understates blast radius, and a hand-audit of graph.json cannot detect it because the edges that are emitted look correct. Test files are exactly where you want impact analysis to be complete.

Related but, I believe, distinct

Happy to test a patch against the real repo and report the before/after connectivity numbers if that is useful.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions