cargo-metadata
cargo-metadata provides Pydantic models for the JSON output of
cargo metadata --format-version 1, plus a small runner function that invokes
the command and returns validated data.
Installation
pip install cargo-metadata
What it includes
Metadataand related model types (Package,Dependency,Target,Resolve, ...)run(...)helper to executecargo metadataand parse the result
Unknown fields from Cargo are allowed and preserved on parsed model instances, which helps with forward compatibility when Cargo adds fields. Refer to the Pydantic documentation for how to interact with unknown fields.
Quick start
Run cargo metadata and parse directly:
from cargo_metadata import run
meta = run(no_deps=True)
print(meta.version)
print(meta.workspace_root)
print([pkg.name for pkg in meta.packages])
Parse existing JSON data:
import json
from cargo_metadata import Metadata
data = json.loads(raw_json)
meta = Metadata.model_validate(data)
for pkg in meta.packages:
print(pkg.name, pkg.version)
Scope
This package focuses on typed parsing and a thin command wrapper.
It does not provide:
- project mutation APIs
- custom Cargo invocation strategies beyond the exposed flags
- higher-level dependency analysis helpers
Snapshot tests
This repository includes a snapshot test for the Pydantic JSON schema of
cargo_metadata.Metadata at tests/snapshots/metadata.schema.json.
Run snapshot tests only:
uv run pytest tests/test_schema_snapshot.py
Accept and update snapshots when running tests:
uv run pytest --snapshot-update
When the schema changes, the snapshot diff appears in git, making the change reviewable.
Requirements
- Python 3.10+
cargoavailable onPATHwhen usingrun(...)
All versions of Cargo should work. However, we do not regularly test compatibility with old versions of Cargo.
LLM disclosure
LLMs were used to generate code used in this package. All code has been fully signed off, owned, and tested by the human(s) running the LLM tooling.
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 cargo_metadata-1.0.0.tar.gz.
File metadata
- Download URL: cargo_metadata-1.0.0.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e4bd1c4d42ec39c13e09620cf88b8b532c90699be056db0e5a7378340324f8a
|
|
| MD5 |
40ec0b631ec5ec38424c1691834c802a
|
|
| BLAKE2b-256 |
7ef9da13d88af0d83b393be376dcaa613921a7428226272f3e194289efcd8c1b
|
Provenance
The following attestation bundles were made for cargo_metadata-1.0.0.tar.gz:
Publisher:
publish.yml on canonical/py-cargo-metadata
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cargo_metadata-1.0.0.tar.gz -
Subject digest:
2e4bd1c4d42ec39c13e09620cf88b8b532c90699be056db0e5a7378340324f8a - Sigstore transparency entry: 2414110135
- Sigstore integration time:
-
Permalink:
canonical/py-cargo-metadata@4d31888de1916df02448f0f0e2f408944bc01069 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/canonical
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4d31888de1916df02448f0f0e2f408944bc01069 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file cargo_metadata-1.0.0-py3-none-any.whl.
File metadata
- Download URL: cargo_metadata-1.0.0-py3-none-any.whl
- Upload date:
- Size: 10.9 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 |
ce36a389069d2fe4a501e575e40045b763ac1e8aed839cb63c0128623ba1d34e
|
|
| MD5 |
863dd6a9d4db1be28952767b698179cd
|
|
| BLAKE2b-256 |
a87014fdba47e7d22621b6a8635390e09e74c24c09aac9b6cff958c296a307b0
|
Provenance
The following attestation bundles were made for cargo_metadata-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on canonical/py-cargo-metadata
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cargo_metadata-1.0.0-py3-none-any.whl -
Subject digest:
ce36a389069d2fe4a501e575e40045b763ac1e8aed839cb63c0128623ba1d34e - Sigstore transparency entry: 2414110320
- Sigstore integration time:
-
Permalink:
canonical/py-cargo-metadata@4d31888de1916df02448f0f0e2f408944bc01069 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/canonical
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4d31888de1916df02448f0f0e2f408944bc01069 -
Trigger Event:
workflow_dispatch
-
Statement type: