2
2
3
3
4
4
# standard library
5
- from dataclasses import Field
6
5
from functools import partial , wraps
7
6
from types import MethodType
8
- from typing import Any , Callable , Dict , Optional , Type , TypeVar , Union , overload
7
+ from typing import (
8
+ Any ,
9
+ Callable ,
10
+ ClassVar ,
11
+ Optional ,
12
+ Type ,
13
+ TypeVar ,
14
+ Union ,
15
+ overload ,
16
+ )
9
17
10
18
11
19
# dependencies
18
26
# submodules
19
27
from .datamodel import DataModel
20
28
from .dataoptions import DataOptions
21
- from .typing import DataType , Order , Shape , Sizes
22
-
23
-
24
- # constants
25
- DEFAULT_OPTIONS = DataOptions (xr .DataArray )
29
+ from .typing import DataClass , DataClassFields , DataType , Order , Shape , Sizes
26
30
27
31
28
32
# type hints
29
33
P = ParamSpec ("P" )
30
34
TDataArray = TypeVar ("TDataArray" , bound = xr .DataArray )
31
35
32
36
33
- class DataClass (Protocol [P ]):
34
- """Type hint for a dataclass object ."""
37
+ class OptionedClass (Protocol [P , TDataArray ]):
38
+ """Type hint for dataclass objects with options ."""
35
39
36
40
def __init__ (self , * args : P .args , ** kwargs : P .kwargs ) -> None :
37
41
...
38
42
39
- __dataclass_fields__ : Dict [str , Field [Any ]]
43
+ __dataclass_fields__ : ClassVar [DataClassFields ]
44
+ __dataoptions__ : DataOptions [TDataArray ]
40
45
41
46
42
- class DataArrayClass (Protocol [P , TDataArray ]):
43
- """Type hint for a dataclass object with a DataArray factory."""
47
+ # runtime functions
48
+ @overload
49
+ def asdataarray (
50
+ dataclass : OptionedClass [P , TDataArray ],
51
+ reference : Optional [DataType ] = None ,
52
+ dataoptions : None = None ,
53
+ ) -> TDataArray :
54
+ ...
44
55
45
- def __init__ (self , * args : P .args , ** kwargs : P .kwargs ) -> None :
46
- ...
47
56
48
- __dataclass_fields__ : Dict [str , Field [Any ]]
49
- __dataoptions__ : DataOptions [TDataArray ]
57
+ @overload
58
+ def asdataarray (
59
+ dataclass : DataClass [P ],
60
+ reference : Optional [DataType ] = None ,
61
+ dataoptions : None = None ,
62
+ ) -> xr .DataArray :
63
+ ...
50
64
51
65
52
- # runtime functions
53
66
@overload
54
67
def asdataarray (
55
- dataclass : DataArrayClass [ Any , TDataArray ],
68
+ dataclass : OptionedClass [ P , Any ],
56
69
reference : Optional [DataType ] = None ,
57
- dataoptions : DataOptions [Any ] = DEFAULT_OPTIONS ,
70
+ dataoptions : Optional [ DataOptions [TDataArray ]] = None ,
58
71
) -> TDataArray :
59
72
...
60
73
61
74
62
75
@overload
63
76
def asdataarray (
64
- dataclass : DataClass [Any ],
77
+ dataclass : DataClass [P ],
65
78
reference : Optional [DataType ] = None ,
66
- dataoptions : DataOptions [TDataArray ] = DEFAULT_OPTIONS ,
79
+ dataoptions : Optional [ DataOptions [TDataArray ]] = None ,
67
80
) -> TDataArray :
68
81
...
69
82
70
83
71
84
def asdataarray (
72
85
dataclass : Any ,
73
86
reference : Optional [DataType ] = None ,
74
- dataoptions : DataOptions [ Any ] = DEFAULT_OPTIONS ,
87
+ dataoptions : Any = None ,
75
88
) -> Any :
76
89
"""Create a DataArray object from a dataclass object.
77
90
@@ -84,10 +97,11 @@ def asdataarray(
84
97
DataArray object created from the dataclass object.
85
98
86
99
"""
87
- try :
88
- dataoptions = dataclass .__dataoptions__
89
- except AttributeError :
90
- pass
100
+ if dataoptions is None :
101
+ try :
102
+ dataoptions = dataclass .__dataoptions__
103
+ except AttributeError :
104
+ dataoptions = DataOptions (xr .DataArray )
91
105
92
106
model = DataModel .from_dataclass (dataclass )
93
107
item = next (iter (model .data .values ()))
@@ -127,7 +141,7 @@ def __init__(self, func: Any) -> None:
127
141
def __get__ (
128
142
self ,
129
143
obj : Any ,
130
- cls : Type [DataArrayClass [P , TDataArray ]],
144
+ cls : Type [OptionedClass [P , TDataArray ]],
131
145
) -> Callable [P , TDataArray ]:
132
146
...
133
147
@@ -163,7 +177,7 @@ def new(cls: Any, *args: Any, **kwargs: Any) -> Any:
163
177
@overload
164
178
@classmethod
165
179
def shaped (
166
- cls : Type [DataArrayClass [P , TDataArray ]],
180
+ cls : Type [OptionedClass [P , TDataArray ]],
167
181
func : Callable [[Shape ], np .ndarray ],
168
182
shape : Union [Shape , Sizes ],
169
183
** kwargs : Any ,
@@ -209,7 +223,7 @@ def shaped(
209
223
@overload
210
224
@classmethod
211
225
def empty (
212
- cls : Type [DataArrayClass [P , TDataArray ]],
226
+ cls : Type [OptionedClass [P , TDataArray ]],
213
227
shape : Union [Shape , Sizes ],
214
228
order : Order = "C" ,
215
229
** kwargs : Any ,
@@ -251,7 +265,7 @@ def empty(
251
265
@overload
252
266
@classmethod
253
267
def zeros (
254
- cls : Type [DataArrayClass [P , TDataArray ]],
268
+ cls : Type [OptionedClass [P , TDataArray ]],
255
269
shape : Union [Shape , Sizes ],
256
270
order : Order = "C" ,
257
271
** kwargs : Any ,
@@ -293,7 +307,7 @@ def zeros(
293
307
@overload
294
308
@classmethod
295
309
def ones (
296
- cls : Type [DataArrayClass [P , TDataArray ]],
310
+ cls : Type [OptionedClass [P , TDataArray ]],
297
311
shape : Union [Shape , Sizes ],
298
312
order : Order = "C" ,
299
313
** kwargs : Any ,
@@ -335,7 +349,7 @@ def ones(
335
349
@overload
336
350
@classmethod
337
351
def full (
338
- cls : Type [DataArrayClass [P , TDataArray ]],
352
+ cls : Type [OptionedClass [P , TDataArray ]],
339
353
shape : Union [Shape , Sizes ],
340
354
fill_value : Any ,
341
355
order : Order = "C" ,
0 commit comments