MCPcopy Index your code
hub / github.com/pydata/xarray / DuckArrayWrapper

Class DuckArrayWrapper

xarray/tests/arrays.py:54–77  ·  view source on GitHub ↗

Array-like that prevents casting to array. Modeled after cupy.

Source from the content-addressed store, hash-verified

52
53
54class DuckArrayWrapper(utils.NDArrayMixin):
55 """Array-like that prevents casting to array.
56 Modeled after cupy."""
57
58 def __init__(self, array: np.ndarray):
59 self.array = array
60
61 def __getitem__(self, key):
62 return type(self)(self.array[key])
63
64 def to_numpy(self) -> np.ndarray:
65 """Allow explicit conversions to numpy in `to_numpy`, but disallow np.asarray etc."""
66 return self.array
67
68 def __array__(
69 self, dtype: np.typing.DTypeLike | None = None, /, *, copy: bool | None = None
70 ) -> np.ndarray:
71 raise UnexpectedDataAccess("Tried accessing data")
72
73 def __array_namespace__(self):
74 """Present to satisfy is_duck_array test."""
75 from xarray.tests import namespace
76
77 return namespace
78
79
80CONCATENATABLEARRAY_HANDLED_ARRAY_FUNCTIONS: dict[str, Callable] = {}

Callers 3

__init__Method · 0.90

Calls

no outgoing calls

Tested by 3

__init__Method · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…