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.0rc2

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.0rc2
File Size Uploaded Provenance Actions
xarray_annotated-0.9.0rc2.tar.gz 54.5 kB Without provenance Details for xarray_annotated-0.9.0rc2.tar.gz

Built distribution (wheel)

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

Total release size: 119.7 kB

Release files / xarray_annotated-0.9.0rc2.tar.gz

Download URL xarray_annotated-0.9.0rc2.tar.gz
Size 54.5 kB
Tags Source
SHA-256 checksum
How to use checksums
35c3a6c5221bc235948829797bf2c1d2ec1d6db4de8af82d72002ff52443bd94
BLAKE2b-256 checksum
How to use checksums
1a0a5d1dae640950cec6bec210b4e4c6e36bf8173d4b3d38f56a8d6a302a07d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","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.0rc2-py3-none-any.whl

Download URL xarray_annotated-0.9.0rc2-py3-none-any.whl
Size 65.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
10759856a9c0d1ace24dbb9c2d8874a936b13e10dd8a28c72449a23a569d3964
BLAKE2b-256 checksum
How to use checksums
02a687fe82b473ac2c5a35e751b4873536ef7fea7402b0ea03f08c23696a749d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","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.0rc2

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