Skip to content

About

Explain explicit Python filesystem import routes without importing target packages, with a physical expected-root CI gate.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

importroute

Tests

Explain where a Python name would be found in an explicit, ordered set of directories, without importing the target package.

Quickstart

python -m pip install importroute==0.1.0
mkdir -p demo/checkout demo/site
printf 'raise RuntimeError("do not import this")\n' > demo/checkout/widget.py
printf 'pass\n' > demo/site/widget.py
importroute widget --path demo/checkout --path demo/site --expect-under demo/site --json

This exits 1 with top-level FAILED and per-module EXPECTATION_FAILED: the first root selects the checkout's widget.py, outside the expected site directory. The exception in that file does not run. Reverse the two --path arguments and this example passes. This models declared filesystem roots, not custom import hooks or a live process's full import behavior.

Install the pinned 0.1.0 release from PyPI:

python -m pip install importroute==0.1.0

The package release is pinned separately from later repository development. Source-install commands in this README use the checked-out code, which may be newer.

A checkout can hide an installed dependency. A regular package in a later root can replace earlier namespace portions. A package can also win over a same-name module at one root. importroute shows the candidates and the route through each dotted-name component, and can fail a CI check when that route leaves an expected directory.

This is a bounded filesystem model, not proof of what a running process imports. It does not evaluate initializers, custom finders, editable-install hooks, .pth files, archives, the module cache, or a package's dynamic __path__ changes. It does not prove that selected code can load or is safe.

Install

Python 3.10 or newer. No runtime dependencies.

python -m pip install .
importroute --help

Run the tool with the interpreter whose default extension suffixes you want to model. The pinned PyPI release and installation from this checkout are separate options; the checkout may contain newer changes.

A reproducible shadowing check

mkdir -p demo/checkout demo/site
printf 'raise RuntimeError("do not import this")\n' > demo/checkout/widget.py
printf 'pass\n' > demo/site/widget.py

importroute widget --path demo/checkout --path demo/site

The report selects demo/checkout/widget.py and lists demo/site/widget.py as a later-root candidate. Paths are displayed as absolute paths. The first file's exception does not run.

Require the selected route to stay under the intended directory:

importroute widget --path demo/checkout --path demo/site \
  --expect-under demo/site --json

This exits 1 with top-level FAILED and per-module EXPECTATION_FAILED, because the first directory wins. Reversing the two --path arguments makes this example pass.

For a dotted name:

importroute acme.plugins.reader --path ./src --path ./vendor --json

Each component has its own search_roots, candidates, selection, ignored_roots, and errors. Child lookup follows the selected package directories, not the original top-level roots. Regular packages are assumed to retain their default directory __path__.

Python API

from importroute import trace_imports

report = trace_imports(
    ["widget", "acme.plugins.reader"],
    ["./src", "./vendor"],
    expected_root="./src",
)
print(report["status"])

Inputs must be iterables, not one string. Module names are absolute dotted identifiers. Relative names and path/glob patterns are rejected. Paths are literal: no environment-variable or tilde expansion. An empty path means the current directory. The function does not change sys.path.

Selection rules

The model uses exact-case directory entries and the running interpreter's default extension, source, then legacy-bytecode suffix order.

  • Within one root, a regular package beats a same-name module, including extension modules. Among matching files, default loader suffix order applies.
  • Across roots, the first concrete module or regular package wins.
  • Namespace portions combine in root order only if there is no concrete match. A later concrete match discards earlier namespace portions.
  • A plain module cannot have a child. A selected regular package's missing child does not fall back to unrelated top-level roots.
  • __pycache__ entries without source are not treated as legacy same-directory .pyc candidates. Extension and bytecode files are located, never loaded or validated.

--expect-under checks the physical path of every selected component, including every namespace portion. A symlink pointing outside fails even when its lexical path is inside. The expected root must be an existing directory. This is a location check, not a trust or containment sandbox.

Reports and exit codes

JSON reports use format: "importroute/v1". All queries are retained. The overall status gives INCOMPLETE priority over failures.

Exit Meaning
0 Every query is LOCATED, with any expectation satisfied
1 A query is MISSING, PARENT_NOT_PACKAGE, or EXPECTATION_FAILED
2 Invalid input, command usage, or output failure
3 At least one query is INCOMPLETE

An INCOMPLETE step may include a tentative selection from readable roots. Do not treat it as a passing result. Unreadable directories, non-directory roots such as archives, scan-budget exhaustion, and failed physical-path checks are explicit failures to complete the model. Missing roots are listed and ignored. Even an unsupported root after an apparent winner causes INCOMPLETE, because this tool promises a candidate inventory, not only a winner.

