Skip to content

Commit 05d28b1

Browse files
committed
move warning into to_dict
1 parent 690c5bb commit 05d28b1

3 files changed

Lines changed: 58 additions & 42 deletions

File tree

src/zarr/codecs/numcodecs/_codecs.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ def __init__(self, **codec_config: JSON) -> None:
102102
) # pragma: no cover
103103

104104
object.__setattr__(self, "codec_config", codec_config)
105-
warn(
106-
"Numcodecs codecs are not in the Zarr version 3 specification and "
107-
"may not be supported by other zarr implementations.",
108-
category=ZarrUserWarning,
109-
stacklevel=2,
110-
)
111105

112106
@cached_property
113107
def _codec(self) -> Numcodec:
@@ -119,6 +113,12 @@ def from_dict(cls, data: dict[str, JSON]) -> Self:
119113
return cls(**codec_config)
120114

121115
def to_dict(self) -> dict[str, JSON]:
116+
warn(
117+
"Numcodecs codecs are not in the Zarr version 3 specification and "
118+
"may not be supported by other zarr implementations.",
119+
category=ZarrUserWarning,
120+
stacklevel=2,
121+
)
122122
codec_config = self.codec_config.copy()
123123
codec_config.pop("id", None)
124124
return {

tests/test_cli/test_migrate_v3.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,7 @@ def test_migrate_incorrect_filter(local_store: LocalStore) -> None:
524524
fill_value=0,
525525
)
526526

527-
with pytest.warns(UserWarning, match=NUMCODECS_USER_WARNING):
528-
result = runner.invoke(cli.app, ["migrate", "v3", str(local_store.root)])
527+
result = runner.invoke(cli.app, ["migrate", "v3", str(local_store.root)])
529528

530529
assert result.exit_code == 1
531530
assert isinstance(result.exception, TypeError)
@@ -548,8 +547,7 @@ def test_migrate_incorrect_compressor(local_store: LocalStore) -> None:
548547
fill_value=0,
549548
)
550549

551-
with pytest.warns(UserWarning, match=NUMCODECS_USER_WARNING):
552-
result = runner.invoke(cli.app, ["migrate", "v3", str(local_store.root)])
550+
result = runner.invoke(cli.app, ["migrate", "v3", str(local_store.root)])
553551

554552
assert result.exit_code == 1
555553
assert isinstance(result.exception, TypeError)

tests/test_codecs/test_numcodecs.py

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import contextlib
44
import pickle
5+
import warnings
56
from typing import TYPE_CHECKING, Any
67

78
import 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:
251247
def 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])
279273
def 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

329321
def 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

335326
def 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:
367386
def 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

Comments
 (0)