1
1
import json
2
- from collections . abc import Iterator
2
+ from pathlib import Path
3
3
from typing import Any , Literal
4
4
5
+ import numcodecs .abc
5
6
import numcodecs .vlen
6
7
import numpy as np
7
8
import pytest
18
19
from zarr .core .dtype import FixedLengthUTF32 , Structured , VariableLengthUTF8
19
20
from zarr .core .dtype .npy .bytes import NullTerminatedBytes
20
21
from zarr .core .dtype .wrapper import ZDType
22
+ from zarr .core .group import Group
21
23
from zarr .core .sync import sync
22
24
from zarr .storage import MemoryStore , StorePath
23
25
24
26
25
27
@pytest .fixture
26
- async def store () -> Iterator [ StorePath ] :
28
+ async def store () -> StorePath :
27
29
return StorePath (await MemoryStore .open ())
28
30
29
31
@@ -68,7 +70,9 @@ def test_codec_pipeline() -> None:
68
70
("|V10" , "|V10" , b"X" , "WAAAAAAAAAAAAA==" ),
69
71
],
70
72
)
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 :
72
76
with config .set (
73
77
{
74
78
"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
99
103
assert serialized == expected
100
104
101
105
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 ))
104
107
105
108
106
109
@pytest .mark .parametrize (
@@ -111,7 +114,7 @@ async def test_v2_encode_decode(dtype, expected_dtype, fill_value, fill_value_js
111
114
(VariableLengthUTF8 (), "Y" ),
112
115
],
113
116
)
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 :
115
118
expected = np .full ((3 ,), value , dtype = dtype .to_native_dtype ())
116
119
a = zarr .create (
117
120
shape = (3 ,),
@@ -136,12 +139,13 @@ def test_v2_filters_codecs(filters: Any, order: Literal["C", "F"]) -> None:
136
139
137
140
@pytest .mark .filterwarnings ("ignore" )
138
141
@pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
139
- def test_create_array_defaults (store : Store ):
142
+ def test_create_array_defaults (store : Store ) -> None :
140
143
"""
141
144
Test that passing compressor=None results in no compressor. Also test that the default value of the compressor
142
145
parameter does produce a compressor.
143
146
"""
144
147
g = zarr .open (store , mode = "w" , zarr_format = 2 )
148
+ assert isinstance (g , Group )
145
149
arr = g .create_array ("one" , dtype = "i8" , shape = (1 ,), chunks = (1 ,), compressor = None )
146
150
assert arr ._async_array .compressor is None
147
151
assert not (arr .filters )
@@ -183,17 +187,19 @@ def test_v2_non_contiguous(numpy_order: Literal["C", "F"], zarr_order: Literal["
183
187
arr [6 :9 , 3 :6 ] = a [6 :9 , 3 :6 ] # The slice on the RHS is important
184
188
np .testing .assert_array_equal (arr [6 :9 , 3 :6 ], a [6 :9 , 3 :6 ])
185
189
190
+ buf = sync (store .get ("2.1" , default_buffer_prototype ()))
191
+ assert buf is not None
186
192
np .testing .assert_array_equal (
187
193
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 ),
191
195
)
192
196
# 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 )
193
199
if zarr_order == "F" :
194
- assert (arr [ 6 : 9 , 3 : 6 ] ).flags .f_contiguous
200
+ assert (sub_arr ).flags .f_contiguous
195
201
else :
196
- assert (arr [ 6 : 9 , 3 : 6 ] ).flags .c_contiguous
202
+ assert (sub_arr ).flags .c_contiguous
197
203
198
204
# Contiguous write
199
205
store = MemoryStore ()
@@ -214,19 +220,21 @@ def test_v2_non_contiguous(numpy_order: Literal["C", "F"], zarr_order: Literal["
214
220
arr [6 :9 , 3 :6 ] = a
215
221
np .testing .assert_array_equal (arr [6 :9 , 3 :6 ], a )
216
222
# 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 )
217
225
if zarr_order == "F" :
218
- assert (arr [ 6 : 9 , 3 : 6 ] ).flags .f_contiguous
226
+ assert (sub_arr ).flags .f_contiguous
219
227
else :
220
- assert (arr [ 6 : 9 , 3 : 6 ] ).flags .c_contiguous
228
+ assert (sub_arr ).flags .c_contiguous
221
229
222
230
223
- def test_default_compressor_deprecation_warning ():
231
+ def test_default_compressor_deprecation_warning () -> None :
224
232
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]
226
234
227
235
228
236
@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 :
230
238
a = np .array (
231
239
[(b"aaa" , 1 , 4.2 ), (b"bbb" , 2 , 8.4 ), (b"ccc" , 3 , 12.6 )],
232
240
dtype = [("foo" , "S3" ), ("bar" , "i4" ), ("baz" , "f8" )],
@@ -289,7 +297,7 @@ def test_parse_structured_fill_value_valid(
289
297
290
298
291
299
@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 :
293
301
a = np .array ([b"a\0 \0 " , b"bb" , b"ccc" ], dtype = "V7" )
294
302
array_path = tmp_path / "data.zarr"
295
303
za = zarr .create (
0 commit comments