AST Metrics for Python
AST Metrics is a static analyzer that tells you which parts of a codebase are risky to touch. This package installs it with pip, like any other dev tool: no compiler, no service. The analyzer is a single binary, downloaded once and cached.
You may not need this package. It exists for one thing: getting the analyzer through pip, so it lands in
requirements.txt(oruv.lock) with the rest of your tooling andpipx run ast-metricsjust works. If that is not what you are after, install the analyzer directly:brew install ast-metrics/tap/ast-metrics # macOS, Linux curl -fsSL https://install.ast-metrics.dev | shAnd on pull requests, there is a ready-made action:
ast-metrics/action-ast-metrics@v2.
Usage
Without installing anything:
pipx run ast-metrics analyze src
Or as a dev dependency, which pins the analyzer version in your lockfile:
pip install ast-metrics # add it to requirements-dev.txt yourself
# or, with uv:
uv add --dev ast-metrics
ast-metrics analyze src
ast-metrics analyze --report-html=./report src
If the virtualenv's bin (or Scripts) directory is not on PATH, run it as
a module instead: python -m ast_metrics analyze src. It behaves exactly like
the ast-metrics command.
Everything else: ast-metrics --help, or
ast-metrics.dev.
What gets analyzed
Python (.py), and, in the same run, Go, PHP, TypeScript, Rust,
Java and C#. Most codebases are not made of one language, and the metrics
are comparable across all of them.
In CI
- run: pipx run ast-metrics analyze --ci src
--ci writes the HTML and Markdown reports and keeps the output plain.
Reports for a quality gate: --report-sarif=<file> (read by GitHub code
scanning and GitLab), --report-json=<file>, --report-openmetrics=<file>.
The binary is downloaded once per version and cached in
$AST_METRICS_CACHE_DIR, which defaults to $XDG_CACHE_HOME/ast-metrics or
~/.cache/ast-metrics, so caching that directory between builds removes the
download entirely.
Pre-commit hook
This repository ships a .pre-commit-hooks.yaml:
repos:
- repo: https://github.com/ast-metrics/ast-metrics-bridge-python
rev: v0.41.1
hooks:
- id: ast-metrics
Without a .ast-metrics.yaml configuration file that sets requirements,
ast-metrics lint has nothing to check and does nothing. Run ast-metrics init once to create one.
Configuration
All optional.
| Variable | Effect |
|---|---|
AST_METRICS_BINARY |
Path to a binary to use as-is, nothing downloaded: air-gapped installs, distribution packages. |
AST_METRICS_VERSION |
Release tag to download, or latest. Defaults to the pinned version. |
AST_METRICS_CACHE_DIR |
Where binaries are cached. Defaults to $XDG_CACHE_HOME/ast-metrics, or ~/.cache/ast-metrics if unset (%LOCALAPPDATA%\ast-metrics on Windows). |
HTTP_PROXY, HTTPS_PROXY, NO_PROXY |
Honoured for the download by urllib, the standard-library HTTP client this package uses. |
The analyzer version is pinned by this package rather than resolved to
latest, so a locked requirements.txt (or uv.lock) gives a reproducible
analysis. The package version matches the analyzer version it installs:
ast-metrics==0.41.1 runs AST Metrics 0.41.1. To upgrade, pip install --upgrade ast-metrics or uv lock --upgrade-package ast-metrics.
Unlike npm, pip has no post-install hook: nothing is downloaded at pip install time. The binary is fetched on the first run of ast-metrics (or
python -m ast_metrics), then cached for every run after that. On an
air-gapped or hermetic install, where that first run cannot reach the
network, install the binary yourself and set
AST_METRICS_BINARY=/path/to/ast-metrics before running this package.
Contributing
See CONTRIBUTING.md. Analyzer bugs go to ast-metrics/ast-metrics; this repository is the Python bridge.
License
MIT. See LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ast_metrics-0.41.1.tar.gz.
File metadata
- Download URL: ast_metrics-0.41.1.tar.gz
- Upload date:
- Size: 15.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
087613057b5a01156fef9a82519ca7285d77a25bff1e03536c71f808cb1f6d7b
|
|
| MD5 |
3cfabd8bdd9f5e56e95a4a63f818cad5
|
|
| BLAKE2b-256 |
e949b126658bd0eeee66312c8eea111c73ae4d8d9f23545a22ca6493c50bef57
|
Provenance
The following attestation bundles were made for ast_metrics-0.41.1.tar.gz:
Publisher:
publish.yml on ast-metrics/ast-metrics-bridge-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ast_metrics-0.41.1.tar.gz -
Subject digest:
087613057b5a01156fef9a82519ca7285d77a25bff1e03536c71f808cb1f6d7b - Sigstore transparency entry: 2476433281
- Sigstore integration time:
-
Permalink:
ast-metrics/ast-metrics-bridge-python@194d16f0cbcf592ecb9234b92365c0230d8a48a8 -
Branch / Tag:
refs/tags/v0.41.1 - Owner: https://github.com/ast-metrics
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@194d16f0cbcf592ecb9234b92365c0230d8a48a8 -
Trigger Event:
push
-
Statement type:
File details
Details for the file ast_metrics-0.41.1-py3-none-any.whl.
File metadata
- Download URL: ast_metrics-0.41.1-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4fbb03fe54eccc3a9aac857a4389f6cec66ebf56f726d495d2f54dc56295e7b7
|
|
| MD5 |
fa8ad6984a923c121850f5b44335affc
|
|
| BLAKE2b-256 |
feb005ed6654ff97d5a743d92c755beffb056287fa90c7e5d174dc68149daf21
|
Provenance
The following attestation bundles were made for ast_metrics-0.41.1-py3-none-any.whl:
Publisher:
publish.yml on ast-metrics/ast-metrics-bridge-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ast_metrics-0.41.1-py3-none-any.whl -
Subject digest:
4fbb03fe54eccc3a9aac857a4389f6cec66ebf56f726d495d2f54dc56295e7b7 - Sigstore transparency entry: 2476433382
- Sigstore integration time:
-
Permalink:
ast-metrics/ast-metrics-bridge-python@194d16f0cbcf592ecb9234b92365c0230d8a48a8 -
Branch / Tag:
refs/tags/v0.41.1 - Owner: https://github.com/ast-metrics
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@194d16f0cbcf592ecb9234b92365c0230d8a48a8 -
Trigger Event:
push
-
Statement type: