Skip to main content

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 (or uv.lock) with the rest of your tooling and pipx run ast-metrics just 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 | sh

And 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.

Metadata

Release files for ast-metrics 0.43.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ast-metrics 0.43.0
File Size Uploaded
ast_metrics-0.43.0.tar.gz 15.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ast-metrics 0.43.0
File Interpreter ABI Platform
ast_metrics-0.43.0-py3-none-any.whl Python 3 none any Details

Total release size: 26.8 kB

Release files / ast_metrics-0.43.0.tar.gz

Download URL ast_metrics-0.43.0.tar.gz
Size 15.3 kB
Tags Source
SHA-256 checksum
How to use checksums
13c6ffc562ebb6151f180be9b6b4f4bc8ab28672771b10637331c7f4149f3e0b
BLAKE2b-256 checksum
How to use checksums
4d19032f1e51f2f5187478744db9be780ad26c633d0118e18a733450e2409746
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 Aug 28, 2026.

Transparency log

Release files / ast_metrics-0.43.0-py3-none-any.whl

Download URL ast_metrics-0.43.0-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
12f38e5ab61c701cab75ed69d8cb0a49a2a938d8cc819c7496846a404dccb359
BLAKE2b-256 checksum
How to use checksums
5d6f5794ed2b3aae98c8496d81804a7233cd3c3a6bca4f009415c8bd07c5fea8
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 Aug 28, 2026.

Transparency log

Release history Release notifications | RSS feed

0.43.1

2 release files

This release

0.43.0 This release

2 release files

0.41.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page