Skip to content

Commit 655d464

Browse files
committed
remove ScalarWrapper and update changes
1 parent 070c673 commit 655d464

File tree

2 files changed

+4
-142
lines changed

2 files changed

+4
-142
lines changed

changes/2718.bugfix.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
0-dimensional arrays are now returning an instance of ``ScalarWrapper``, which behaves like a scalar
2-
but follow the ``NDArrayLike`` interface. This change is to make the behavior of 0-dimensional arrays
3-
consistent with ``numpy`` scalars while not breaking the output format of the ``NDArrayLike`` interface.
1+
0-dimensional arrays are now returning a scalar. Therefore, the return type of ``__getitem__`` changed
2+
to NDArrayOrScalarLike. This change is to make the behavior of 0-dimensional arrays consistent with
3+
``numpy`` scalars.

src/zarr/core/buffer/core.py

Lines changed: 1 addition & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
from zarr.codecs.bytes import Endian
2424
from zarr.core.common import BytesLike, ChunkCoords
25-
from zarr.core.indexing import Selection
2625

2726
# Everything here is imported into ``zarr.core.buffer`` namespace.
2827
__all__: list[str] = []
@@ -110,138 +109,6 @@ def __eq__(self, other: object) -> Self: # type: ignore[explicit-override, over
110109
NDArrayOrScalarLike = ScalarType | NDArrayLike
111110

112111

113-
class ScalarWrapper:
114-
def __init__(self, value: Any, dtype: np.dtype[Any] | None = None) -> None:
115-
self._value: Any = value
116-
self._dtype: np.dtype[Any] = dtype or np.dtype(type(self._value))
117-
118-
@property
119-
def dtype(self) -> np.dtype[Any]:
120-
return self._dtype
121-
122-
@property
123-
def ndim(self) -> int:
124-
return 0
125-
126-
@property
127-
def size(self) -> int:
128-
return 1
129-
130-
@property
131-
def shape(self) -> tuple[()]:
132-
return ()
133-
134-
def __len__(self) -> int:
135-
raise TypeError("len() of unsized object.")
136-
137-
def __getitem__(self, key: Selection) -> Self:
138-
if key != slice(None) and key != Ellipsis and key != ():
139-
raise IndexError("Invalid index for scalar.")
140-
return self
141-
142-
def __setitem__(self, key: Selection, value: Any) -> None:
143-
if key != slice(None) and key != Ellipsis and key != ():
144-
raise IndexError("Invalid index for scalar.")
145-
self._value = value
146-
147-
def __array__(
148-
self, dtype: npt.DTypeLike | None = None, copy: bool | None = True
149-
) -> npt.NDArray[Any]:
150-
return np.array(self._value, dtype=dtype or self._dtype, copy=copy)
151-
152-
def reshape(
153-
self, shape: tuple[int, ...] | Literal[-1], *, order: Literal["A", "C", "F"] = "C"
154-
) -> Self:
155-
if shape != () and shape != -1:
156-
raise ValueError("Cannot reshape scalar to non-scalar shape.")
157-
return self
158-
159-
def view(self, dtype: npt.DTypeLike) -> Self:
160-
return self.astype(dtype)
161-
162-
def astype(
163-
self, dtype: npt.DTypeLike, order: Literal["K", "A", "C", "F"] = "K", *, copy: bool = True
164-
) -> Self:
165-
if copy:
166-
return self.__class__(self._value, np.dtype(dtype))
167-
self._dtype = np.dtype(dtype)
168-
return self
169-
170-
def fill(self, value: Any) -> None:
171-
self._value = value
172-
173-
def copy(self) -> Self:
174-
return self.__class__(self._value)
175-
176-
def transpose(self, axes: SupportsIndex | Sequence[SupportsIndex] | None = None) -> Self:
177-
return self
178-
179-
def ravel(self, order: Literal["K", "A", "C", "F"] = "C") -> Self:
180-
return self
181-
182-
def all(self) -> bool:
183-
return bool(self._value)
184-
185-
def __eq__(self, other: object) -> Self: # type: ignore[explicit-override, override]
186-
return self.__class__(self._value == other)
187-
188-
def __repr__(self) -> str:
189-
return f"ScalarWrapper({self._value!r})"
190-
191-
def __getattr__(self, name: str) -> Any:
192-
return getattr(self._value, name)
193-
194-
def __add__(self, other: Any) -> Any:
195-
return self._value + other
196-
197-
def __sub__(self, other: Any) -> Any:
198-
return self._value - other
199-
200-
def __mul__(self, other: Any) -> Any:
201-
return self._value * other
202-
203-
def __truediv__(self, other: Any) -> Any:
204-
return self._value / other
205-
206-
def __floordiv__(self, other: Any) -> Any:
207-
return self._value // other
208-
209-
def __mod__(self, other: Any) -> Any:
210-
return self._value % other
211-
212-
def __pow__(self, other: Any) -> Any:
213-
return self._value**other
214-
215-
def __neg__(self) -> Any:
216-
return -self._value
217-
218-
def __abs__(self) -> Any:
219-
if hasattr(self._value, "__abs__"):
220-
return abs(self._value)
221-
raise TypeError(f"bad operand type for abs(): '{self._value.__class__.__name__}'")
222-
223-
def __int__(self) -> int:
224-
return int(self._value)
225-
226-
def __float__(self) -> float:
227-
return float(self._value)
228-
229-
def __complex__(self) -> complex:
230-
return complex(self._value)
231-
232-
def __bool__(self) -> bool:
233-
return bool(self._value)
234-
235-
def __hash__(self) -> int:
236-
return hash(self._value)
237-
238-
def __str__(self) -> str:
239-
return str(self._value)
240-
241-
def __format__(self, format_spec: str) -> str:
242-
return format(self._value, format_spec)
243-
244-
245112
def check_item_key_is_1d_contiguous(key: Any) -> None:
246113
"""Raises error if `key` isn't a 1d contiguous slice"""
247114
if not isinstance(key, slice):
@@ -557,12 +424,7 @@ def as_numpy_array(self) -> npt.NDArray[Any]:
557424
...
558425

559426
def as_scalar(self) -> ScalarType:
560-
"""Returns the buffer as a scalar value
561-
562-
Returns
563-
-------
564-
ScalarWrapper of this buffer
565-
"""
427+
"""Returns the buffer as a scalar value"""
566428
if self._data.size != 1:
567429
raise ValueError("Buffer does not contain a single scalar value")
568430
value: ScalarType = self.dtype.type(self.as_numpy_array().item())

0 commit comments

Comments
 (0)