v1.2.0 (2022-06-09)
From this release, special type hints (Attr
, Coord
, Data
, Name
) can be used within union types.
This would be useful if you want to customize values after dataclass object creation (__post_init__
).
The following example automatically sets ranged values to x and y coordinates if they are not specified.
import numpy as np
from dataclasses import dataclass
from typing import Literal, Optional
from xarray_dataclasses import AsDataArray, Coord, Data
X = Literal["x"]
Y = Literal["y"]
@dataclass
class Image(AsDataArray):
"""2D image as DataArray."""
data: Data[tuple[X, Y], float]
x: Optional[Coord[X, int]] = None
y: Optional[Coord[Y, int]] = None
def __post_init__(self) -> None:
"""Set ranged values to x and y."""
shape = np.shape(self.data)
if self.x is None:
self.x = np.arange(shape[0])
if self.y is None:
self.y = np.arange(shape[1])
What's Changed
- Update static type check by @astropenguin in #153
- Update typing module by @astropenguin in #158
- Release v1.2.0 by @astropenguin in #161
Full Changelog: v1.1.0...v1.2.0