Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
79dd9e2
define zarr-specific FutureWarning and DeprecationWarning
d-v-b May 25, 2025
b2e25cf
Merge branch 'main' of github.com:zarr-developers/zarr-python into re…
d-v-b Jun 30, 2025
7c644eb
make unstablespecificationwarning an instance of zarrfuturewarning
d-v-b Jun 30, 2025
c604075
use pytest.warns instead of pytest.raises
d-v-b Jun 30, 2025
b964ff6
Merge branch 'main' into refactor-warnings
d-v-b Jul 11, 2025
12bea9c
Merge branch 'main' into refactor-warnings
d-v-b Jul 11, 2025
8267c0c
Merge branch 'main' into refactor-warnings
d-v-b Jul 11, 2025
702ef09
Merge branch 'main' of https://github.com/zarr-developers/zarr-python…
d-v-b Jul 31, 2025
64e1405
changelog
d-v-b Jul 31, 2025
d322cad
ensure that all deprecations are ZarrDeprecations
d-v-b Aug 1, 2025
31e63aa
add docstrings and export warnings
d-v-b Aug 1, 2025
917fd3d
fix imports
d-v-b Aug 1, 2025
9055d3b
handle warnings in tests explicitly; make userwarnings ZarrUserWarning
d-v-b Aug 1, 2025
fee48d3
fix doctests by making them more realistic
d-v-b Aug 1, 2025
6a208eb
lint and fix typo
d-v-b Aug 1, 2025
8a2bb9e
Merge branch 'main' into refactor-warnings
d-v-b Aug 1, 2025
a235660
move unstable spec warning to errors
d-v-b Aug 1, 2025
8afdce9
Merge branch 'refactor-warnings' of https://github.com/d-v-b/zarr-pyt…
d-v-b Aug 1, 2025
33a9184
handle warnings in gpu tests
d-v-b Aug 1, 2025
fc72752
handle more warnings
d-v-b Aug 1, 2025
42b4cb5
handle another warning
d-v-b Aug 1, 2025
713bda9
Fix exports
d-v-b Aug 1, 2025
f483eb8
Update src/zarr/errors.py
d-v-b Aug 2, 2025
d029800
Update changes/3098.misc.rst
d-v-b Aug 2, 2025
94ab461
Merge branch 'main' into refactor-warnings
d-v-b Aug 4, 2025
c11328b
Merge branch 'main' into refactor-warnings
d-v-b Aug 4, 2025
fd76671
Merge branch 'main' into refactor-warnings
d-v-b Aug 4, 2025
b7861b7
Merge branch 'main' into refactor-warnings
d-v-b Aug 5, 2025
ba5be4f
Update src/zarr/errors.py
d-v-b Aug 5, 2025
df0eef4
add test to ensure that ambiguous group.open warns
d-v-b Aug 5, 2025
61ff062
update changelog
d-v-b Aug 5, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changes/3098.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Define Zarr-specific warning classes ``ZarrDeprecationWarning`` and ``ZarrFutureWarning``, that
subclass ``DeprecationWarning`` and ``FutureWarning``, respectively.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ filterwarnings = [
"ignore:Automatic shard shape inference is experimental and may change without notice.*:UserWarning",
"ignore:The codec .* is currently not part in the Zarr format 3 specification.*:UserWarning",
"ignore:The dtype .* is currently not part in the Zarr format 3 specification.*:UserWarning",
"ignore:Use zarr.create_array instead.:DeprecationWarning",
"ignore:Use zarr.create_array instead.:zarr.errors.ZarrDeprecationWarning",
"ignore:Duplicate name.*:UserWarning",
"ignore:The `compressor` argument is deprecated. Use `compressors` instead.:UserWarning",
"ignore:Numcodecs codecs are not in the Zarr version 3 specification and may not be supported by other zarr implementations.:UserWarning",
Expand Down
4 changes: 3 additions & 1 deletion src/zarr/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from inspect import Parameter, signature
from typing import Any, TypeVar

from zarr.errors import ZarrFutureWarning

T = TypeVar("T")

# Based off https://github.com/scikit-learn/scikit-learn/blob/e87b32a81c70abed8f2e97483758eb64df8255e9/sklearn/utils/validation.py#L63
Expand Down Expand Up @@ -54,7 +56,7 @@ def inner_f(*args: Any, **kwargs: Any) -> T:
f"{version} passing these as positional arguments "
"will result in an error"
),
FutureWarning,
ZarrFutureWarning,
stacklevel=2,
)
kwargs.update(zip(sig.parameters, args, strict=False))
Expand Down
6 changes: 3 additions & 3 deletions src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
create_hierarchy,
)
from zarr.core.metadata import ArrayMetadataDict, ArrayV2Metadata, ArrayV3Metadata
from zarr.errors import GroupNotFoundError, NodeTypeValidationError
from zarr.errors import GroupNotFoundError, NodeTypeValidationError, ZarrDeprecationWarning
from zarr.storage import StorePath
from zarr.storage._common import make_store_path

