underscope
The underscore's position in a Python import path sets that import's scope — Rust's pub(super)/pub(crate), applied to PEP 8 underscores.
underscope draws a repo's real Python imports as mermaid diagrams, checks them against two conventions derived from the code itself (no allow-list to maintain), and reports which files and directories carry the most structural coupling. It needs only the standard library to analyze your code — no runtime dependencies.
Install and run
uvx underscope check .
uvx underscope graph . --out docs/imports
uvx underscope metrics .
Or, inside a project: uv add --dev underscope and run underscope ... in that project's venv.
The rules check enforces
Given this tree:
shop/
__init__.py
billing/
__init__.py
invoice.py # from shop.billing._pricing import compute_total
_pricing.py # def compute_total(): ...
catalog/
__init__.py
listing.py # from shop.billing._pricing import compute_total <- finding
- No import cycle, at any directory level: between files, and between directories at every depth. Imports under
if TYPE_CHECKING:and inside function bodies count. There is no baseline — an existing cycle fails too. - A private item stays visible only to its owner and the owner's descendants (Go's
internal/, applied as Rust's visibility rule to PEP 8 underscores):- a private module — a file, or a path component, named
_x(single leading underscore, not a dunder) — belongs to the directory holding it; only files under that directory may import it, across package boundaries; - a private name imported via
from m import _xbelongs tom; ifmis a single file, no other file may import_x, not even a sibling in the same directory; - if
mis a package (an__init__.pyor a namespace package directory),_xbelongs to that directory, and files under it may import it. - a parent never reaches into a child's private items, and neither do siblings.
shop/billing/invoice.pyabove is allowed (it's undershop/billing,_pricing's owner);shop/catalog/listing.pyis a finding.- the attribute form (
import shop.billing._pricing as p; p.compute_total) is out of scope here — ruff'sSLF001already flags it.
- a private module — a file, or a path component, named
underscope check . exits 1 on any finding and prints, per finding, the importing file, the target, and the directory or file that owns it.
The 3 subcommands
underscope check <repo>— run both conventions above, print findings, exit 1 on any finding.underscope graph <repo> --out DIR [--html]— write one mermaid view per directory underDIR:index.mdeverywhere, plus a clickableindex.htmlper view with--html(needs network access in the browser to load mermaid from a CDN).underscope metrics <repo>— print a coupling report to stdout: reach (dependents/dependencies, transitive) and ascoreper file and per directory; interface width (names other code imports from a unit); exposure share (how much of what a file defines actually gets used elsewhere); entry files (how much of a directory outside code actually reaches into); misplaced files (files whose closest partners sit mostly in another directory). Always exits 0 — a report, never a gate.
Blind spots
underscope shows import coupling only, a lower bound on logical coupling ("if A changes, must B?"). Coupling that bypasses Python imports stays invisible:
- HTTP APIs, shared database schemas and tables, config keys and env vars, message and file formats, string-keyed registries, runtime
sys.pathedits. - Two files with no edge here can still force each other's edits. Never read a missing edge as independence.
Other limits:
- a
warning: ambiguous importline (printed to stderr) means several files in the repo match one import; no edge is drawn for it. - an import that only resolves through a runtime
sys.pathedit is matched by file path, which can pick the wrong file. - a third-party name that happens to equal a package folder elsewhere in the repo draws a false edge.
- test directories (
tests/,test/) and test files (test_*.py,*_test.py,conftest.py) are always skipped, no flag — a test importing a sibling would otherwise draw a production dependency that production never has. metricsis a report on numbers that keep moving even on a repo that passescheckclean; gating on it rewards gaming the number (for example, replacing an import with a string-keyed registry) instead of actually lowering coupling.
Metadata
Release files for underscope 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| underscope-0.1.0.tar.gz | 21.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| underscope-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 43.4 kB
Release files / underscope-0.1.0.tar.gz
| Download URL | underscope-0.1.0.tar.gz |
|---|---|
| Size | 21.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4a62a387284f0627b70e190e9806b7224eea2ef3556045cc4d9144075857e664
|
|
BLAKE2b-256 checksum How to use checksums |
a4c4eac008707ce81946d9ff1611f581d74591d33a549de3d841fc8495c6d27a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.
Transparency logRelease files / underscope-0.1.0-py3-none-any.whl
| Download URL | underscope-0.1.0-py3-none-any.whl |
|---|---|
| Size | 22.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
604358b1370a2014bdf30d7daadcb4ab0b2cce0271170b084c33865f3f59b8c2
|
|
BLAKE2b-256 checksum How to use checksums |
05832619235b65d82bd6202c1b17ba64a5919981264b872b69f8e7b2b10ac3df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.
Transparency log