Skip to content

Commit a7d4421

Browse files
committed
increase codecov
1 parent c150462 commit a7d4421

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

src/zarr/core/buffer/core.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,16 @@ def shape(self) -> tuple[()]:
127127
return ()
128128

129129
def __len__(self) -> int:
130-
raise TypeError("ScalarWrapper object has no len()")
130+
raise TypeError("len() of unsized object.")
131131

132132
def __getitem__(self, key: slice) -> Self:
133-
if key != slice(None):
134-
raise IndexError("Invalid index for scalar")
133+
if key != slice(None) and key != Ellipsis and key != ():
134+
raise IndexError("Invalid index for scalar.")
135135
return self
136136

137137
def __setitem__(self, key: slice, value: Any) -> None:
138-
if key != slice(None):
139-
raise IndexError("Invalid index for scalar")
138+
if key != slice(None) and key != Ellipsis and key != ():
139+
raise IndexError("Invalid index for scalar.")
140140
self._value = value
141141

142142
def __array__(
@@ -148,16 +148,19 @@ def reshape(
148148
self, shape: tuple[int, ...] | Literal[-1], *, order: Literal["A", "C", "F"] = "C"
149149
) -> Self:
150150
if shape != () and shape != -1:
151-
raise ValueError("Cannot reshape scalar to non-scalar shape")
151+
raise ValueError("Cannot reshape scalar to non-scalar shape.")
152152
return self
153153

154154
def view(self, dtype: npt.DTypeLike) -> Self:
155-
return self
155+
return self.astype(dtype)
156156

157157
def astype(
158158
self, dtype: npt.DTypeLike, order: Literal["K", "A", "C", "F"] = "K", *, copy: bool = True
159159
) -> Self:
160-
raise TypeError("ScalarWrapper object has no astype()")
160+
if copy:
161+
return self.__class__(self._value, dtype)
162+
self._dtype = dtype
163+
return self
161164

162165
def fill(self, value: Any) -> None:
163166
self._value = value

tests/test_array.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pickle
55
import re
66
from itertools import accumulate
7-
from typing import TYPE_CHECKING, Any, Literal, cast
7+
from typing import TYPE_CHECKING, Any, Literal
88

99
import numcodecs
1010
import numpy as np
@@ -1258,26 +1258,23 @@ async def test_create_array_v2_no_shards(store: MemoryStore) -> None:
12581258
)
12591259

12601260

1261-
@pytest.mark.parametrize("value", [1, 1.4, "a", b"a", np.array(1)])
1262-
def test_scalar_array(value: Any) -> None:
1263-
arr = zarr.array(value)
1264-
assert arr[...] == value
1265-
assert arr.shape == ()
1266-
assert arr.ndim == 0
1267-
1268-
x = arr[()]
1269-
assert isinstance(arr[()], ScalarWrapper)
1270-
assert isinstance(arr[()], NDArrayLike)
1271-
assert x.shape == arr.shape
1272-
assert x.ndim == arr.ndim
1261+
@pytest.mark.parametrize("value", [1, 1.4, "a", b"a", np.array(1), False, True])
1262+
def test_scalar_wrapper(value: Any) -> None:
1263+
x = ScalarWrapper(value)
12731264
assert x == value
12741265
assert value == x
1266+
assert x == x[()]
1267+
assert x.view(str) == x
1268+
assert x.copy() == x
1269+
assert x.transpose() == x
1270+
assert x.ravel() == x
1271+
assert x.all() == bool(value)
12751272
if isinstance(value, (int, float)):
1276-
x = cast(ScalarWrapper, x)
12771273
assert -x == -value
12781274
assert abs(x) == abs(value)
12791275
assert int(x) == int(value)
12801276
assert float(x) == float(value)
1277+
assert complex(x) == complex(value)
12811278
assert x + 1 == value + 1
12821279
assert x - 1 == value - 1
12831280
assert x * 2 == value * 2
@@ -1291,5 +1288,34 @@ def test_scalar_array(value: Any) -> None:
12911288
assert hash(x) == hash(value)
12921289
assert str(x) == str(value)
12931290
assert format(x, "") == format(value, "")
1291+
x.fill(2)
1292+
x[()] += 1
1293+
assert x == 3
12941294
elif isinstance(value, str):
12951295
assert str(x) == value
1296+
with pytest.raises(TypeError, match=re.escape("bad operand type for abs(): 'str'")):
1297+
abs(x)
1298+
1299+
with pytest.raises(ValueError, match="Cannot reshape scalar to non-scalar shape."):
1300+
x.reshape((1, 2))
1301+
with pytest.raises(IndexError, match="Invalid index for scalar."):
1302+
x[10] = value
1303+
with pytest.raises(IndexError, match="Invalid index for scalar."):
1304+
x[10]
1305+
with pytest.raises(TypeError, match=re.escape("len() of unsized object.")):
1306+
len(x)
1307+
1308+
1309+
@pytest.mark.parametrize("value", [1, 1.4, "a", b"a", np.array(1)])
1310+
@pytest.mark.parametrize("zarr_format", [2, 3])
1311+
def test_scalar_array(value: Any, zarr_format: ZarrFormat) -> None:
1312+
arr = zarr.array(value, zarr_format=zarr_format)
1313+
assert arr[...] == value
1314+
assert arr.shape == ()
1315+
assert arr.ndim == 0
1316+
1317+
x = arr[()]
1318+
assert isinstance(arr[()], ScalarWrapper)
1319+
assert isinstance(arr[()], NDArrayLike)
1320+
assert x.shape == arr.shape
1321+
assert x.ndim == arr.ndim

tests/test_buffer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,5 @@ def test_numpy_buffer_prototype() -> None:
151151
ndbuffer = cpu.buffer_prototype.nd_buffer.create(shape=(1, 2), dtype=np.dtype("int64"))
152152
assert isinstance(buffer.as_array_like(), np.ndarray)
153153
assert isinstance(ndbuffer.as_ndarray_like(), np.ndarray)
154+
with pytest.raises(ValueError, match="Buffer does not contain a single scalar value"):
155+
ndbuffer.as_scalar()

0 commit comments

Comments
 (0)