Top-level names with builtin or frozen precedence in this interpreter return INCOMPLETE instead of pretending a directory candidate is the runtime winner. Other non-filesystem behavior is excluded by the stated assumptions, not detected automatically.

Limits per call: 128 queries, 64 initial roots, 32 components per query, and 100,000 directory entries. One additional entry may be observed to detect that the entry budget was exceeded. Directory-name caches live only for the call. These are work limits, not a timeout: a slow filesystem or network mount can still block an operating-system call.

When to use something else

For the spec selected by the real import system, use importlib.util.find_spec. Looking up a dotted name there can execute its parent package. If runtime behavior matters, reproduce the real process and inspect the actual imported module instead of relying on this model.

Ruff's A005 checks standard-library name shadowing. deptry checks dependency declarations and imports. Import Linter checks architecture contracts. Those solve different problems; this tool does not replace them. Its narrower task is an explicit-directory route explanation with all candidates and an expected-root gate.

Detect Shadowed Modules is a closer alternative for duplicate-directory checks across an environment's search paths. Its CLI includes uv editable locations and installed-distribution ownership; its Python API also accepts explicit roots. Prefer it for that environment-wide inventory. importroute instead follows requested dotted names, includes ordinary module files and loader suffix precedence, distinguishes namespace accumulation from concrete-package selection, and checks the physical path of every selected component against an expected root.

Do not run Python itself on a hostile checkout merely because this tool does not import target packages. Interpreter startup, the installed tool, and its own imports remain part of your execution environment. Use a trusted installation and working directory. Filesystem observations can race with concurrent changes and are not an atomic snapshot. The model intentionally does not follow platform case-folding or PYTHONCASEOK behavior.

Development

python -m unittest discover -s tests -v

Tests compare 25 two-root layout pairs with the standard-library finder and 120 seeded nested layouts with actual harmless imports in isolated child processes. They also cover namespace precedence, suffix selection, symlinks, scan limits, no target execution, and output failures. This is Linux validation, not a claim of complete cross-platform import emulation.

See design and sources for the scope and evidence.

MIT licensed.

Compare two declared search-path plans

Before changing root order in a build or CI job, compare the modeled selections:

importroute widget --path demo/checkout --path demo/site \
  --compare-path demo/site --compare-path demo/checkout --json

--path is the baseline plan; repeated --compare-path values form the candidate plan. Both are scanned now, sequentially against the observed filesystem, not loaded from historical snapshots. Target code is never read or imported. The source-only API is:

from importroute import compare_routes
report = compare_routes(['widget'], ['demo/checkout', 'demo/site'],
                        ['demo/site', 'demo/checkout'])

The full JSON embeds both ordinary traces plus one comparison row per requested module. It compares the terminal module status and the selection at every dotted component: kind, storage, lexical origin, and ordered namespace locations (including repeated portions). It reports the first changed component's zero-based index when known. Differences only in discarded candidates, root positions or scan counts do not count as selected-route changes, but remain in the full traces. A symlink alias is a different lexical origin even if it points to the same file. Identical paths do not prove equal code bytes.

Comparison status Exit Meaning
UNCHANGED 0 Selected routes/statuses agree in this model
CHANGED 1 At least one selected route/status differs
INCOMPLETE 3 At least one side could not be fully checked
INVALID 2 Invalid invocation/input, or output delivery failed

Incomplete dominates changed, and no first-change witness is asserted for an incomplete module. Two missing names or two identical failed root expectations can be UNCHANGED: exit 0 is not a loadability or expectation pass. With --expect-under, inspect each embedded trace's verdict. Builtin/frozen precedence remains incomplete, not a successful filesystem comparison. Each side has an independent normal scan budget, so two plans may scan twice the normal limit in total. Slow filesystem calls and concurrent edits are still outside the bound.

No runtime import, historical file change, source-content equality, environment migration, rollout safety or dynamic initializer behavior is verified. Normal single-plan output is unchanged. This is a source-checkout feature, not a new PyPI release.

Complete CLI text delivery in the source checkout

Help and usage messages now use the same checked write-and-flush boundary as reports. Short writes, non-integer write results, unavailable streams, and write/flush failures return exit 2. The process runner checks both standard streams before shutdown, including help/usage exits, so a failed flush does not turn into Python's shutdown exit 120. Partial bytes cannot be retracted.

Embedded main(argv) does not close or replace caller-owned streams. Successful help and usage parsing still raise the usual SystemExit(0) or SystemExit(2). The installed command and python -m importroute own their shutdown handling. Report fields, route selection and ordinary successful text are unchanged. This is a source-checkout fix, not a new package-index release.

About

Explain explicit Python filesystem import routes without importing target packages, with a physical expected-root CI gate.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages