| 
3 | 3 | import json  | 
4 | 4 | import warnings  | 
5 | 5 | from asyncio import gather  | 
6 |  | -from collections.abc import Iterable, Mapping  | 
 | 6 | +from collections.abc import Iterable  | 
7 | 7 | from dataclasses import dataclass, field  | 
8 | 8 | from itertools import starmap  | 
9 | 9 | from logging import getLogger  | 
 | 
93 | 93 | from zarr.core.metadata.v3 import DataType, parse_node_type_array  | 
94 | 94 | from zarr.core.sync import sync  | 
95 | 95 | from zarr.errors import MetadataValidationError  | 
96 |  | -from zarr.registry import get_codec_class, get_pipeline_class  | 
 | 96 | +from zarr.registry import (  | 
 | 97 | +    _parse_array_array_codec,  | 
 | 98 | +    _parse_bytes_bytes_codec,  | 
 | 99 | +    _resolve_codec,  | 
 | 100 | +    get_pipeline_class,  | 
 | 101 | +)  | 
97 | 102 | from zarr.storage import StoreLike, make_store_path  | 
98 | 103 | from zarr.storage.common import StorePath, ensure_no_existing_node  | 
99 | 104 | 
 
  | 
@@ -3546,7 +3551,6 @@ async def create_array(  | 
3546 | 3551 |     # TODO: figure out why putting these imports at top-level causes circular imports  | 
3547 | 3552 |     from zarr.codecs.sharding import ShardingCodec  | 
3548 | 3553 | 
 
  | 
3549 |  | -    # TODO: fix this when modes make sense. It should be `w` for overwriting, `w-` otherwise  | 
3550 | 3554 |     mode: Literal["a"] = "a"  | 
3551 | 3555 |     dtype_parsed = parse_dtype(dtype, zarr_format=zarr_format)  | 
3552 | 3556 |     config_parsed = parse_array_config(config)  | 
@@ -3678,7 +3682,7 @@ def _get_default_encoding_v3(  | 
3678 | 3682 |         dtype_key = "numeric"  | 
3679 | 3683 | 
 
  | 
3680 | 3684 |     codec_dicts = default_codecs[dtype_key]  | 
3681 |  | -    codecs = tuple(get_codec_class(c["name"]).from_dict(c) for c in codec_dicts)  | 
 | 3685 | +    codecs = tuple(_resolve_codec(c) for c in codec_dicts)  | 
3682 | 3686 |     array_bytes_maybe = None  | 
3683 | 3687 |     array_array: list[ArrayArrayCodec] = []  | 
3684 | 3688 |     bytes_bytes: list[BytesBytesCodec] = []  | 
@@ -3710,21 +3714,11 @@ def _get_default_chunk_encoding_v2(  | 
3710 | 3714 |     """  | 
3711 | 3715 |     Get the default chunk encoding for zarr v2 arrays, given a dtype  | 
3712 | 3716 |     """  | 
3713 |  | -    if dtype.kind in "biufcmM":  | 
3714 |  | -        dtype_key = "numeric"  | 
3715 |  | -    elif dtype.kind in "U":  | 
3716 |  | -        dtype_key = "string"  | 
3717 |  | -    elif dtype.kind in "OSV":  | 
3718 |  | -        dtype_key = "bytes"  | 
3719 |  | -    else:  | 
3720 |  | -        raise ValueError(f"Unsupported dtype kind {dtype.kind}")  | 
3721 | 3717 | 
 
  | 
3722 |  | -    compressor_dict = zarr_config.get("array.v2_default_compressor").get(dtype_key, None)  | 
3723 |  | -    filter_dicts = zarr_config.get("array.v2_default_filters").get(dtype_key, [])  | 
 | 3718 | +    compressor_dict = _default_compressor(dtype)  | 
 | 3719 | +    filter_dicts = _default_filters(dtype)  | 
3724 | 3720 | 
 
  | 
3725 |  | -    compressor = None  | 
3726 |  | -    if compressor_dict is not None:  | 
3727 |  | -        compressor = numcodecs.get_codec(compressor_dict)  | 
 | 3721 | +    compressor = numcodecs.get_codec(compressor_dict)  | 
3728 | 3722 |     filters = tuple(numcodecs.get_codec(f) for f in filter_dicts)  | 
3729 | 3723 |     return filters, compressor  | 
3730 | 3724 | 
 
  | 
@@ -3753,28 +3747,34 @@ def _parse_chunk_encoding_v2(  | 
3753 | 3747 | 
 
  | 
3754 | 3748 | def _parse_chunk_encoding_v3(  | 
3755 | 3749 |     *,  | 
3756 |  | -    compression: Iterable[BytesBytesCodec] | Literal["auto"],  | 
3757 |  | -    filters: Iterable[ArrayArrayCodec] | Literal["auto"],  | 
 | 3750 | +    compression: Iterable[BytesBytesCodec | dict[str, JSON]] | Literal["auto"],  | 
 | 3751 | +    filters: Iterable[ArrayArrayCodec | dict[str, JSON]] | Literal["auto"],  | 
3758 | 3752 |     dtype: np.dtype[Any],  | 
3759 | 3753 | ) -> tuple[tuple[ArrayArrayCodec, ...], ArrayBytesCodec, tuple[BytesBytesCodec, ...]]:  | 
3760 | 3754 |     """  | 
3761 | 3755 |     Generate chunk encoding classes for v3 arrays with optional defaults.  | 
3762 | 3756 |     """  | 
3763 | 3757 |     default_array_array, default_array_bytes, default_bytes_bytes = _get_default_encoding_v3(dtype)  | 
 | 3758 | +    maybe_bytes_bytes: Iterable[BytesBytesCodec | dict[str, JSON]]  | 
 | 3759 | +    maybe_array_array: Iterable[ArrayArrayCodec | dict[str, JSON]]  | 
3764 | 3760 | 
 
  | 
3765 | 3761 |     if compression == "auto":  | 
3766 | 3762 |         out_bytes_bytes = default_bytes_bytes  | 
3767 | 3763 |     else:  | 
3768 |  | -        if isinstance(compression, Mapping | Codec):  | 
3769 |  | -            out_bytes_bytes = (compression,)  | 
 | 3764 | +        if isinstance(compression, dict | Codec):  | 
 | 3765 | +            maybe_bytes_bytes = (compression,)  | 
3770 | 3766 |         else:  | 
3771 |  | -            out_bytes_bytes = tuple(compression)  | 
 | 3767 | +            maybe_bytes_bytes = compression  | 
 | 3768 | + | 
 | 3769 | +        out_bytes_bytes = tuple(_parse_bytes_bytes_codec(c) for c in maybe_bytes_bytes)  | 
 | 3770 | + | 
3772 | 3771 |     if filters == "auto":  | 
3773 | 3772 |         out_array_array = default_array_array  | 
3774 | 3773 |     else:  | 
3775 |  | -        if isinstance(filters, Mapping | Codec):  | 
3776 |  | -            out_array_array = (filters,)  | 
 | 3774 | +        if isinstance(filters, dict | Codec):  | 
 | 3775 | +            maybe_array_array = (filters,)  | 
3777 | 3776 |         else:  | 
3778 |  | -            out_array_array = tuple(filters)  | 
 | 3777 | +            maybe_array_array = filters  | 
 | 3778 | +        out_array_array = tuple(_parse_array_array_codec(c) for c in maybe_array_array)  | 
3779 | 3779 | 
 
  | 
3780 | 3780 |     return out_array_array, default_array_bytes, out_bytes_bytes  | 
0 commit comments