Skip to content

Commit be60d73

Browse files
committed
adds deprecated compressor arg to Group.create_array
1 parent c5c761e commit be60d73

File tree

7 files changed

+230
-34
lines changed

7 files changed

+230
-34
lines changed

src/zarr/api/synchronous.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818

1919
from zarr.abc.codec import Codec
2020
from zarr.api.asynchronous import ArrayLike, PathLike
21-
from zarr.core.array import CompressorsLike, FiltersLike, SerializerLike, ShardsLike
21+
from zarr.core.array import (
22+
CompressorsLike,
23+
FiltersLike,
24+
SerializerLike,
25+
ShardsLike,
26+
)
2227
from zarr.core.array_spec import ArrayConfig, ArrayConfigLike
2328
from zarr.core.buffer import NDArrayLike
2429
from zarr.core.chunk_key_encodings import ChunkKeyEncoding, ChunkKeyEncodingLike

src/zarr/core/_info.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy as np
77

88
from zarr.abc.codec import Codec
9+
from zarr.core.common import ZarrFormat
910
from zarr.core.metadata.v3 import DataType
1011

1112

@@ -20,7 +21,7 @@ class GroupInfo:
2021

2122
_name: str
2223
_type: Literal["Group"] = "Group"
23-
_zarr_format: Literal[2, 3]
24+
_zarr_format: ZarrFormat
2425
_read_only: bool
2526
_store_type: str
2627
_count_members: int | None = None
@@ -76,7 +77,7 @@ class ArrayInfo:
7677
"""
7778

7879
_type: Literal["Array"] = "Array"
79-
_zarr_format: Literal[2, 3]
80+
_zarr_format: ZarrFormat
8081
_data_type: np.dtype[Any] | DataType
8182
_shape: tuple[int, ...]
8283
_chunk_shape: tuple[int, ...] | None = None

src/zarr/core/array.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3636,8 +3636,10 @@ def _get_default_codecs(
36363636
| Literal["auto"]
36373637
| None
36383638
)
3639+
CompressorLike: TypeAlias = dict[str, JSON] | BytesBytesCodec | numcodecs.abc.Codec | None
36393640
CompressorsLike: TypeAlias = (
3640-
Iterable[dict[str, JSON] | BytesBytesCodec]
3641+
Iterable[dict[str, JSON] | BytesBytesCodec | numcodecs.abc.Codec]
3642+
| dict[str, JSON]
36413643
| BytesBytesCodec
36423644
| numcodecs.abc.Codec
36433645
| Literal["auto"]
@@ -4064,3 +4066,18 @@ def _parse_chunk_encoding_v3(
40644066
out_array_bytes = _parse_array_bytes_codec(serializer)
40654067

40664068
return out_array_array, out_array_bytes, out_bytes_bytes
4069+
4070+
4071+
def _parse_deprecated_compressor(
4072+
compressor: CompressorLike | None, compressors: CompressorsLike
4073+
) -> CompressorsLike | None:
4074+
if compressor:
4075+
if compressors != "auto":
4076+
raise ValueError("Cannot specify both `compressor` and `compressors`.")
4077+
warn(
4078+
"The `compressor` argument is deprecated. Use `compressors` instead.",
4079+
category=UserWarning,
4080+
stacklevel=2,
4081+
)
4082+
compressors = (compressor,)
4083+
return compressors

src/zarr/core/group.py

Lines changed: 187 additions & 25 deletions
Large diffs are not rendered by default.

src/zarr/testing/strategies.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Literal
1+
from typing import Any
22

33
import hypothesis.extra.numpy as npst
44
import hypothesis.strategies as st
@@ -8,6 +8,7 @@
88

99
import zarr
1010
from zarr.core.array import Array
11+
from zarr.core.common import ZarrFormat
1112
from zarr.core.sync import sync
1213
from zarr.storage import MemoryStore, StoreLike
1314
from zarr.storage.common import _dereference_path
@@ -69,7 +70,7 @@ def v2_dtypes() -> st.SearchStrategy[np.dtype]:
6970
# So we map a clear to reset the store.
7071
stores = st.builds(MemoryStore, st.just({})).map(lambda x: sync(x.clear()))
7172
compressors = st.sampled_from([None, "default"])
72-
zarr_formats: st.SearchStrategy[Literal[2, 3]] = st.sampled_from([2, 3])
73+
zarr_formats: st.SearchStrategy[ZarrFormat] = st.sampled_from([2, 3])
7374
array_shapes = npst.array_shapes(max_dims=4, min_side=0)
7475

7576

@@ -78,7 +79,7 @@ def numpy_arrays(
7879
draw: st.DrawFn,
7980
*,
8081
shapes: st.SearchStrategy[tuple[int, ...]] = array_shapes,
81-
zarr_formats: st.SearchStrategy[Literal[2, 3]] = zarr_formats,
82+
zarr_formats: st.SearchStrategy[ZarrFormat] = zarr_formats,
8283
) -> Any:
8384
"""
8485
Generate numpy arrays that can be saved in the provided Zarr format.

tests/test_array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ def test_vlen_errors() -> None:
466466

467467

468468
@pytest.mark.parametrize("zarr_format", [2, 3])
469-
def test_update_attrs(zarr_format: Literal[2, 3]) -> None:
469+
def test_update_attrs(zarr_format: ZarrFormat) -> None:
470470
# regression test for https://github.com/zarr-developers/zarr-python/issues/2328
471471
store = MemoryStore()
472472
arr = zarr.create_array(
@@ -799,7 +799,7 @@ def test_array_create_metadata_order_v2(
799799
@pytest.mark.parametrize("store", ["memory"], indirect=True)
800800
def test_array_create_order(
801801
order_config: MemoryOrder | None,
802-
zarr_format: Literal[2, 3],
802+
zarr_format: ZarrFormat,
803803
store: MemoryStore,
804804
) -> None:
805805
"""

tests/test_group.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,3 +1427,13 @@ def test_delitem_removes_children(store: Store, zarr_format: ZarrFormat) -> None
14271427
del g1["0"]
14281428
with pytest.raises(KeyError):
14291429
g1["0/0"]
1430+
1431+
1432+
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
1433+
def test_deprecated_compressor(store: Store) -> None:
1434+
g = zarr.group(store=store, zarr_format=2)
1435+
with pytest.warns(UserWarning, match="The `compressor` argument is deprecated.*"):
1436+
a = g.create_array(
1437+
"foo", shape=(100,), chunks=(10,), dtype="i4", compressor={"id": "blosc"}
1438+
)
1439+
assert a.metadata.compressor.codec_id == "blosc"

0 commit comments

Comments
 (0)