Skip to main content

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

  • Metadata and related model types (Package, Dependency, Target, Resolve, ...)
  • run(...) helper to execute cargo metadata and 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+
  • cargo available on PATH when using run(...)

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

cargo_metadata-1.0.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cargo_metadata-1.0.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

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

Hashes for cargo_metadata-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2e4bd1c4d42ec39c13e09620cf88b8b532c90699be056db0e5a7378340324f8a
MD5 40ec0b631ec5ec38424c1691834c802a
BLAKE2b-256 7ef9da13d88af0d83b393be376dcaa613921a7428226272f3e194289efcd8c1b

See more details on using hashes here.

Provenance

The following attestation bundles were made for cargo_metadata-1.0.0.tar.gz:

Publisher: publish.yml on canonical/py-cargo-metadata

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for cargo_metadata-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ce36a389069d2fe4a501e575e40045b763ac1e8aed839cb63c0128623ba1d34e
MD5 863dd6a9d4db1be28952767b698179cd
BLAKE2b-256 a87014fdba47e7d22621b6a8635390e09e74c24c09aac9b6cff958c296a307b0

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page