Expand Down Expand Up @@ -162,7 +162,7 @@ def _handle_zarr_version_or_format(
)
if zarr_version is not None:
warnings.warn(
"zarr_version is deprecated, use zarr_format", DeprecationWarning, stacklevel=2
"zarr_version is deprecated, use zarr_format", ZarrDeprecationWarning, stacklevel=2
)
return zarr_version
return zarr_format
Expand Down Expand Up @@ -536,7 +536,7 @@ async def save_group(
await asyncio.gather(*aws)


@deprecated("Use AsyncGroup.tree instead.")
@deprecated("Use AsyncGroup.tree instead.", category=ZarrDeprecationWarning)
async def tree(grp: AsyncGroup, expand: bool | None = None, level: int | None = None) -> Any:
"""Provide a rich display of the hierarchy.

Expand Down
3 changes: 2 additions & 1 deletion src/zarr/api/synchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from zarr.core.group import Group
from zarr.core.sync import sync
from zarr.core.sync_group import create_hierarchy
from zarr.errors import ZarrDeprecationWarning

if TYPE_CHECKING:
from collections.abc import Iterable
Expand Down Expand Up @@ -339,7 +340,7 @@ def save_group(
)


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

Expand Down
3 changes: 2 additions & 1 deletion src/zarr/convenience.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
save_group,
tree,
)
from zarr.errors import ZarrDeprecationWarning

__all__ = [
"consolidate_metadata",
Expand All @@ -40,6 +41,6 @@
warnings.warn(
"zarr.convenience is deprecated. "
"Import these functions from the top level zarr. namespace instead.",
DeprecationWarning,
ZarrDeprecationWarning,
stacklevel=2,
)
6 changes: 3 additions & 3 deletions src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
)
from zarr.core.metadata.v3 import parse_node_type_array
from zarr.core.sync import sync
from zarr.errors import MetadataValidationError
from zarr.errors import MetadataValidationError, ZarrDeprecationWarning
from zarr.registry import (
_parse_array_array_codec,
_parse_array_bytes_codec,
Expand Down Expand Up @@ -1061,7 +1061,7 @@ def serializer(self) -> ArrayBytesCodec | None:
)

@property
@deprecated("Use AsyncArray.compressors instead.")
@deprecated("Use AsyncArray.compressors instead.", category=ZarrDeprecationWarning)
def compressor(self) -> numcodecs.abc.Codec | None:
"""
Compressor that is applied to each chunk of the array.
Expand Down Expand Up @@ -2242,7 +2242,7 @@ def serializer(self) -> None | ArrayBytesCodec:
return self._async_array.serializer

@property
@deprecated("Use Array.compressors instead.")
@deprecated("Use Array.compressors instead.", category=ZarrDeprecationWarning)
def compressor(self) -> numcodecs.abc.Codec | None:
"""
Compressor that is applied to each chunk of the array.
Expand Down
3 changes: 2 additions & 1 deletion src/zarr/core/dtype/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing_extensions import ReadOnly

from zarr.core.common import NamedConfig
from zarr.errors import ZarrFutureWarning

EndiannessStr = Literal["little", "big"]
ENDIANNESS_STR: Final = "little", "big"
Expand Down Expand Up @@ -216,7 +217,7 @@ class HasObjectCodec:
object_codec_id: ClassVar[str]


class UnstableSpecificationWarning(FutureWarning): ...
class UnstableSpecificationWarning(ZarrFutureWarning): ...


def v3_unstable_dtype_warning(dtype: object) -> None:
Expand Down
15 changes: 10 additions & 5 deletions src/zarr/core/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@
from zarr.core.config import config
from zarr.core.metadata import ArrayV2Metadata, ArrayV3Metadata
from zarr.core.sync import SyncMixin, sync
from zarr.errors import ContainsArrayError, ContainsGroupError, MetadataValidationError
from zarr.errors import (
ContainsArrayError,
ContainsGroupError,
MetadataValidationError,
ZarrDeprecationWarning,
)
from zarr.storage import StoreLike, StorePath
from zarr.storage._common import ensure_no_existing_node, make_store_path
from zarr.storage._utils import _join_paths, _normalize_path_keys, normalize_path
Expand Down Expand Up @@ -1142,7 +1147,7 @@ async def create_array(
write_data=write_data,
)

@deprecated("Use AsyncGroup.create_array instead.")
@deprecated("Use AsyncGroup.create_array instead.", category=ZarrDeprecationWarning)
async def create_dataset(
self, name: str, *, shape: ShapeLike, **kwargs: Any
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
Expand Down Expand Up @@ -1176,7 +1181,7 @@ async def create_dataset(
await array.setitem(slice(None), data)
return array

@deprecated("Use AsyncGroup.require_array instead.")
@deprecated("Use AsyncGroup.require_array instead.", category=ZarrDeprecationWarning)
async def require_dataset(
self,
name: str,
Expand Down Expand Up @@ -2577,7 +2582,7 @@ def create_array(
)
)

@deprecated("Use Group.create_array instead.")
@deprecated("Use Group.create_array instead.", category=ZarrDeprecationWarning)
def create_dataset(self, name: str, **kwargs: Any) -> Array:
"""Create an array.

Expand All @@ -2601,7 +2606,7 @@ def create_dataset(self, name: str, **kwargs: Any) -> Array:
"""
return Array(self._sync(self._async_group.create_dataset(name, **kwargs)))

@deprecated("Use Group.require_array instead.")
@deprecated("Use Group.require_array instead.", category=ZarrDeprecationWarning)
def require_dataset(self, name: str, *, shape: ShapeLike, **kwargs: Any) -> Array:
"""Obtain an array, creating if it doesn't exist.

Expand Down
3 changes: 2 additions & 1 deletion src/zarr/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
zeros,
zeros_like,
)
from zarr.errors import ZarrDeprecationWarning

