Skip to content

Commit 5e59752

Browse files
committed
#237 Add typing module for tags and type aliases
1 parent ae6ca8f commit 5e59752

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

xarray_dataclasses/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
1+
__all__ = [
2+
# submodules
3+
"typing",
4+
# aliases
5+
"Attr",
6+
"Attrs",
7+
"Coord",
8+
"Coords",
9+
"Data",
10+
"DataVars",
11+
"Factory",
12+
"Name",
13+
"Tag",
14+
]
115
__version__ = "2.0.0"
16+
17+
18+
# submodules
19+
from . import typing
20+
21+
22+
# aliases
23+
from .typing import *

xarray_dataclasses/typing.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)