From 655d464db0e5db45517997dbc35d8202845a1a47 Mon Sep 17 00:00:00 2001 From: brokkoli71 Date: Wed, 12 Feb 2025 15:09:29 +0100 Subject: [PATCH] remove ScalarWrapper and update changes --- changes/2718.bugfix.rst | 6 +- src/zarr/core/buffer/core.py | 140 +---------------------------------- 2 files changed, 4 insertions(+), 142 deletions(-) diff --git a/changes/2718.bugfix.rst b/changes/2718.bugfix.rst index ed06226988..f2b1c2a9d3 100644 --- a/changes/2718.bugfix.rst +++ b/changes/2718.bugfix.rst @@ -1,3 +1,3 @@ -0-dimensional arrays are now returning an instance of ``ScalarWrapper``, which behaves like a scalar -but follow the ``NDArrayLike`` interface. This change is to make the behavior of 0-dimensional arrays -consistent with ``numpy`` scalars while not breaking the output format of the ``NDArrayLike`` interface. \ No newline at end of file +0-dimensional arrays are now returning a scalar. Therefore, the return type of ``__getitem__`` changed +to NDArrayOrScalarLike. This change is to make the behavior of 0-dimensional arrays consistent with +``numpy`` scalars. \ No newline at end of file diff --git a/src/zarr/core/buffer/core.py b/src/zarr/core/buffer/core.py index 074881d0ba..5402c58992 100644 --- a/src/zarr/core/buffer/core.py +++ b/src/zarr/core/buffer/core.py @@ -22,7 +22,6 @@ from zarr.codecs.bytes import Endian from zarr.core.common import BytesLike, ChunkCoords - from zarr.core.indexing import Selection # Everything here is imported into ``zarr.core.buffer`` namespace. __all__: list[str] = [] @@ -110,138 +109,6 @@ def __eq__(self, other: object) -> Self: # type: ignore[explicit-override, over NDArrayOrScalarLike = ScalarType | NDArrayLike -class ScalarWrapper: - def __init__(self, value: Any, dtype: np.dtype[Any] | None = None) -> None: - self._value: Any = value - self._dtype: np.dtype[Any] = dtype or np.dtype(type(self._value)) - - @property - def dtype(self) -> np.dtype[Any]: - return self._dtype - - @property - def ndim(self) -> int: - return 0 - - @property - def size(self) -> int: - return 1 - - @property - def shape(self) -> tuple[()]: - return () - - def __len__(self) -> int: - raise TypeError("len() of unsized object.") - - def __getitem__(self, key: Selection) -> Self: - if key != slice(None) and key != Ellipsis and key != (): - raise IndexError("Invalid index for scalar.") - return self - - def __setitem__(self, key: Selection, value: Any) -> None: - if key != slice(None) and key != Ellipsis and key != (): - raise IndexError("Invalid index for scalar.") - self._value = value - - def __array__( - self, dtype: npt.DTypeLike | None = None, copy: bool | None = True - ) -> npt.NDArray[Any]: - return np.array(self._value, dtype=dtype or self._dtype, copy=copy) - - def reshape( - self, shape: tuple[int, ...] | Literal[-1], *, order: Literal["A", "C", "F"] = "C" - ) -> Self: - if shape != () and shape != -1: - raise ValueError("Cannot reshape scalar to non-scalar shape.") - return self - - def view(self, dtype: npt.DTypeLike) -> Self: - return self.astype(dtype) - - def astype( - self, dtype: npt.DTypeLike, order: Literal["K", "A", "C", "F"] = "K", *, copy: bool = True - ) -> Self: - if copy: - return self.__class__(self._value, np.dtype(dtype)) - self._dtype = np.dtype(dtype) - return self - - def fill(self, value: Any) -> None: - self._value = value - - def copy(self) -> Self: - return self.__class__(self._value) - - def transpose(self, axes: SupportsIndex | Sequence[SupportsIndex] | None = None) -> Self: - return self - - def ravel(self, order: Literal["K", "A", "C", "F"] = "C") -> Self: - return self - - def all(self) -> bool: - return bool(self._value) - - def __eq__(self, other: object) -> Self: # type: ignore[explicit-override, override] - return self.__class__(self._value == other) - - def __repr__(self) -> str: - return f"ScalarWrapper({self._value!r})" - - def __getattr__(self, name: str) -> Any: - return getattr(self._value, name) - - def __add__(self, other: Any) -> Any: - return self._value + other - - def __sub__(self, other: Any) -> Any: - return self._value - other - - def __mul__(self, other: Any) -> Any: - return self._value * other - - def __truediv__(self, other: Any) -> Any: - return self._value / other - - def __floordiv__(self, other: Any) -> Any: - return self._value // other - - def __mod__(self, other: Any) -> Any: - return self._value % other - - def __pow__(self, other: Any) -> Any: - return self._value**other - - def __neg__(self) -> Any: - return -self._value - - def __abs__(self) -> Any: - if hasattr(self._value, "__abs__"): - return abs(self._value) - raise TypeError(f"bad operand type for abs(): '{self._value.__class__.__name__}'") - - def __int__(self) -> int: - return int(self._value) - - def __float__(self) -> float: - return float(self._value) - - def __complex__(self) -> complex: - return complex(self._value) - - def __bool__(self) -> bool: - return bool(self._value) - - def __hash__(self) -> int: - return hash(self._value) - - def __str__(self) -> str: - return str(self._value) - - def __format__(self, format_spec: str) -> str: - return format(self._value, format_spec) - - def check_item_key_is_1d_contiguous(key: Any) -> None: """Raises error if `key` isn't a 1d contiguous slice""" if not isinstance(key, slice): @@ -557,12 +424,7 @@ def as_numpy_array(self) -> npt.NDArray[Any]: ... def as_scalar(self) -> ScalarType: - """Returns the buffer as a scalar value - - Returns - ------- - ScalarWrapper of this buffer - """ + """Returns the buffer as a scalar value""" if self._data.size != 1: raise ValueError("Buffer does not contain a single scalar value") value: ScalarType = self.dtype.type(self.as_numpy_array().item())