Untangled for Python
The Python library and CLI of Untangled,
published on PyPI as untangled. It scans a Python project into the
pairs document defined by the shared spec, following
the Python profile.
Untangled is a re-imagining of snakefood by Martin Blais, and this library is its most direct successor: it parses every file, collects its imports and resolves them against the project's import roots, without running any of its code. See Credit.
It gives the same analyses, HTML reports and DOT graphs as the PHP library for the same pairs document. See what they look like in the examples.
Install
Requires Python 3.11+, and nothing else: the scanner uses Python's own ast
module.
pip install untangled # or: pipx install untangled
untangled --version
utg is a short alias for untangled, and python -m untangled works too.
Untangled reads the project you point it at and never imports or runs it, so
it does not need to be installed into that project's virtualenv. It runs on
Python 3.11 or newer, whatever Python the project targets: a file using syntax
from a newer Python than the one running Untangled is read statement by
statement, which finds the same imports except in the rare statement that
still does not parse, and the scan says which files were read that way.
Commands
untangled scan [<path>] [scan options] [--format=json|snakefood] [--output=<file>]
untangled analyze [<path>|<pairs.json>] [scan options] [filter options] [--output=<file>]
untangled report [<path>|<pairs.json>] [scan options] [filter options]
[--format=html|dot] [--style=classic] [--title=<text>] [--output=<file>]
untangled dependents [<path>|<pairs.json>] <file-or-symbol>... [scan options] [--include/--exclude ...]
[--transitive] [--max-depth=<n>] [--through=<glob>]...
[--format=text|json] [--output=<file>]
Each command runs the steps before it: analyze scans first, and report
scans and analyzes first. analyze and report also accept a pairs document
saved by scan (from this library or any other Untangled library), so a large
project can be scanned once and analyzed many ways. untangled --help prints
the same summary.
scan
Writes every file-to-file dependency of the project as a pairs document.
untangled scan /path/to/project # pairs document (JSON) to stdout
untangled scan /path/to/project --output=pairs.json # ... to a file
untangled scan /path/to/project --dev # include tests
untangled scan /path/to/project --format=snakefood # snakefood's format, for sfood-* tools
analyze
Writes cycles and coupling metrics as JSON (the analysis document): a summary, per-unit fan-in, fan-out and instability, and every dependency cycle with one example loop.
untangled analyze /path/to/project # per file
untangled analyze /path/to/project --cluster=namespace:2 # per package path, e.g. shop.billing
untangled analyze /path/to/project --cluster=folder:2 --exclude='src/shop/legacy/**'
untangled analyze pairs.json --cluster=namespace:2 # from a saved scan
What each number means, and why it matters, is in metrics.
dependents
Lists the files that depend on one or more files, before you change them or when you choose which tests to run. A target is a node id (a file path) or, where the scanner records them, a declared symbol (the Python scanner records none, so use files).
untangled dependents /path/to/project src/shop/billing/tax.py
untangled dependents /path/to/project src/shop/billing/tax.py --transitive
untangled dependents pairs.json src/shop/billing/tax.py --transitive --format=json
untangled dependents /path/to/project src/shop/billing/tax.py --transitive --exclude='**' --include='tests/**'
By default it lists the direct dependents. --transitive adds the files that
depend on those, and so on, each with its depth (the shortest distance from a
target). --include and --exclude filter the list, not the walk, so a test
that reaches a target only through application code is still found (scan with
--dev to have tests in the graph). The text form groups files by folder;
--format=json writes the document of the
spec.
The first argument is the source when it is a folder or a .json file; any
other argument is a target, and the source is the current folder. A target
that matches nothing exits with code 1.
To choose the tests to run after a change, limit the walk instead of the list.
--max-depth=N (with --transitive) stops after N steps from the targets, and
--through=<glob> (repeatable, any match counts) only walks into files that
match, so nothing is found by way of other files:
# tests that use the file, or reach it only through other tests (helpers, builders, base classes)
untangled dependents /path/to/project src/shop/billing/tax.py --transitive --through='tests/**'
# application code that reaches it, not through tests
untangled dependents /path/to/project src/shop/billing/tax.py --transitive --through='src/**'
# tests of the files that use it directly
untangled dependents /path/to/project src/shop/billing/tax.py --transitive --max-depth=2 --exclude='**' --include='tests/**'
A file that --through leaves out is never listed, even when it uses a target
directly, and it is not walked through. --include and --exclude still only
filter the list afterwards, and --max-depth without --transitive is a usage
error (exit code 2). A target can also be an absolute path inside the project,
or a path with ./ or \ separators.
report
Writes one self-contained HTML page meant for people who do not read code: a summary in plain words, an interactive map of the dependencies, each cycle written out as a loop, the parts to look at first, and every metric with a short explanation. It needs no network access to open.
untangled report /path/to/project --output=report.html
untangled report /path/to/project --cluster=namespace:2 --title="Billing" --output=report.html
untangled report /path/to/project --format=dot | dot -Tsvg > graph.svg
untangled report /path/to/project --format=dot --style=classic | dot -Tpng > graph.png
--format=dot writes a Graphviz file instead, with units in a cycle drawn in
red. --style=classic draws the black and white look of snakefood's
sfood-graph.
The file asks for Graphviz's dot layout itself, so other Graphviz programs
and viewers lay it out the same way. On a large project, render a clustered
graph (--cluster=namespace:2, or folder:2) rather than one node per file:
thousands of files take dot minutes and come out too small to read.
Options
| Option | Meaning |
|---|---|
--dev |
Also scan tests: test/ and tests/ folders, test_*.py, *_test.py, conftest.py. Tests usually depend on everything, which hides the real structure. |
--source=PATH |
Add a folder, relative to the project, to the import roots (repeatable), for code importable from a folder the scanner does not find by itself. |
--venv=PATH |
Read installed packages from this virtualenv instead of the ones found in the project (repeatable). |
--format=FORMAT |
scan: json (the pairs document, default) or snakefood. report: html (default) or dot. |
--style=classic |
report --format=dot only: snakefood's black and white look. |
--title=TEXT |
report only: the report's title. Defaults to the project name in pyproject.toml, else the folder name. |
--cluster=STRATEGY |
analyze, report: group files into units. namespace:N keeps the first N segments of the module name (shop.billing.invoice → shop.billing with N=2), folder:N the first N folders, and rules:<file> reads <prefix> <cluster> lines from a file, the first matching path prefix wins. See clustering. |
--transitive |
dependents: also list the files that depend on a dependent, with their depth. |
--max-depth=N |
dependents: with --transitive, stop after N steps from the targets (a positive integer). |
--through=GLOB |
dependents: only walk into files matching GLOB; nothing is found by way of other files. Repeatable, any match counts. |
--strict |
analyze, report: with rules:<file>, exit 1 instead of warning when a file matches no rule (a file no rule matches is its own unit, and the warning names it). |
--include=GLOB, --exclude=GLOB |
analyze, report: keep or drop project files by path (repeatable, the last matching pattern wins). * stays within a folder, ** crosses folders. |
--output=FILE |
Write to a file instead of standard output. |
--diagnostics=FORMAT |
How warnings and errors are written to standard error: text (default), or json, one object per line. See Diagnostics for programs. |
What it reads
- Files: every
.pyfile under the project folder, except virtualenvs, hidden folders,__pycache__,node_modules,site-packages,*.egg-info, andbuild/anddist/at the top. Tests only with--dev, unless real code imports them. Stubs (.pyi), Cython sources (.pyx) and compiled extensions (.so,.pyd) are import targets, but are not read. - Import roots: the project folder,
src/(the src layout), the folderspyproject.tomlgives setuptools, and--sourcefolders. - Packages: the project's virtualenv (any folder in it with a
pyvenv.cfg, such as.venv/), read from each installed distribution'sRECORD, soimport yamlispypi:pyyaml. Without one, an import matches a dependency declared inpyproject.tomlorrequirements*.txtonly when the names are equal (import jaraco.abodematches a declaredjaraco.abode).
Every import ends up as a project file, a standard library module
(builtin:os), a package (pypi:requests) or an unresolved reference
(unresolved:module:...). The scanner never guesses: an import of a module
that does not exist is unresolved, never an edge to its parent package. The
Python profile has the full rules.
Exit codes and warnings
0 on success, 1 when the project folder does not exist, a pairs document
or rules file cannot be read, the output cannot be written, or --strict finds a file no rule
matches, 2 for a usage
error. Warnings go to standard error and
do not change the exit code: a file that does not parse (such as Python 2
code; it stays in the pairs document marked "error": "parse", and the report
lists it under "Files we could not read"), files read statement by statement,
finding no files to scan, a --source or --venv that does not exist, and unresolved imports
of modules outside the project when no virtualenv was found.
Diagnostics for programs
Everything the commands have to say about a project goes to standard error;
standard output holds only the result. By default that is text, one
warning: ... or error: ... line each. --diagnostics=json writes one JSON
object per line instead, with a stable code to branch on, so a CI job or an
agent never has to read the wording of a message:
untangled scan /path/to/project --diagnostics=json --output=pairs.json 2> diagnostics.jsonl
jq -r 'select(.code == "uncovered-files") | .files[]' diagnostics.jsonl
{"level":"warning","code":"file-unparseable","message":"bad.py: cannot parse as Python 3: ...","files":["bad.py"]}
level is warning or error (the reason a command stopped, with exit code
1), code names the condition, message is for people, and files lists the
project-relative paths it is about when there are any. The exit code and the
standard output do not change. The codes are listed in the
diagnostics specification,
which also says how to read the lines. --diagnostics=github (annotations for
GitHub Actions) is not supported yet; a formatter can be built on the JSON
lines. A usage error (exit code 2) is always text.
From Python, Scanner.scan() returns a ScanResult whose diagnostics are
untangled.diagnostics.Diagnostic objects (level, code, message, files,
to_dict(), to_json()); its older warnings list of strings is kept.
Development
cd python
python -m pip install -e . --group dev
pytest
ruff check . && ruff format --check .
The tests include the shared conformance cases
that have a python/ folder.
License
GPL-2.0-only, see LICENSE. This folder may port code from snakefood, which is GPL, so code here must never be copied into the MIT-licensed folders.
Metadata
Release files for untangled 1.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 | Provenance | Actions |
|---|---|---|---|---|
| untangled-1.1.0.tar.gz | 103.0 kB | With provenance | Details for untangled-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| untangled-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for untangled-1.1.0-py3-none-any.whl |
Total release size: 175.8 kB
Release files / untangled-1.1.0.tar.gz
| Download URL | untangled-1.1.0.tar.gz |
|---|---|
| Size | 103.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b2f522593efe498152a501daee181734a9669ea754bb1d2352c110d4778b198d
|
|
BLAKE2b-256 checksum How to use checksums |
1ba7e087671306c6d6d6b43205277b96a78b11b6c51e14065999c5d82706872a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 5, 2026.
Transparency logRelease files / untangled-1.1.0-py3-none-any.whl
| Download URL | untangled-1.1.0-py3-none-any.whl |
|---|---|
| Size | 72.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ffa39f3ab71e9f46f5b89e7d26a4d7b442d431b0c3d81e561182947a78cf6190
|
|
BLAKE2b-256 checksum How to use checksums |
100958a82d92cad2932b879c4ef2c2bde4763a8edb31530a75283d380d8fa12d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 5, 2026.
Transparency logSecurity for untangled 1.1.0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for untangled-1.1.0.tar.gz
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 Nov 5, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.