28
28
# type hints
29
29
P = ParamSpec ("P" )
30
30
TDataArray = TypeVar ("TDataArray" , bound = xr .DataArray )
31
- TDataArray_ = TypeVar ("TDataArray_" , bound = xr .DataArray , contravariant = True )
32
31
33
32
34
33
class DataClass (Protocol [P ]):
35
34
"""Type hint for a dataclass object."""
36
35
37
- __init__ : Callable [P , None ]
38
- __dataclass_fields__ : Dict [str , Field [Any ]]
39
-
40
-
41
- class DataArrayClass (Protocol [P , TDataArray_ ]):
42
- """Type hint for a dataclass object with a DataArray factory."""
36
+ def __init__ (self , * args : P .args , ** kwargs : P .kwargs ) -> None :
37
+ ...
43
38
44
- __init__ : Callable [P , None ]
45
39
__dataclass_fields__ : Dict [str , Field [Any ]]
46
- __dataoptions__ : DataOptions [TDataArray_ ]
47
40
48
41
49
- # custom classproperty
50
- class classproperty :
51
- """Class property only for AsDataArray.new().
52
-
53
- As a classmethod and a property can be chained together since Python 3.9,
54
- this class will be removed when the support for Python 3.7 and 3.8 ends.
55
-
56
- """
42
+ class DataArrayClass (Protocol [P , TDataArray ]):
43
+ """Type hint for a dataclass object with a DataArray factory."""
57
44
58
- def __init__ (self , func : Callable [..., Callable [ P , TDataArray ]] ) -> None :
59
- self . __func__ = func
45
+ def __init__ (self , * args : P . args , ** kwargs : P . kwargs ) -> None :
46
+ ...
60
47
61
- def __get__ (
62
- self ,
63
- obj : Any ,
64
- cls : Type [DataArrayClass [P , TDataArray ]],
65
- ) -> Callable [P , TDataArray ]:
66
- return self .__func__ (cls )
48
+ __dataclass_fields__ : Dict [str , Field [Any ]]
49
+ __dataoptions__ : DataOptions [TDataArray ]
67
50
68
51
69
- # runtime functions and classes
52
+ # runtime functions
70
53
@overload
71
54
def asdataarray (
72
55
dataclass : DataArrayClass [Any , TDataArray ],
73
56
reference : Optional [DataType ] = None ,
74
- dataoptions : Any = DEFAULT_OPTIONS ,
57
+ dataoptions : DataOptions [ Any ] = DEFAULT_OPTIONS ,
75
58
) -> TDataArray :
76
59
...
77
60
@@ -87,8 +70,8 @@ def asdataarray(
87
70
88
71
def asdataarray (
89
72
dataclass : Any ,
90
- reference : Any = None ,
91
- dataoptions : Any = DEFAULT_OPTIONS ,
73
+ reference : Optional [ DataType ] = None ,
74
+ dataoptions : DataOptions [ Any ] = DEFAULT_OPTIONS ,
92
75
) -> Any :
93
76
"""Create a DataArray object from a dataclass object.
94
77
@@ -134,36 +117,82 @@ def asdataarray(
134
117
return dataarray
135
118
136
119
120
+ # runtime classes
121
+ class classproperty :
122
+ """Class property only for AsDataArray.new().
123
+
124
+ As a classmethod and a property can be chained together since Python 3.9,
125
+ this class will be removed when the support for Python 3.7 and 3.8 ends.
126
+
127
+ """
128
+
129
+ def __init__ (self , func : Any ) -> None :
130
+ self .__func__ = func
131
+
132
+ @overload
133
+ def __get__ (
134
+ self ,
135
+ obj : Any ,
136
+ cls : Type [DataArrayClass [P , TDataArray ]],
137
+ ) -> Callable [P , TDataArray ]:
138
+ ...
139
+
140
+ @overload
141
+ def __get__ (
142
+ self ,
143
+ obj : Any ,
144
+ cls : Type [DataClass [P ]],
145
+ ) -> Callable [P , xr .DataArray ]:
146
+ ...
147
+
148
+ def __get__ (self , obj : Any , cls : Any ) -> Any :
149
+ return self .__func__ (cls )
150
+
151
+
137
152
class AsDataArray :
138
153
"""Mix-in class that provides shorthand methods."""
139
154
140
- __dataoptions__ = DEFAULT_OPTIONS
141
-
142
155
@classproperty
143
- def new (cls : Type [ DataArrayClass [ P , TDataArray ]] ) -> Callable [ P , TDataArray ] :
156
+ def new (cls : Any ) -> Any :
144
157
"""Create a DataArray object from dataclass parameters."""
145
158
146
159
init = copy (cls .__init__ )
147
- init .__annotations__ ["return" ] = TDataArray
148
160
init .__doc__ = cls .__init__ .__doc__
161
+ init .__annotations__ ["return" ] = TDataArray
149
162
150
163
@wraps (init )
151
- def new (
152
- cls : Type [DataArrayClass [P , TDataArray ]],
153
- * args : P .args ,
154
- ** kwargs : P .kwargs ,
155
- ) -> TDataArray :
164
+ def new (cls : Any , * args : Any , ** kwargs : Any ) -> Any :
156
165
return asdataarray (cls (* args , ** kwargs ))
157
166
158
167
return MethodType (new , cls )
159
168
169
+ @overload
160
170
@classmethod
161
171
def empty (
162
172
cls : Type [DataArrayClass [P , TDataArray ]],
163
173
shape : Union [Shape , Sizes ],
164
174
order : Order = "C" ,
165
175
** kwargs : Any ,
166
176
) -> TDataArray :
177
+ ...
178
+
179
+ @overload
180
+ @classmethod
181
+ def empty (
182
+ cls : Type [DataClass [P ]],
183
+ shape : Union [Shape , Sizes ],
184
+ order : Order = "C" ,
185
+ ** kwargs : Any ,
186
+ ) -> xr .DataArray :
187
+ ...
188
+
189
+ @classmethod
190
+ def empty (
191
+ cls : Any ,
192
+ shape : Union [Shape , Sizes ],
193
+ order : Order = "C" ,
194
+ ** kwargs : Any ,
195
+ ) -> Any :
167
196
"""Create a DataArray object without initializing data.
168
197
169
198
Args:
@@ -185,13 +214,33 @@ def empty(
185
214
data = np .empty (shape , order = order )
186
215
return asdataarray (cls (** {name : data }, ** kwargs ))
187
216
217
+ @overload
188
218
@classmethod
189
219
def zeros (
190
220
cls : Type [DataArrayClass [P , TDataArray ]],
191
221
shape : Union [Shape , Sizes ],
192
222
order : Order = "C" ,
193
223
** kwargs : Any ,
194
224
) -> TDataArray :
225
+ ...
226
+
227
+ @overload
228
+ @classmethod
229
+ def zeros (
230
+ cls : Type [DataClass [P ]],
231
+ shape : Union [Shape , Sizes ],
232
+ order : Order = "C" ,
233
+ ** kwargs : Any ,
234
+ ) -> xr .DataArray :
235
+ ...
236
+
237
+ @classmethod
238
+ def zeros (
239
+ cls : Any ,
240
+ shape : Union [Shape , Sizes ],
241
+ order : Order = "C" ,
242
+ ** kwargs : Any ,
243
+ ) -> Any :
195
244
"""Create a DataArray object filled with zeros.
196
245
197
246
Args:
@@ -213,13 +262,33 @@ def zeros(
213
262
data = np .zeros (shape , order = order )
214
263
return asdataarray (cls (** {name : data }, ** kwargs ))
215
264
265
+ @overload
216
266
@classmethod
217
267
def ones (
218
268
cls : Type [DataArrayClass [P , TDataArray ]],
219
269
shape : Union [Shape , Sizes ],
220
270
order : Order = "C" ,
221
271
** kwargs : Any ,
222
272
) -> TDataArray :
273
+ ...
274
+
275
+ @overload
276
+ @classmethod
277
+ def ones (
278
+ cls : Type [DataClass [P ]],
279
+ shape : Union [Shape , Sizes ],
280
+ order : Order = "C" ,
281
+ ** kwargs : Any ,
282
+ ) -> xr .DataArray :
283
+ ...
284
+
285
+ @classmethod
286
+ def ones (
287
+ cls : Any ,
288
+ shape : Union [Shape , Sizes ],
289
+ order : Order = "C" ,
290
+ ** kwargs : Any ,
291
+ ) -> Any :
223
292
"""Create a DataArray object filled with ones.
224
293
225
294
Args:
@@ -241,6 +310,7 @@ def ones(
241
310
data = np .ones (shape , order = order )
242
311
return asdataarray (cls (** {name : data }, ** kwargs ))
243
312
313
+ @overload
244
314
@classmethod
245
315
def full (
246
316
cls : Type [DataArrayClass [P , TDataArray ]],
@@ -249,6 +319,27 @@ def full(
249
319
order : Order = "C" ,
250
320
** kwargs : Any ,
251
321
) -> TDataArray :
322
+ ...
323
+
324
+ @overload
325
+ @classmethod
326
+ def full (
327
+ cls : Type [DataClass [P ]],
328
+ shape : Union [Shape , Sizes ],
329
+ fill_value : Any ,
330
+ order : Order = "C" ,
331
+ ** kwargs : Any ,
332
+ ) -> xr .DataArray :
333
+ ...
334
+
335
+ @classmethod
336
+ def full (
337
+ cls : Any ,
338
+ shape : Union [Shape , Sizes ],
339
+ fill_value : Any ,
340
+ order : Order = "C" ,
341
+ ** kwargs : Any ,
342
+ ) -> Any :
252
343
"""Create a DataArray object filled with given value.
253
344
254
345
Args:
0 commit comments