|
| 1 | +__all__ = [ |
| 2 | + "Attr", |
| 3 | + "Attrs", |
| 4 | + "Coord", |
| 5 | + "Coords", |
| 6 | + "Data", |
| 7 | + "DataVars", |
| 8 | + "Factory", |
| 9 | + "Name", |
| 10 | + "Tag", |
| 11 | +] |
| 12 | + |
| 13 | + |
| 14 | +# standard library |
| 15 | +from collections.abc import Collection as Collection_, Hashable |
| 16 | +from enum import auto |
| 17 | +from typing import Annotated, Callable, Protocol, TypeVar, Union |
| 18 | + |
| 19 | + |
| 20 | +# dependencies |
| 21 | +from dataspecs import TagBase |
| 22 | +from xarray import DataArray, Dataset |
| 23 | + |
| 24 | + |
| 25 | +# type hints |
| 26 | +TAny = TypeVar("TAny") |
| 27 | +TDims = TypeVar("TDims", covariant=True) |
| 28 | +TDtype = TypeVar("TDtype", covariant=True) |
| 29 | +THashable = TypeVar("THashable", bound=Hashable) |
| 30 | +TXarray = TypeVar("TXarray", bound="Xarray") |
| 31 | +Xarray = Union[DataArray, Dataset] |
| 32 | + |
| 33 | + |
| 34 | +class Collection(Collection_[TDtype], Protocol[TDims, TDtype]): |
| 35 | + """Same as Collection[T] but accepts additional type variable for dims.""" |
| 36 | + |
| 37 | + pass |
| 38 | + |
| 39 | + |
| 40 | +# constants |
| 41 | +class Tag(TagBase): |
| 42 | + """Collection of xarray-related tags for annotating type hints.""" |
| 43 | + |
| 44 | + ATTR = auto() |
| 45 | + """Tag for specifying an attribute of DataArray/set.""" |
| 46 | + |
| 47 | + COORD = auto() |
| 48 | + """Tag for specifying a coordinate of DataArray/set.""" |
| 49 | + |
| 50 | + DATA = auto() |
| 51 | + """Tag for specifying a data object of DataArray/set.""" |
| 52 | + |
| 53 | + DIMS = auto() |
| 54 | + """Tag for specifying a dims object of DataArray/set.""" |
| 55 | + |
| 56 | + DTYPE = auto() |
| 57 | + """Tag for specifying a dtype object of DataArray/set.""" |
| 58 | + |
| 59 | + FACTORY = auto() |
| 60 | + """Tag for specifying a factory of DataArray/set.""" |
| 61 | + |
| 62 | + MULTIPLE = auto() |
| 63 | + """Tag for specifying multiple items (attrs, coords, data vars).""" |
| 64 | + |
| 65 | + NAME = auto() |
| 66 | + """Tag for specifying an item name (attr, coord, data). """ |
| 67 | + |
| 68 | + |
| 69 | +# type aliases |
| 70 | +Arrayable = Collection[Annotated[TDims, Tag.DIMS], Annotated[TDtype, Tag.DTYPE]] |
| 71 | +"""Type alias for Collection[TDims, TDtype] annotated by tags.""" |
| 72 | + |
| 73 | +Attr = Annotated[TAny, Tag.ATTR] |
| 74 | +"""Type alias for an attribute of DataArray/set.""" |
| 75 | + |
| 76 | +Attrs = Annotated[dict[str, TAny], Tag.ATTR, Tag.MULTIPLE] |
| 77 | +"""Type alias for attributes of DataArray/set.""" |
| 78 | + |
| 79 | +Coord = Annotated[Arrayable[TDims, TDtype], Tag.COORD] |
| 80 | +"""Type alias for a coordinate of DataArray/set.""" |
| 81 | + |
| 82 | +Coords = Annotated[dict[str, Arrayable[TDims, TDtype]], Tag.COORD, Tag.MULTIPLE] |
| 83 | +"""Type alias for coordinates of DataArray/set.""" |
| 84 | + |
| 85 | +Data = Annotated[Arrayable[TDims, TDtype], Tag.DATA] |
| 86 | +"""Type alias for a data object of DataArray/set.""" |
| 87 | + |
| 88 | +DataVars = Annotated[dict[str, Arrayable[TDims, TDtype]], Tag.DATA, Tag.MULTIPLE] |
| 89 | +"""Type alias for data objects of DataArray/set.""" |
| 90 | + |
| 91 | +Factory = Annotated[Callable[..., TXarray], Tag.FACTORY] |
| 92 | +"""Type alias for a factory of DataArray/set.""" |
| 93 | + |
| 94 | +Name = Annotated[THashable, Tag.NAME] |
| 95 | +"""Type alias for an item name (attr, coord, data).""" |
0 commit comments