Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

xarray-annotated

PyPI version Python versions CI Docs License: MIT

xarray-annotated turns function signatures into explicit, machine-readable contracts for DataArray structure, units and time frequency, so you can check that the functions in a pipeline agree about the arrays passing between them. The contracts are enforced at run time and can also be compared with each other, and they read only metadata and indexes, so a mismatch between stages or in an input can be caught before any data is loaded.

For full user documentation please visit https://jmarshrossney.github.io/xarray-annotated/.

Why?

Why check at all? A mistake that raises an error gets fixed. One that doesn't can produce plausible numbers, which can travel all the way into published results. xarray doesn't check the dimensions, dtype, units or time axis of the arrays a function receives, and an expectation written in a docstring protects nothing. If a mistake can be caught by an automated check, it should be.

Why annotations?

  • One source of truth: an expectation is written once, in the signature, so the checks and the generated documentation cannot drift apart.
  • Machine-readable: annotations are code, so pipeline tools, documentation generators and AI agents can all use them.
  • Usable before running: a check inside a function body only runs when the function does, whereas a declaration can be compared with other declarations and with opened files before anything expensive starts.

Installation

Either using uv (recommended) or pip:

uv add xarray-annotated
# or
pip install xarray-annotated

Usage

Add markers to the Annotated hint of each DataArray parameter or return value, and decorate the function with xra.check_annotations:

from typing import Annotated

import xarray as xr
import xarray_annotated as xra
from xarray_annotated import Dims, Freq, Unit

R = 287.05  # specific gas constant of dry air, J / kg / K


@xra.check_annotations
def daily_air_density(
    p: Annotated[xr.DataArray, Dims("time", "site"), Unit("hPa"), Freq("h")],
    t: Annotated[xr.DataArray, Dims("time", "site"), Unit("K"), Freq("h")],
) -> Annotated[xr.DataArray, Dims("time", "site"), Unit("kg/m^3"), Freq("D")]:
    """Daily mean density of dry air."""
    rho = 100.0 * p / (R * t)
    return rho.resample(time="D").mean().assign_attrs(units="kg/m^3")

Anything that does not match its declaration raises.

A check compares a declaration with another declaration, or with an array's metadata, such as its units attribute. It never reads values, so a wrong calculation with the right label passes, and needs testing like any other code.

Many checks can run pre-flight, before any data is loaded, if a computational graph can be built, as in catena or dask. See Pre-flight checks for more details.

See Also

Uses:

Used by:

  • NERC-CEH/catena - An opinionated integration of Apache Hamilton and xarray for building configurable environmental data pipelines.
  • NERC-CEH/xharvest - Environmental data harvester that standardises data to metadata-rich netCDF or zarr.

Similar tools:

Contributing

Issues and pull requests are welcome. See CONTRIBUTING.md for the package's scope and how to set up a development environment.

Metadata

Release files for xarray-annotated 0.9.0rc1

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

Source distribution (sdist)

Source distribution for xarray-annotated 0.9.0rc1
File Size Uploaded Provenance Actions
xarray_annotated-0.9.0rc1.tar.gz 49.9 kB Without provenance Details for xarray_annotated-0.9.0rc1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xarray-annotated 0.9.0rc1
File Interpreter ABI Platform Actions
xarray_annotated-0.9.0rc1-py3-none-any.whl Python 3 none any Details for xarray_annotated-0.9.0rc1-py3-none-any.whl

Total release size: 110.1 kB

Release files / xarray_annotated-0.9.0rc1.tar.gz

Download URL xarray_annotated-0.9.0rc1.tar.gz
Size 49.9 kB
Tags Source
SHA-256 checksum
How to use checksums
154e91bb6c35856179fd3a991fe59c77fc2b53aa99c2552b3a5d43d193a19421
BLAKE2b-256 checksum
How to use checksums
bc99204de543a6bdf73cc239f06a872e60ef1386b907f181288be0469f458f87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / xarray_annotated-0.9.0rc1-py3-none-any.whl

Download URL xarray_annotated-0.9.0rc1-py3-none-any.whl
Size 60.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a351c1934975faec27e97e901b75feeedeb36123122b2b58be1ab67fecc31c1
BLAKE2b-256 checksum
How to use checksums
c07313f15f3a47affe3c3f59a411ef5b8562c35318b2d1333169ece4112872c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for xarray-annotated 0.9.0rc1

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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

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