@@ -49,32 +49,12 @@ def __init__(self, *args: P.args, **kwargs: P.kwargs) -> None:
49
49
__dataoptions__ : DataOptions [TDataArray ]
50
50
51
51
52
- # custom classproperty
53
- class classproperty :
54
- """Class property only for AsDataArray.new().
55
-
56
- As a classmethod and a property can be chained together since Python 3.9,
57
- this class will be removed when the support for Python 3.7 and 3.8 ends.
58
-
59
- """
60
-
61
- def __init__ (self , func : Callable [..., Any ]) -> None :
62
- self .__func__ = func
63
-
64
- def __get__ (
65
- self ,
66
- obj : Any ,
67
- cls : Type [DataArrayClass [P , TDataArray ]],
68
- ) -> Callable [P , TDataArray ]:
69
- return self .__func__ (cls )
70
-
71
-
72
- # runtime functions and classes
52
+ # runtime functions
73
53
@overload
74
54
def asdataarray (
75
55
dataclass : DataArrayClass [Any , TDataArray ],
76
56
reference : Optional [DataType ] = None ,
77
- dataoptions : Any = DEFAULT_OPTIONS ,
57
+ dataoptions : DataOptions [ Any ] = DEFAULT_OPTIONS ,
78
58
) -> TDataArray :
79
59
...
80
60
@@ -90,8 +70,8 @@ def asdataarray(
90
70
91
71
def asdataarray (
92
72
dataclass : Any ,
93
- reference : Any = None ,
94
- dataoptions : Any = DEFAULT_OPTIONS ,
73
+ reference : Optional [ DataType ] = None ,
74
+ dataoptions : DataOptions [ Any ] = DEFAULT_OPTIONS ,
95
75
) -> Any :
96
76
"""Create a DataArray object from a dataclass object.
97
77
@@ -137,36 +117,82 @@ def asdataarray(
137
117
return dataarray
138
118
139
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
+
140
152
class AsDataArray :
141
153
"""Mix-in class that provides shorthand methods."""
142
154
143
- __dataoptions__ = DEFAULT_OPTIONS
144
-
145
155
@classproperty
146
- def new (cls : Type [ DataArrayClass [ P , TDataArray ]] ) -> Callable [ P , TDataArray ] :
156
+ def new (cls : Any ) -> Any :
147
157
"""Create a DataArray object from dataclass parameters."""
148
158
149
- init = copy (cls .__init__ ) # type: ignore
150
- init .__doc__ = cls .__init__ .__doc__ # type: ignore
159
+ init = copy (cls .__init__ )
160
+ init .__doc__ = cls .__init__ .__doc__
151
161
init .__annotations__ ["return" ] = TDataArray
152
162
153
163
@wraps (init )
154
- def new (
155
- cls : Type [DataArrayClass [P , TDataArray ]],
156
- * args : P .args ,
157
- ** kwargs : P .kwargs ,
158
- ) -> TDataArray :
164
+ def new (cls : Any , * args : Any , ** kwargs : Any ) -> Any :
159
165
return asdataarray (cls (* args , ** kwargs ))
160
166
161
167
return MethodType (new , cls )
162
168
169
+ @overload
163
170
@classmethod
164
171
def empty (
165
172
cls : Type [DataArrayClass [P , TDataArray ]],
166
173
shape : Union [Shape , Sizes ],
167
174
order : Order = "C" ,
168
175
** kwargs : Any ,
169
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 :
170
196
"""Create a DataArray object without initializing data.
171
197
172
198
Args:
@@ -188,13 +214,33 @@ def empty(
188
214
data = np .empty (shape , order = order )
189
215
return asdataarray (cls (** {name : data }, ** kwargs ))
190
216
217
+ @overload
191
218
@classmethod
192
219
def zeros (
193
220
cls : Type [DataArrayClass [P , TDataArray ]],
194
221
shape : Union [Shape , Sizes ],
195
222
order : Order = "C" ,
196
223
** kwargs : Any ,
197
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 :
198
244
"""Create a DataArray object filled with zeros.
199
245
200
246
Args:
@@ -216,13 +262,33 @@ def zeros(
216
262
data = np .zeros (shape , order = order )
217
263
return asdataarray (cls (** {name : data }, ** kwargs ))
218
264
265
+ @overload
219
266
@classmethod
220
267
def ones (
221
268
cls : Type [DataArrayClass [P , TDataArray ]],
222
269
shape : Union [Shape , Sizes ],
223
270
order : Order = "C" ,
224
271
** kwargs : Any ,
225
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 :
226
292
"""Create a DataArray object filled with ones.
227
293
228
294
Args:
@@ -244,6 +310,7 @@ def ones(
244
310
data = np .ones (shape , order = order )
245
311
return asdataarray (cls (** {name : data }, ** kwargs ))
246
312
313
+ @overload
247
314
@classmethod
248
315
def full (
249
316
cls : Type [DataArrayClass [P , TDataArray ]],
@@ -252,6 +319,27 @@ def full(
252
319
order : Order = "C" ,
253
320
** kwargs : Any ,
254
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 :
255
343
"""Create a DataArray object filled with given value.
256
344
257
345
Args:
0 commit comments