Skip to content

Commit a12f333

Browse files
d-v-bmeeseeksmachine
authored andcommitted
Backport PR zarr-developers#3098: refactor warnings
1 parent 98111f5 commit a12f333

37 files changed

+487
-247
lines changed

changes/3098.misc.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Define Zarr-specific warning classes that subclass the Python built-in warnings.
2+
These classes makes it easier to control the visibility of warnings emitted by Zarr Python.
3+
See `zarr.errors` for these warning classes.

docs/user-guide/arrays.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ built-in delta filter::
238238

239239
>>> import lzma
240240
>>> from numcodecs.zarr3 import LZMA
241+
>>> import warnings
242+
>>> warnings.filterwarnings("ignore", category=UserWarning)
241243
>>>
242244
>>> lzma_filters = [dict(id=lzma.FILTER_DELTA, dist=4), dict(id=lzma.FILTER_LZMA2, preset=1)]
243245
>>> compressors = LZMA(filters=lzma_filters)

docs/user-guide/consolidated_metadata.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ In Python, the consolidated metadata is available on the ``.consolidated_metadat
2727
attribute of the ``GroupMetadata`` object.
2828

2929
>>> import zarr
30+
>>> import warnings
31+
>>> warnings.filterwarnings("ignore", category=UserWarning)
3032
>>>
3133
>>> store = zarr.storage.MemoryStore()
3234
>>> group = zarr.create_group(store=store)

pyproject.toml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -389,16 +389,6 @@ addopts = [
389389
]
390390
filterwarnings = [
391391
"error",
392-
# TODO: explicitly filter or catch the warnings below where we expect them to be emitted in the tests
393-
"ignore:Consolidated metadata is currently not part in the Zarr format 3 specification.*:UserWarning",
394-
"ignore:Creating a zarr.buffer.gpu.Buffer with an array that does not support the __cuda_array_interface__.*:UserWarning",
395-
"ignore:Automatic shard shape inference is experimental and may change without notice.*:UserWarning",
396-
"ignore:The codec .* is currently not part in the Zarr format 3 specification.*:UserWarning",
397-
"ignore:The dtype .* is currently not part in the Zarr format 3 specification.*:UserWarning",
398-
"ignore:Use zarr.create_array instead.:DeprecationWarning",
399-
"ignore:Duplicate name.*:UserWarning",
400-
"ignore:The `compressor` argument is deprecated. Use `compressors` instead.:UserWarning",
401-
"ignore:Numcodecs codecs are not in the Zarr version 3 specification and may not be supported by other zarr implementations.:UserWarning",
402392
"ignore:Unclosed client session <aiohttp.client.ClientSession.*:ResourceWarning"
403393
]
404394
markers = [

src/zarr/_compat.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from inspect import Parameter, signature
55
from typing import Any, TypeVar
66

7+
from zarr.errors import ZarrFutureWarning
8+
79
T = TypeVar("T")
810

911
# Based off https://github.com/scikit-learn/scikit-learn/blob/e87b32a81c70abed8f2e97483758eb64df8255e9/sklearn/utils/validation.py#L63
@@ -54,7 +56,7 @@ def inner_f(*args: Any, **kwargs: Any) -> T:
5456
f"{version} passing these as positional arguments "
5557
"will result in an error"
5658
),
57-
FutureWarning,
59+
ZarrFutureWarning,
5860
stacklevel=2,
5961
)
6062
kwargs.update(zip(sig.parameters, args, strict=False))

src/zarr/api/asynchronous.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@
3939
create_hierarchy,
4040
)
4141
from zarr.core.metadata import ArrayMetadataDict, ArrayV2Metadata, ArrayV3Metadata
42-
from zarr.errors import GroupNotFoundError, NodeTypeValidationError
42+
from zarr.errors import (
43+
GroupNotFoundError,
44+
NodeTypeValidationError,
45+
ZarrDeprecationWarning,
46+
ZarrRuntimeWarning,
47+
ZarrUserWarning,
48+
)
4349
from zarr.storage import StorePath
4450
from zarr.storage._common import make_store_path
4551

