Skip to content

Commit 7a826e8

Browse files
dstansbyd-v-b
andauthored
Fix typing in test_v2 (#3143)
Co-authored-by: Davis Bennett <davis.v.bennett@gmail.com>
1 parent 72786ea commit 7a826e8

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ module = [
384384
"tests.test_indexing",
385385
"tests.test_properties",
386386
"tests.test_sync",
387-
"tests.test_v2",
388387
"tests.test_regression.scripts.*"
389388
]
390389
ignore_errors = true

tests/test_v2.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import json
2-
from collections.abc import Iterator
2+
from pathlib import Path
33
from typing import Any, Literal
44

5+
import numcodecs.abc
56
import numcodecs.vlen
67
import numpy as np
78
import pytest
@@ -18,12 +19,13 @@
1819
from zarr.core.dtype import FixedLengthUTF32, Structured, VariableLengthUTF8
1920
from zarr.core.dtype.npy.bytes import NullTerminatedBytes
2021
from zarr.core.dtype.wrapper import ZDType
22+
from zarr.core.group import Group
2123
from zarr.core.sync import sync
2224
from zarr.storage import MemoryStore, StorePath
2325

2426

2527
@pytest.fixture
26-
async def store() -> Iterator[StorePath]:
28+
async def store() -> StorePath:
2729
return StorePath(await MemoryStore.open())
2830

2931

@@ -68,7 +70,9 @@ def test_codec_pipeline() -> None:
6870
("|V10", "|V10", b"X", "WAAAAAAAAAAAAA=="),
6971
],
7072
)
71-
async def test_v2_encode_decode(dtype, expected_dtype, fill_value, fill_value_json) -> None:
73+
async def test_v2_encode_decode(
74+
dtype: str, expected_dtype: str, fill_value: bytes, fill_value_json: str
75+
) -> None:
7276
with config.set(
7377
{
7478
"array.v2_default_filters.bytes": [{"id": "vlen-bytes"}],
@@ -99,8 +103,7 @@ async def test_v2_encode_decode(dtype, expected_dtype, fill_value, fill_value_js
99103
assert serialized == expected
100104

101105
data = zarr.open_array(store=store, path="foo")[:]
102-
expected = np.full((3,), b"X", dtype=dtype)
103-
np.testing.assert_equal(data, expected)
106+
np.testing.assert_equal(data, np.full((3,), b"X", dtype=dtype))
104107

105108

106109
@pytest.mark.parametrize(
@@ -111,7 +114,7 @@ async def test_v2_encode_decode(dtype, expected_dtype, fill_value, fill_value_js
111114
(VariableLengthUTF8(), "Y"),
112115
],
113116
)
114-
def test_v2_encode_decode_with_data(dtype: ZDType[Any, Any], value: str):
117+
def test_v2_encode_decode_with_data(dtype: ZDType[Any, Any], value: str) -> None:
115118
expected = np.full((3,), value, dtype=dtype.to_native_dtype())
116119
a = zarr.create(
117120
shape=(3,),
@@ -136,12 +139,13 @@ def test_v2_filters_codecs(filters: Any, order: Literal["C", "F"]) -> None:
136139

137140
@pytest.mark.filterwarnings("ignore")
138141
@pytest.mark.parametrize("store", ["memory"], indirect=True)
139-
def test_create_array_defaults(store: Store):
142+
def test_create_array_defaults(store: Store) -> None:
140143
"""
141144
Test that passing compressor=None results in no compressor. Also test that the default value of the compressor
142145
parameter does produce a compressor.
143146
"""
144147
g = zarr.open(store, mode="w", zarr_format=2)
148+
assert isinstance(g, Group)
145149
arr = g.create_array("one", dtype="i8", shape=(1,), chunks=(1,), compressor=None)
146150
assert arr._async_array.compressor is None
147151
assert not (arr.filters)
@@ -183,17 +187,19 @@ def test_v2_non_contiguous(numpy_order: Literal["C", "F"], zarr_order: Literal["
183187
arr[6:9, 3:6] = a[6:9, 3:6] # The slice on the RHS is important
184188
np.testing.assert_array_equal(arr[6:9, 3:6], a[6:9, 3:6])
185189

190+
buf = sync(store.get("2.1", default_buffer_prototype()))
191+
assert buf is not None
186192
np.testing.assert_array_equal(
187193
a[6:9, 3:6],
188-
np.frombuffer(
189-
sync(store.get("2.1", default_buffer_prototype())).to_bytes(), dtype="float64"
190-
).reshape((3, 3), order=zarr_order),
194+
np.frombuffer(buf.to_bytes(), dtype="float64").reshape((3, 3), order=zarr_order),
191195
)
192196
# After writing and reading from zarr array, order should be same as zarr order
197+
sub_arr = arr[6:9, 3:6]
198+
assert isinstance(sub_arr, np.ndarray)
193199
if zarr_order == "F":
194-
assert (arr[6:9, 3:6]).flags.f_contiguous
200+
assert (sub_arr).flags.f_contiguous
195201
else:
196-
assert (arr[6:9, 3:6]).flags.c_contiguous
202+
assert (sub_arr).flags.c_contiguous
197203

198204
# Contiguous write
199205
store = MemoryStore()
@@ -214,19 +220,21 @@ def test_v2_non_contiguous(numpy_order: Literal["C", "F"], zarr_order: Literal["
214220
arr[6:9, 3:6] = a
215221
np.testing.assert_array_equal(arr[6:9, 3:6], a)
216222
# After writing and reading from zarr array, order should be same as zarr order
223+
sub_arr = arr[6:9, 3:6]
224+
assert isinstance(sub_arr, np.ndarray)
217225
if zarr_order == "F":
218-
assert (arr[6:9, 3:6]).flags.f_contiguous
226+
assert (sub_arr).flags.f_contiguous
219227
else:
220-
assert (arr[6:9, 3:6]).flags.c_contiguous
228+
assert (sub_arr).flags.c_contiguous
221229

222230

223-
def test_default_compressor_deprecation_warning():
231+
def test_default_compressor_deprecation_warning() -> None:
224232
with pytest.warns(DeprecationWarning, match="default_compressor is deprecated"):
225-
zarr.storage.default_compressor = "zarr.codecs.zstd.ZstdCodec()"
233+
zarr.storage.default_compressor = "zarr.codecs.zstd.ZstdCodec()" # type: ignore[attr-defined]
226234

227235

228236
@pytest.mark.parametrize("fill_value", [None, (b"", 0, 0.0)], ids=["no_fill", "fill"])
229-
def test_structured_dtype_roundtrip(fill_value, tmp_path) -> None:
237+
def test_structured_dtype_roundtrip(fill_value: float | bytes, tmp_path: Path) -> None:
230238
a = np.array(
231239
[(b"aaa", 1, 4.2), (b"bbb", 2, 8.4), (b"ccc", 3, 12.6)],
232240
dtype=[("foo", "S3"), ("bar", "i4"), ("baz", "f8")],
@@ -289,7 +297,7 @@ def test_parse_structured_fill_value_valid(
289297

290298

291299
@pytest.mark.parametrize("fill_value", [None, b"x"], ids=["no_fill", "fill"])
292-
def test_other_dtype_roundtrip(fill_value, tmp_path) -> None:
300+
def test_other_dtype_roundtrip(fill_value: None | bytes, tmp_path: Path) -> None:
293301
a = np.array([b"a\0\0", b"bb", b"ccc"], dtype="V7")
294302
array_path = tmp_path / "data.zarr"
295303
za = zarr.create(

0 commit comments

Comments
 (0)