xarray-annotated
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 enforce:
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.enforce
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:
- xarray
- pint and pint-xarray, for unit parsing and conversion
- cf-xarray (optional, via the
cfextra), for UDUNITS spellings such as"g m-2 d-1"; see Choosing a unit registry
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:
- xarray-schema and xarray-validate, for validating xarray objects against a schema.
- pint's
wrapsandcheck, decorators that take units or dimensions as arguments rather than from annotations. - TypedUnit, for checking the dimensions of pint quantities from annotations.
- pydantic-pint, for validating and converting pint quantities in Pydantic models.
- astropy's
quantity_input, the same idea for astropy quantities.
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.8.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| xarray_annotated-0.8.0.tar.gz | 43.1 kB | Without provenance | Details for xarray_annotated-0.8.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| xarray_annotated-0.8.0-py3-none-any.whl | Python 3 | none | any | Details for xarray_annotated-0.8.0-py3-none-any.whl |
Total release size: 95.2 kB
Release files / xarray_annotated-0.8.0.tar.gz
| Download URL | xarray_annotated-0.8.0.tar.gz |
|---|---|
| Size | 43.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0ed0b0085beb5e0fceab8b9e217418d874d51fab8faad5e05078b76065f4b3a7
|
|
BLAKE2b-256 checksum How to use checksums |
462f079a0a158f6ffbbb7330b4ccbd6b54d32e9987b656dc7efba716ea920dd2
|
| 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.8.0-py3-none-any.whl
| Download URL | xarray_annotated-0.8.0-py3-none-any.whl |
|---|---|
| Size | 52.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ce7eac2d81e644d8d76f36073ad0f902a337d9c6224dc815db7e7fa617356d1a
|
|
BLAKE2b-256 checksum How to use checksums |
3e0e6599f189c40c76d71527fd7d04679adec699d239184245da6ea4938dd776
|
| 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.8.0
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.