__all__ = [
"array",
Expand All @@ -42,6 +43,6 @@
warnings.warn(
"zarr.creation is deprecated. "
"Import these functions from the top level zarr. namespace instead.",
DeprecationWarning,
ZarrDeprecationWarning,
stacklevel=2,
)
6 changes: 6 additions & 0 deletions src/zarr/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,9 @@ class NodeTypeValidationError(MetadataValidationError):
This can be raised when the value is invalid or unexpected given the context,
for example an 'array' node when we expected a 'group'.
"""


class ZarrFutureWarning(FutureWarning): ...


class ZarrDeprecationWarning(DeprecationWarning): ...
3 changes: 2 additions & 1 deletion src/zarr/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from types import ModuleType
from typing import Any

from zarr.errors import ZarrDeprecationWarning
from zarr.storage._common import StoreLike, StorePath
from zarr.storage._fsspec import FsspecStore
from zarr.storage._local import LocalStore
Expand Down Expand Up @@ -33,7 +34,7 @@ def __setattr__(self, attr: str, value: Any) -> None:
"setting zarr.storage.default_compressor is deprecated, use "
"zarr.config to configure array.v2_default_compressor "
"e.g. config.set({'codecs.zstd':'numcodecs.Zstd', 'array.v2_default_compressor.numeric': 'zstd'})",
DeprecationWarning,
ZarrDeprecationWarning,
stacklevel=1,
)
else:
Expand Down
8 changes: 5 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import zarr.storage
from zarr.core.array import init_array
from zarr.storage._common import StorePath
from zarr.storage._local import LocalStore
from zarr.storage._zip import ZipStore

if TYPE_CHECKING:
from collections.abc import Callable
Expand Down Expand Up @@ -41,8 +43,8 @@
save_group,
)
from zarr.core.buffer import NDArrayLike
from zarr.errors import MetadataValidationError
from zarr.storage import LocalStore, MemoryStore, ZipStore
from zarr.errors import MetadataValidationError, ZarrDeprecationWarning
from zarr.storage import MemoryStore
from zarr.storage._utils import normalize_path
from zarr.testing.utils import gpu_test

Expand Down Expand Up @@ -470,7 +472,7 @@ def test_tree() -> None:
g3.create_group("baz")
g5 = g3.create_group("qux")
g5.create_array("baz", shape=(100,), chunks=(10,), dtype="float64")
with pytest.warns(DeprecationWarning, match=r"Group\.tree instead\."): # noqa: PT031
with pytest.warns(ZarrDeprecationWarning, match=r"Group\.tree instead\."): # noqa: PT031
assert repr(zarr.tree(g1)) == repr(g1.tree())
assert str(zarr.tree(g1)) == str(g1.tree())

Expand Down
5 changes: 4 additions & 1 deletion tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@
from zarr.core.metadata.v2 import ArrayV2Metadata
from zarr.core.metadata.v3 import ArrayV3Metadata
from zarr.core.sync import sync
from zarr.errors import ContainsArrayError, ContainsGroupError
from zarr.errors import (
ContainsArrayError,
ContainsGroupError,
)
from zarr.storage import LocalStore, MemoryStore, StorePath

from .test_dtype.conftest import zdtype_examples
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dtype/test_npy/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_unstable_dtype_warning(
Test that we get a warning when serializing a dtype without a zarr v3 spec to json
when zarr_format is 3
"""
with pytest.raises(UnstableSpecificationWarning):
with pytest.warns(UnstableSpecificationWarning):
zdtype.to_json(zarr_format=3)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_dtype/test_npy/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_unstable_dtype_warning(zdtype: FixedLengthUTF32 | VariableLengthUTF8) -
Test that we get a warning when serializing a dtype without a zarr v3 spec to json
when zarr_format is 3
"""
with pytest.raises(UnstableSpecificationWarning):
with pytest.warns(UnstableSpecificationWarning):
zdtype.to_json(zarr_format=3)


Expand Down
19 changes: 12 additions & 7 deletions tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
)
from zarr.core.metadata.v3 import ArrayV3Metadata
from zarr.core.sync import _collect_aiterator, sync
from zarr.errors import ContainsArrayError, ContainsGroupError, MetadataValidationError
from zarr.errors import (
ContainsArrayError,
ContainsGroupError,
MetadataValidationError,
ZarrDeprecationWarning,
)
from zarr.storage import LocalStore, MemoryStore, StorePath, ZipStore
from zarr.storage._common import make_store_path
from zarr.storage._utils import _join_paths, normalize_path
Expand Down Expand Up @@ -648,7 +653,7 @@ def test_group_create_array(
array = group.create_array(name=name, shape=shape, dtype=dtype)
array[:] = data
elif method == "array":
with pytest.warns(DeprecationWarning, match=r"Group\.create_array instead\."):
with pytest.warns(ZarrDeprecationWarning, match=r"Group\.create_array instead\."):
array = group.array(name=name, data=data, shape=shape, dtype=dtype)
else:
raise AssertionError
Expand All @@ -660,7 +665,7 @@ def test_group_create_array(
a[:] = data
elif method == "array":
with pytest.raises(ContainsArrayError): # noqa: PT012
with pytest.warns(DeprecationWarning, match=r"Group\.create_array instead\."):
with pytest.warns(ZarrDeprecationWarning, match=r"Group\.create_array instead\."):
a = group.array(name=name, shape=shape, dtype=dtype)
a[:] = data

Expand Down Expand Up @@ -1194,27 +1199,27 @@ def test_create_dataset_with_data(store: Store, zarr_format: ZarrFormat) -> None
"""
root = Group.from_store(store=store, zarr_format=zarr_format)
arr = np.random.random((5, 5))
with pytest.warns(DeprecationWarning, match=r"Group\.create_array instead\."):
with pytest.warns(ZarrDeprecationWarning, match=r"Group\.create_array instead\."):
data = root.create_dataset("random", data=arr, shape=arr.shape)
np.testing.assert_array_equal(np.asarray(data), arr)


async def test_create_dataset(store: Store, zarr_format: ZarrFormat) -> None:
root = await AsyncGroup.from_store(store=store, zarr_format=zarr_format)
with pytest.warns(DeprecationWarning, match=r"Group\.create_array instead\."):
with pytest.warns(ZarrDeprecationWarning, match=r"Group\.create_array instead\."):
foo = await root.create_dataset("foo", shape=(10,), dtype="uint8")
assert foo.shape == (10,)

with (
pytest.raises(ContainsArrayError),
pytest.warns(DeprecationWarning, match=r"Group\.create_array instead\."),
pytest.warns(ZarrDeprecationWarning, match=r"Group\.create_array instead\."),
):
await root.create_dataset("foo", shape=(100,), dtype="int8")

_ = await root.create_group("bar")
with (
pytest.raises(ContainsGroupError),
pytest.warns(DeprecationWarning, match=r"Group\.create_array instead\."),
pytest.warns(ZarrDeprecationWarning, match=r"Group\.create_array instead\."),
):
await root.create_dataset("bar", shape=(100,), dtype="int8")

Expand Down
3 changes: 2 additions & 1 deletion tests/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from zarr.core.dtype.wrapper import ZDType
from zarr.core.group import Group
from zarr.core.sync import sync
from zarr.errors import ZarrDeprecationWarning
from zarr.storage import MemoryStore, StorePath


Expand Down Expand Up @@ -226,7 +227,7 @@ def test_v2_non_contiguous(numpy_order: Literal["C", "F"], zarr_order: Literal["


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


Expand Down
Loading