@@ -162,7 +168,7 @@ def _handle_zarr_version_or_format(
162168
)
163169
if zarr_version is not None:
164170
warnings.warn(
165-
"zarr_version is deprecated, use zarr_format", DeprecationWarning, stacklevel=2
171+
"zarr_version is deprecated, use zarr_format", ZarrDeprecationWarning, stacklevel=2
166172
)
167173
return zarr_version
168174
return zarr_format
@@ -228,7 +234,7 @@ async def consolidate_metadata(
228234
warnings.warn(
229235
"Consolidated metadata is currently not part in the Zarr format 3 specification. It "
230236
"may not be supported by other zarr implementations and may change in the future.",
231-
category=UserWarning,
237+
category=ZarrUserWarning,
232238
stacklevel=1,
233239
)
234240

@@ -536,7 +542,7 @@ async def save_group(
536542
await asyncio.gather(*aws)
537543

538544

539-
@deprecated("Use AsyncGroup.tree instead.")
545+
@deprecated("Use AsyncGroup.tree instead.", category=ZarrDeprecationWarning)
540546
async def tree(grp: AsyncGroup, expand: bool | None = None, level: int | None = None) -> Any:
541547
"""Provide a rich display of the hierarchy.
542548
@@ -674,13 +680,13 @@ async def group(
674680
store_path = await make_store_path(store, path=path, mode=mode, storage_options=storage_options)
675681

676682
if chunk_store is not None:
677-
warnings.warn("chunk_store is not yet implemented", RuntimeWarning, stacklevel=2)
683+
warnings.warn("chunk_store is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
678684
if cache_attrs is not None:
679-
warnings.warn("cache_attrs is not yet implemented", RuntimeWarning, stacklevel=2)
685+
warnings.warn("cache_attrs is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
680686
if synchronizer is not None:
681-
warnings.warn("synchronizer is not yet implemented", RuntimeWarning, stacklevel=2)
687+
warnings.warn("synchronizer is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
682688
if meta_array is not None:
683-
warnings.warn("meta_array is not yet implemented", RuntimeWarning, stacklevel=2)
689+
warnings.warn("meta_array is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
684690

685691
if attributes is None:
686692
attributes = {}
@@ -827,13 +833,13 @@ async def open_group(
827833
zarr_format = _handle_zarr_version_or_format(zarr_version=zarr_version, zarr_format=zarr_format)
828834

829835
if cache_attrs is not None:
830-
warnings.warn("cache_attrs is not yet implemented", RuntimeWarning, stacklevel=2)
836+
warnings.warn("cache_attrs is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
831837
if synchronizer is not None:
832-
warnings.warn("synchronizer is not yet implemented", RuntimeWarning, stacklevel=2)
838+
warnings.warn("synchronizer is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
833839
if meta_array is not None:
834-
warnings.warn("meta_array is not yet implemented", RuntimeWarning, stacklevel=2)
840+
warnings.warn("meta_array is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
835841
if chunk_store is not None:
836-
warnings.warn("chunk_store is not yet implemented", RuntimeWarning, stacklevel=2)
842+
warnings.warn("chunk_store is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
837843

838844
store_path = await make_store_path(store, mode=mode, storage_options=storage_options, path=path)
839845
if attributes is None:
@@ -1011,19 +1017,19 @@ async def create(
10111017
)
10121018

10131019
if synchronizer is not None:
1014-
warnings.warn("synchronizer is not yet implemented", RuntimeWarning, stacklevel=2)
1020+
warnings.warn("synchronizer is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
10151021
if chunk_store is not None:
1016-
warnings.warn("chunk_store is not yet implemented", RuntimeWarning, stacklevel=2)
1022+
warnings.warn("chunk_store is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
10171023
if cache_metadata is not None:
1018-
warnings.warn("cache_metadata is not yet implemented", RuntimeWarning, stacklevel=2)
1024+
warnings.warn("cache_metadata is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
10191025
if cache_attrs is not None:
1020-
warnings.warn("cache_attrs is not yet implemented", RuntimeWarning, stacklevel=2)
1026+
warnings.warn("cache_attrs is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
10211027
if object_codec is not None:
1022-
warnings.warn("object_codec is not yet implemented", RuntimeWarning, stacklevel=2)
1028+
warnings.warn("object_codec is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
10231029
if read_only is not None:
1024-
warnings.warn("read_only is not yet implemented", RuntimeWarning, stacklevel=2)
1030+
warnings.warn("read_only is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
10251031
if meta_array is not None:
1026-
warnings.warn("meta_array is not yet implemented", RuntimeWarning, stacklevel=2)
1032+
warnings.warn("meta_array is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
10271033

10281034
if write_empty_chunks is not None:
10291035
_warn_write_empty_chunks_kwarg()
@@ -1042,7 +1048,7 @@ async def create(
10421048
"This is redundant. When both are set, write_empty_chunks will be used instead "
10431049
"of the value in config."
10441050
)
1045-
warnings.warn(UserWarning(msg), stacklevel=1)
1051+
warnings.warn(ZarrUserWarning(msg), stacklevel=1)
10461052
config_parsed = dataclasses.replace(config_parsed, write_empty_chunks=write_empty_chunks)
10471053

10481054
return await AsyncArray._create(

src/zarr/api/synchronous.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from zarr.core.group import Group
1111
from zarr.core.sync import sync
1212
from zarr.core.sync_group import create_hierarchy
13+
from zarr.errors import ZarrDeprecationWarning
1314

1415
if TYPE_CHECKING:
1516
from collections.abc import Iterable
@@ -339,7 +340,7 @@ def save_group(
339340
)
340341

341342

342-
@deprecated("Use Group.tree instead.")
343+
@deprecated("Use Group.tree instead.", category=ZarrDeprecationWarning)
343344
def tree(grp: Group, expand: bool | None = None, level: int | None = None) -> Any:
344345
"""Provide a rich display of the hierarchy.
345346

src/zarr/codecs/transpose.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def validate(
5656
) -> None:
5757
if len(self.order) != len(shape):
5858
raise ValueError(
59-
f"The `order` tuple needs have as many entries as there are dimensions in the array. Got {self.order}."
59+
f"The `order` tuple must have as many entries as there are dimensions in the array. Got {self.order}."
6060
)
6161
if len(self.order) != len(set(self.order)):
6262
raise ValueError(
@@ -71,7 +71,7 @@ def evolve_from_array_spec(self, array_spec: ArraySpec) -> Self:
7171
ndim = array_spec.ndim
7272
if len(self.order) != ndim:
7373
raise ValueError(
74-
f"The `order` tuple needs have as many entries as there are dimensions in the array. Got {self.order}."
74+
f"The `order` tuple must have as many entries as there are dimensions in the array. Got {self.order}."
7575
)
7676
if len(self.order) != len(set(self.order)):
7777
raise ValueError(

src/zarr/convenience.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
save_group,
2323
tree,
2424
)
25+
from zarr.errors import ZarrDeprecationWarning
2526

2627
__all__ = [
2728
"consolidate_metadata",
@@ -40,6 +41,6 @@
4041
warnings.warn(
4142
"zarr.convenience is deprecated. "
4243
"Import these functions from the top level zarr. namespace instead.",
43-
DeprecationWarning,
44+
ZarrDeprecationWarning,
4445
stacklevel=2,
4546
)

src/zarr/core/array.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
)
120120
from zarr.core.metadata.v3 import parse_node_type_array
121121
from zarr.core.sync import sync
122-
from zarr.errors import MetadataValidationError
122+
from zarr.errors import MetadataValidationError, ZarrDeprecationWarning, ZarrUserWarning
123123
from zarr.registry import (
124124
_parse_array_array_codec,
125125
_parse_array_bytes_codec,
@@ -232,7 +232,7 @@ async def get_array_metadata(
232232
if zarr_json_bytes is not None and zarray_bytes is not None:
233233
# warn and favor v3
234234
msg = f"Both zarr.json (Zarr format 3) and .zarray (Zarr format 2) metadata objects exist at {store_path}. Zarr v3 will be used."
235-
warnings.warn(msg, stacklevel=1)
235+
warnings.warn(msg, category=ZarrUserWarning, stacklevel=1)
236236
if zarr_json_bytes is None and zarray_bytes is None:
237237
raise FileNotFoundError(store_path)
238238
# set zarr_format based on which keys were found
@@ -441,7 +441,7 @@ async def create(
441441
) -> AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]: ...
442442

443443
@classmethod
444-
@deprecated("Use zarr.api.asynchronous.create_array instead.")
444+
@deprecated("Use zarr.api.asynchronous.create_array instead.", category=ZarrDeprecationWarning)
445445
async def create(
446446
cls,
447447
store: StoreLike,
@@ -1061,7 +1061,7 @@ def serializer(self) -> ArrayBytesCodec | None:
10611061
)
10621062

10631063
@property
1064-
@deprecated("Use AsyncArray.compressors instead.")
1064+
@deprecated("Use AsyncArray.compressors instead.", category=ZarrDeprecationWarning)
10651065
def compressor(self) -> numcodecs.abc.Codec | None:
10661066
"""
10671067
Compressor that is applied to each chunk of the array.
@@ -1855,7 +1855,7 @@ class Array:
18551855
_async_array: AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]
18561856

18571857
@classmethod
1858-
@deprecated("Use zarr.create_array instead.")
1858+
@deprecated("Use zarr.create_array instead.", category=ZarrDeprecationWarning)
18591859
def create(
18601860
cls,
18611861
store: StoreLike,
@@ -2242,7 +2242,7 @@ def serializer(self) -> None | ArrayBytesCodec:
22422242
return self._async_array.serializer
22432243

22442244
@property
2245-
@deprecated("Use Array.compressors instead.")
2245+
@deprecated("Use Array.compressors instead.", category=ZarrDeprecationWarning)
22462246
def compressor(self) -> numcodecs.abc.Codec | None:
22472247
"""
22482248
Compressor that is applied to each chunk of the array.
@@ -4648,7 +4648,7 @@ def _parse_keep_array_attr(
46484648
warnings.warn(
46494649
"The 'order' attribute of a Zarr format 2 array does not have a direct analogue in Zarr format 3. "
46504650
"The existing order='F' of the source Zarr format 2 array will be ignored.",
4651-
UserWarning,
4651+
ZarrUserWarning,
46524652
stacklevel=2,
46534653
)
46544654
elif order is None and zarr_format == 2:
@@ -4937,7 +4937,7 @@ def _parse_deprecated_compressor(
49374937
if zarr_format == 3:
49384938
warn(
49394939
"The `compressor` argument is deprecated. Use `compressors` instead.",
4940-
category=UserWarning,
4940+
category=ZarrUserWarning,
49414941
stacklevel=2,
49424942
)
49434943
if compressor is None:

0 commit comments

Comments
 (0)