Skip to content

Cleanups to implementation of create() #3111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 8 additions & 14 deletions src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
Array,
AsyncArray,
CompressorLike,
_get_default_chunk_encoding_v2,
create_array,
from_array,
get_array_metadata,
Expand All @@ -33,7 +32,7 @@
_warn_order_kwarg,
_warn_write_empty_chunks_kwarg,
)
from zarr.core.dtype import ZDTypeLike, get_data_type_from_native_dtype, parse_data_type
from zarr.core.dtype import ZDTypeLike, get_data_type_from_native_dtype
from zarr.core.group import (
AsyncGroup,
ConsolidatedMetadata,
Expand All @@ -48,6 +47,8 @@
if TYPE_CHECKING:
from collections.abc import Iterable

import numcodecs.abc

from zarr.abc.codec import Codec
from zarr.core.buffer import NDArrayLikeOrScalar
from zarr.core.chunk_key_encodings import ChunkKeyEncoding
Expand Down Expand Up @@ -871,7 +872,7 @@ async def create(
overwrite: bool = False,
path: PathLike | None = None,
chunk_store: StoreLike | None = None,
filters: list[dict[str, JSON]] | None = None, # TODO: type has changed
filters: Iterable[dict[str, JSON] | numcodecs.abc.Codec] | None = None,
cache_metadata: bool | None = None,
cache_attrs: bool | None = None,
read_only: bool | None = None,
Expand Down Expand Up @@ -1009,13 +1010,6 @@ async def create(
_handle_zarr_version_or_format(zarr_version=zarr_version, zarr_format=zarr_format)
or _default_zarr_format()
)
zdtype = parse_data_type(dtype, zarr_format=zarr_format)
if zarr_format == 2:
default_filters, default_compressor = _get_default_chunk_encoding_v2(zdtype)
if not filters:
filters = default_filters # type: ignore[assignment]
if compressor == "auto":
compressor = default_compressor

if synchronizer is not None:
warnings.warn("synchronizer is not yet implemented", RuntimeWarning, stacklevel=2)
Expand All @@ -1029,14 +1023,14 @@ async def create(
warnings.warn("object_codec is not yet implemented", RuntimeWarning, stacklevel=2)
if read_only is not None:
warnings.warn("read_only is not yet implemented", RuntimeWarning, stacklevel=2)
if meta_array is not None:
warnings.warn("meta_array is not yet implemented", RuntimeWarning, stacklevel=2)

if order is not None:
_warn_order_kwarg()
if write_empty_chunks is not None:
_warn_write_empty_chunks_kwarg()

if meta_array is not None:
warnings.warn("meta_array is not yet implemented", RuntimeWarning, stacklevel=2)

mode = kwargs.pop("mode", None)
if mode is None:
mode = "a"
Expand Down Expand Up @@ -1067,7 +1061,7 @@ async def create(
store_path,
shape=shape,
chunks=chunks,
dtype=zdtype,
dtype=dtype,
compressor=compressor,
fill_value=fill_value,
overwrite=overwrite,
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 @@ -15,6 +15,7 @@
if TYPE_CHECKING:
from collections.abc import Iterable

import numcodecs.abc
import numpy as np
import numpy.typing as npt

Expand Down Expand Up @@ -613,7 +614,7 @@ def create(
overwrite: bool = False,
path: PathLike | None = None,
chunk_store: StoreLike | None = None,
filters: list[dict[str, JSON]] | None = None, # TODO: type has changed
filters: Iterable[dict[str, JSON] | numcodecs.abc.Codec] | None = None,
cache_metadata: bool | None = None,
cache_attrs: bool | None = None,
read_only: bool | None = None,
Expand Down
8 changes: 6 additions & 2 deletions src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ async def _create(
chunks: ShapeLike | None = None,
dimension_separator: Literal[".", "/"] | None = None,
order: MemoryOrder | None = None,
filters: list[dict[str, JSON]] | None = None,
filters: Iterable[dict[str, JSON] | numcodecs.abc.Codec] | None = None,
compressor: CompressorLike = "auto",
# runtime
overwrite: bool = False,
Expand Down Expand Up @@ -850,9 +850,10 @@ async def _create_v2(
else:
await ensure_no_existing_node(store_path, zarr_format=2)

default_filters, default_compressor = _get_default_chunk_encoding_v2(dtype)
compressor_parsed: CompressorLikev2
if compressor == "auto":
_, compressor_parsed = _get_default_chunk_encoding_v2(dtype)
compressor_parsed = default_compressor
elif isinstance(compressor, BytesBytesCodec):
raise ValueError(
"Cannot use a BytesBytesCodec as a compressor for zarr v2 arrays. "
Expand All @@ -861,6 +862,9 @@ async def _create_v2(
else:
compressor_parsed = compressor

if filters is None:
filters = default_filters

metadata = cls._create_metadata_v2(
shape=shape,
dtype=dtype,
Expand Down
7 changes: 7 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,3 +1412,10 @@ def test_auto_chunks(f: Callable[..., Array]) -> None:

a = f(**kwargs)
assert a.chunks == (500, 500)


@pytest.mark.parametrize("kwarg_name", ["synchronizer", "chunk_store", "cache_attrs", "meta_array"])
def test_unimplemented_kwarg_warnings(kwarg_name: str) -> None:
kwargs = {kwarg_name: 1}
with pytest.warns(RuntimeWarning, match=".* is not yet implemented"):
zarr.create(shape=(1,), **kwargs) # type: ignore[arg-type]