Skip to content

Using with jaxtyping

jaxtyping checks dtype and shape at run time, via beartype. Its array type sits in the base-type slot of Annotated, leaving the metadata slot free for markers:

Annotated[Float[xr.DataArray, "time x"], Unit("Pa"), Dims("time", "x")]
#         └────────── base type ───────┘  └──────── metadata ────────┘

The full stack

from typing import Annotated

import xarray as xr
from beartype import beartype
from jaxtyping import Float, jaxtyped

import xarray_annotated as xra
from xarray_annotated import Dims, Unit

Pressure = Float[xr.DataArray, "time x"]


@jaxtyped(typechecker=beartype)
@xra.enforce
def total(
    p: Annotated[Pressure, Unit("Pa"), Dims("time", "x")],
    q: Annotated[Pressure, Unit("Pa"), Dims("time", "x")],
) -> Annotated[Pressure, Unit("Pa")]:
    return p + q

Put @jaxtyped outermost

It then sees the call as the caller made it, with @xra.enforce checking underneath.

Naming the array type (Pressure) rather than spelling it inline keeps pyright quiet, since it reads a shape string in an annotation as a forward reference. A type statement counts as an annotation here, so name the array type with an assignment, then use it in type statements as usual:

type PascalPressure = Annotated[Pressure, Unit("Pa")]

What to use for what

Property Use Note
dtype jaxtyping (Float, Int, Float32, unions) More capable than Dtype, which needs no extra dependency
Sizes, and their consistency across arguments jaxtyping Its "time x" are positional size symbols, and names are never looked at
Dim names Dims Checks names but not sizes, so declaring both is not redundant
Coord names Coords
Units Unit
Temporal frequency Freq

Caveats

  • Use beartype, not typeguard. On typeguard 4, jaxtyping’s checks can fail to import or silently pass everything.
  • Base type: annotate(Pressure, unit="Pa") keeps the jaxtyping class.