22
33import contextlib
44import pickle
5+ import warnings
56from typing import TYPE_CHECKING , Any
67
78import numpy as np
@@ -164,8 +165,7 @@ def test_generic_filter(
164165
165166 a [:, :] = data .copy ()
166167 with codec_conf ():
167- with pytest .warns (ZarrUserWarning , match = EXPECTED_WARNING_STR ):
168- b = open_array (a .store , mode = "r" )
168+ b = open_array (a .store , mode = "r" )
169169 np .testing .assert_array_equal (data , b [:, :])
170170
171171
@@ -183,8 +183,7 @@ def test_generic_filter_bitround() -> None:
183183 )
184184
185185 a [:, :] = data .copy ()
186- with pytest .warns (ZarrUserWarning , match = EXPECTED_WARNING_STR ):
187- b = open_array (a .store , mode = "r" )
186+ b = open_array (a .store , mode = "r" )
188187 assert np .allclose (data , b [:, :], atol = 0.1 )
189188
190189
@@ -202,8 +201,7 @@ def test_generic_filter_quantize() -> None:
202201 )
203202
204203 a [:, :] = data .copy ()
205- with pytest .warns (ZarrUserWarning , match = EXPECTED_WARNING_STR ):
206- b = open_array (a .store , mode = "r" )
204+ b = open_array (a .store , mode = "r" )
207205 assert np .allclose (data , b [:, :], atol = 0.001 )
208206
209207
@@ -222,20 +220,18 @@ def test_generic_filter_packbits() -> None:
222220 )
223221
224222 a [:, :] = data .copy ()
225- with pytest .warns (ZarrUserWarning , match = EXPECTED_WARNING_STR ):
226- b = open_array (a .store , mode = "r" )
223+ b = open_array (a .store , mode = "r" )
227224 np .testing .assert_array_equal (data , b [:, :])
228225
229- with pytest .warns (ZarrUserWarning , match = EXPECTED_WARNING_STR ):
230- with pytest .raises (ValueError , match = ".*requires bool dtype.*" ):
231- create_array (
232- {},
233- shape = data .shape ,
234- chunks = (16 , 16 ),
235- dtype = "uint32" ,
236- fill_value = 0 ,
237- filters = [_numcodecs .PackBits ()],
238- )
226+ with pytest .raises (ValueError , match = ".*requires bool dtype.*" ):
227+ create_array (
228+ {},
229+ shape = data .shape ,
230+ chunks = (16 , 16 ),
231+ dtype = "uint32" ,
232+ fill_value = 0 ,
233+ filters = [_numcodecs .PackBits ()],
234+ )
239235
240236
241237@pytest .mark .parametrize (
@@ -251,8 +247,7 @@ def test_generic_filter_packbits() -> None:
251247def test_generic_checksum (codec_class : type [_numcodecs ._NumcodecsBytesBytesCodec ]) -> None :
252248 # Check if the codec is available in numcodecs
253249 try :
254- with pytest .warns (ZarrUserWarning , match = EXPECTED_WARNING_STR ):
255- codec_class ()._codec # noqa: B018
250+ codec_class ()._codec # noqa: B018
256251 except UnknownCodecError as e : # pragma: no cover
257252 pytest .skip (f"{ codec_class .codec_name } is not available in numcodecs: { e } " )
258253
@@ -270,16 +265,14 @@ def test_generic_checksum(codec_class: type[_numcodecs._NumcodecsBytesBytesCodec
270265
271266 a [:, :] = data .copy ()
272267 with codec_conf ():
273- with pytest .warns (ZarrUserWarning , match = EXPECTED_WARNING_STR ):
274- b = open_array (a .store , mode = "r" )
268+ b = open_array (a .store , mode = "r" )
275269 np .testing .assert_array_equal (data , b [:, :])
276270
277271
278272@pytest .mark .parametrize ("codec_class" , [_numcodecs .PCodec , _numcodecs .ZFPY ])
279273def test_generic_bytes_codec (codec_class : type [_numcodecs ._NumcodecsArrayBytesCodec ]) -> None :
280274 try :
281- with pytest .warns (ZarrUserWarning , match = EXPECTED_WARNING_STR ):
282- codec_class ()._codec # noqa: B018
275+ codec_class ()._codec # noqa: B018
283276 except ValueError as e : # pragma: no cover
284277 if "codec not available" in str (e ):
285278 pytest .xfail (f"{ codec_class .codec_name } is not available: { e } " )
@@ -321,21 +314,47 @@ def test_delta_astype() -> None:
321314
322315 a [:, :] = data .copy ()
323316 with codec_conf ():
324- with pytest .warns (ZarrUserWarning , match = EXPECTED_WARNING_STR ):
325- b = open_array (a .store , mode = "r" )
317+ b = open_array (a .store , mode = "r" )
326318 np .testing .assert_array_equal (data , b [:, :])
327319
328320
329321def test_repr () -> None :
330- with pytest .warns (ZarrUserWarning , match = EXPECTED_WARNING_STR ):
331- codec = _numcodecs .LZ4 (level = 5 )
322+ codec = _numcodecs .LZ4 (level = 5 )
332323 assert repr (codec ) == "LZ4(codec_name='numcodecs.lz4', codec_config={'level': 5})"
333324
334325
335326def test_to_dict () -> None :
327+ codec = _numcodecs .LZ4 (level = 5 )
328+ with pytest .warns (ZarrUserWarning , match = EXPECTED_WARNING_STR ):
329+ assert codec .to_dict () == {"name" : "numcodecs.lz4" , "configuration" : {"level" : 5 }}
330+
331+
332+ def test_warn_on_write_not_read () -> None :
333+ data = np .arange (0 , 256 , dtype = "uint16" ).reshape ((16 , 16 ))
334+
336335 with pytest .warns (ZarrUserWarning , match = EXPECTED_WARNING_STR ):
337- codec = _numcodecs .LZ4 (level = 5 )
338- assert codec .to_dict () == {"name" : "numcodecs.lz4" , "configuration" : {"level" : 5 }}
336+ a = create_array (
337+ {},
338+ shape = data .shape ,
339+ chunks = (16 , 16 ),
340+ dtype = data .dtype ,
341+ fill_value = 0 ,
342+ compressors = [_numcodecs .Zstd (level = 1 )],
343+ )
344+
345+ a [:, :] = data .copy ()
346+ with codec_conf ():
347+ with warnings .catch_warnings (record = True ) as caught :
348+ warnings .simplefilter ("always" )
349+ b = open_array (a .store , mode = "r" )
350+
351+ np .testing .assert_array_equal (data , b [:, :])
352+ assert not [
353+ warning
354+ for warning in caught
355+ if issubclass (warning .category , ZarrUserWarning )
356+ and "Numcodecs codecs are not in the Zarr version 3 specification" in str (warning .message )
357+ ]
339358
340359
341360@pytest .mark .parametrize (
@@ -367,8 +386,7 @@ def test_to_dict() -> None:
367386def test_codecs_pickleable (codec_cls : type [_numcodecs ._NumcodecsCodec ]) -> None :
368387 # Check if the codec is available in numcodecs
369388 try :
370- with pytest .warns (ZarrUserWarning , match = EXPECTED_WARNING_STR ):
371- codec = codec_cls ()
389+ codec = codec_cls ()
372390 except UnknownCodecError as e : # pragma: no cover
373391 pytest .skip (f"{ codec_cls .codec_name } is not available in numcodecs: { e } " )
374392
0 commit comments