Working with declarations¶
A declaration is data as well as a check, so tools can read one from a signature, write one into a signature and compare it with another, all with no array in hand.
Reading declarations¶
from typing import Annotated, TypedDict
import xarray as xr
from xarray_annotated import Dims, Dtype, Unit, declarations_from_signature
class Output(TypedDict):
gpp: Annotated[xr.DataArray, Unit("g m-2 d-1")]
diagnostic: xr.DataArray
def node(
x: Annotated[xr.DataArray, Dims("time", "x"), Dtype("float64"), Unit("degC")],
plain: xr.DataArray,
) -> Output: ...
inputs, output = declarations_from_signature(node)
# inputs == {"x": Declared(unit=Unit("degC"), dims=Dims("time", "x"), dtype=Dtype("float64"))}
# output == {"gpp": Declared(unit=Unit("g m-2 d-1"))}
- Omitted: parameters and fields with no declaration.
- Return shape: a dict for a
TypedDictor dataclass, including one writtenR | NoneorR[int], oneDeclaredfor a single array, otherwiseNone. - Slots: each slot of a
Declaredholds a marker orNone, and.schemalists the structural markers. - One hint: use
declaration_from_hint.
Writing declarations¶
annotate is the inverse: it builds a real Annotated hint, from raw values or markers.
from xarray_annotated import annotate
hint = annotate(unit="Pa", dims=("time", "x"), dtype="float64", freq="7D")
# Annotated[xr.DataArray, Unit("Pa"), Dims("time", "x"), Dtype("float64"), Freq("7D")]
Assign the result to a function’s __annotations__ and @xra.enforce reads it as if it were hand-written.
The first positional argument is the base type, so a jaxtyping array type can be passed there.
Comparing declarations¶
Each declaration can look fine on its own and still disagree with the next stage.
declaration_conflicts compares two of them, as read above:
@xra.enforce
def to_pascals(
p: Annotated[xr.DataArray, Unit("hPa")],
) -> Annotated[xr.DataArray, Unit("Pa")]:
return xra.units.convert(p, "Pa")
@xra.enforce
def altitude(
p: Annotated[xr.DataArray, Unit("hPa")],
) -> Annotated[xr.DataArray, Unit("m")]:
alt = 44330.0 * (1.0 - (p / 1013.25) ** 0.1903)
return alt.assign_attrs(units="m")
_, produced = xra.declarations_from_signature(to_pascals)
consumed, _ = xra.declarations_from_signature(altitude)
xra.declaration_conflicts(produced, consumed["p"])
# ["unit: Unit('Pa') and Unit('hPa') are different units"]
- Partly annotated pipelines: a facet declared on only one side cannot conflict, so you can adopt this one function at a time.
- Units: they conflict unless equal, matching
enforce.units_compatiblealone would pass this example, whichenforcewould then refuse. Coords: they never conflict, since each declaration only requires names to be present.- Several consumers: compare all pairs of the declarations for one array, not each against the first. A loose
DimsorDtypeis compatible with stricter ones that conflict with each other. - No conflict is not proof:
Freq("D", gaps_ok=True)is compatible withFreq("D"), which still raises on an axis with holes when the data arrives. - Malformed declarations: these raise
ValueErrorrather than conflict, andassert_valid_declarationasks only that. - The switch: a conflict is a fact about declarations, so this ignores
xra.enabled. A tool that should stay quiet can askresolve_enabledfirst.
To run these comparisons across a pipeline, and against its input files, see Pre-flight checks.