Skip to content

Commit 9d97b24

Browse files
authored
Cleanups to implementation of create() (#3111)
* Remove duplicate compressor handling * Push filter parsing down the stack * Put not implemented warnings together * Test unimplemented kwargs warn * pre-commit fixes * Fix syncrhonous create() signature
1 parent 23edb80 commit 9d97b24

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

src/zarr/api/asynchronous.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
Array,
1616
AsyncArray,
1717
CompressorLike,
18-
_get_default_chunk_encoding_v2,
1918
create_array,
2019
from_array,
2120
get_array_metadata,
@@ -33,7 +32,7 @@
3332
_warn_order_kwarg,
3433
_warn_write_empty_chunks_kwarg,
3534
)
36-
from zarr.core.dtype import ZDTypeLike, get_data_type_from_native_dtype, parse_data_type
35+
from zarr.core.dtype import ZDTypeLike, get_data_type_from_native_dtype
3736
from zarr.core.group import (
3837
AsyncGroup,
3938
ConsolidatedMetadata,
@@ -48,6 +47,8 @@
4847
if TYPE_CHECKING:
4948
from collections.abc import Iterable
5049

50+
import numcodecs.abc
51+
5152
from zarr.abc.codec import Codec
5253
from zarr.core.buffer import NDArrayLikeOrScalar
5354
from zarr.core.chunk_key_encodings import ChunkKeyEncoding
@@ -871,7 +872,7 @@ async def create(
871872
overwrite: bool = False,
872873
path: PathLike | None = None,
873874
chunk_store: StoreLike | None = None,
874-
filters: list[dict[str, JSON]] | None = None, # TODO: type has changed
875+
filters: Iterable[dict[str, JSON] | numcodecs.abc.Codec] | None = None,
875876
cache_metadata: bool | None = None,
876877
cache_attrs: bool | None = None,
877878
read_only: bool | None = None,
@@ -1009,13 +1010,6 @@ async def create(
10091010
_handle_zarr_version_or_format(zarr_version=zarr_version, zarr_format=zarr_format)
10101011
or _default_zarr_format()
10111012
)
1012-
zdtype = parse_data_type(dtype, zarr_format=zarr_format)
1013-
if zarr_format == 2:
1014-
default_filters, default_compressor = _get_default_chunk_encoding_v2(zdtype)
1015-
if not filters:
1016-
filters = default_filters # type: ignore[assignment]
1017-
if compressor == "auto":
1018-
compressor = default_compressor
10191013

10201014
if synchronizer is not None:
10211015
warnings.warn("synchronizer is not yet implemented", RuntimeWarning, stacklevel=2)
@@ -1029,14 +1023,14 @@ async def create(
10291023
warnings.warn("object_codec is not yet implemented", RuntimeWarning, stacklevel=2)
10301024
if read_only is not None:
10311025
warnings.warn("read_only is not yet implemented", RuntimeWarning, stacklevel=2)
1026+
if meta_array is not None:
1027+
warnings.warn("meta_array is not yet implemented", RuntimeWarning, stacklevel=2)
1028+
10321029
if order is not None:
10331030
_warn_order_kwarg()
10341031
if write_empty_chunks is not None:
10351032
_warn_write_empty_chunks_kwarg()
10361033

1037-
if meta_array is not None:
1038-
warnings.warn("meta_array is not yet implemented", RuntimeWarning, stacklevel=2)
1039-
10401034
mode = kwargs.pop("mode", None)
10411035
if mode is None:
10421036
mode = "a"
@@ -1067,7 +1061,7 @@ async def create(
10671061
store_path,
10681062
shape=shape,
10691063
chunks=chunks,
1070-
dtype=zdtype,
1064+
dtype=dtype,
10711065
compressor=compressor,
10721066
fill_value=fill_value,
10731067
overwrite=overwrite,

src/zarr/api/synchronous.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
if TYPE_CHECKING:
1616
from collections.abc import Iterable
1717

18+
import numcodecs.abc
1819
import numpy as np
1920
import numpy.typing as npt
2021

@@ -613,7 +614,7 @@ def create(
613614
overwrite: bool = False,
614615
path: PathLike | None = None,
615616
chunk_store: StoreLike | None = None,
616-
filters: list[dict[str, JSON]] | None = None, # TODO: type has changed
617+
filters: Iterable[dict[str, JSON] | numcodecs.abc.Codec] | None = None,
617618
cache_metadata: bool | None = None,
618619
cache_attrs: bool | None = None,
619620
read_only: bool | None = None,

src/zarr/core/array.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ async def _create(
591591
chunks: ShapeLike | None = None,
592592
dimension_separator: Literal[".", "/"] | None = None,
593593
order: MemoryOrder | None = None,
594-
filters: list[dict[str, JSON]] | None = None,
594+
filters: Iterable[dict[str, JSON] | numcodecs.abc.Codec] | None = None,
595595
compressor: CompressorLike = "auto",
596596
# runtime
597597
overwrite: bool = False,
@@ -850,9 +850,10 @@ async def _create_v2(
850850
else:
851851
await ensure_no_existing_node(store_path, zarr_format=2)
852852

853+
default_filters, default_compressor = _get_default_chunk_encoding_v2(dtype)
853854
compressor_parsed: CompressorLikev2
854855
if compressor == "auto":
855-
_, compressor_parsed = _get_default_chunk_encoding_v2(dtype)
856+
compressor_parsed = default_compressor
856857
elif isinstance(compressor, BytesBytesCodec):
857858
raise ValueError(
858859
"Cannot use a BytesBytesCodec as a compressor for zarr v2 arrays. "
@@ -861,6 +862,9 @@ async def _create_v2(
861862
else:
862863
compressor_parsed = compressor
863864

865+
if filters is None:
866+
filters = default_filters
867+
864868
metadata = cls._create_metadata_v2(
865869
shape=shape,
866870
dtype=dtype,

tests/test_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,3 +1412,10 @@ def test_auto_chunks(f: Callable[..., Array]) -> None:
14121412

14131413
a = f(**kwargs)
14141414
assert a.chunks == (500, 500)
1415+
1416+
1417+
@pytest.mark.parametrize("kwarg_name", ["synchronizer", "chunk_store", "cache_attrs", "meta_array"])
1418+
def test_unimplemented_kwarg_warnings(kwarg_name: str) -> None:
1419+
kwargs = {kwarg_name: 1}
1420+
with pytest.warns(RuntimeWarning, match=".* is not yet implemented"):
1421+
zarr.create(shape=(1,), **kwargs) # type: ignore[arg-type]

0 commit comments

Comments
 (0)