Skip to content

Commit 761e76c

Browse files
committed
#141 Remove ArrayLike
1 parent 18571af commit 761e76c

File tree

2 files changed

+14
-35
lines changed

2 files changed

+14
-35
lines changed

xarray_dataclasses/datamodel.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
# submodules
1616
from .typing import (
17-
ArrayLike,
1817
DataClass,
1918
DataType,
2019
Dims,
@@ -254,27 +253,27 @@ def get_typedarray(
254253
DataArray object with given dims and dtype.
255254
256255
"""
257-
if isinstance(data, ArrayLike):
258-
array = cast(np.ndarray, data)
259-
else:
260-
array = np.asarray(data)
256+
try:
257+
data.__array__
258+
except AttributeError:
259+
data = np.asarray(data)
261260

262261
if dtype is not None:
263-
array = array.astype(dtype, copy=False)
262+
data = data.astype(dtype, copy=False)
264263

265-
if array.ndim == len(dims):
266-
dataarray = xr.DataArray(array, dims=dims)
267-
elif array.ndim == 0 and reference is not None:
268-
dataarray = xr.DataArray(array)
264+
if data.ndim == len(dims):
265+
dataarray = xr.DataArray(data, dims=dims)
266+
elif data.ndim == 0 and reference is not None:
267+
dataarray = xr.DataArray(data)
269268
else:
270269
raise ValueError(
271270
"Could not create a DataArray object from data. "
272-
f"Mismatch between shape {array.shape} and dims {dims}."
271+
f"Mismatch between shape {data.shape} and dims {dims}."
273272
)
274273

275274
if reference is None:
276275
return dataarray
277-
278-
diff_dims = set(reference.dims) - set(dims)
279-
subspace = reference.isel({dim: 0 for dim in diff_dims})
280-
return dataarray.broadcast_like(subspace)
276+
else:
277+
ddims = set(reference.dims) - set(dims)
278+
reference = reference.isel({dim: 0 for dim in ddims})
279+
return dataarray.broadcast_like(reference)

xarray_dataclasses/typing.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
get_args,
4646
get_origin,
4747
get_type_hints,
48-
runtime_checkable,
4948
)
5049

5150

@@ -78,25 +77,6 @@ class Collection(Labeled[TDims], Collection[TDtype], Protocol):
7877
pass
7978

8079

81-
@runtime_checkable
82-
class ArrayLike(Protocol[TDims, TDtype]):
83-
"""Type hint for array-like objects."""
84-
85-
def astype(self: T, dtype: Any) -> T:
86-
"""Method to convert data type of the object."""
87-
...
88-
89-
@property
90-
def ndim(self) -> int:
91-
"""Number of dimensions of the object."""
92-
...
93-
94-
@property
95-
def shape(self) -> Tuple[int, ...]:
96-
"""Shape of the object."""
97-
...
98-
99-
10080
class DataClass(Protocol[P]):
10181
"""Type hint for dataclass objects."""
10282

0 commit comments

Comments
 (0)