Skip to content

Commit 31c95ca

Browse files
committed
recover from bad rebase
1 parent 60939c2 commit 31c95ca

56 files changed

Lines changed: 4622 additions & 1747 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/zarr/abc/codec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from collections.abc import Awaitable, Callable, Iterable
2323
from typing import Self
2424

25-
from zarr.abc.store import ByteGetter, ByteSetter
25+
from zarr.abc.store import ByteGetter, ByteSetter, Store
2626
from zarr.core.array_spec import ArraySpec
2727
from zarr.core.chunk_grids import ChunkGrid
2828
from zarr.core.dtype.wrapper import TBaseDType, TBaseScalar, ZDType

src/zarr/api/asynchronous.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
import numpy.typing as npt
1010
from typing_extensions import deprecated
1111

12+
from zarr.abc.store import Store
1213
from zarr.core.array import (
14+
DEFAULT_FILL_VALUE,
1315
Array,
1416
AsyncArray,
15-
_get_default_chunk_encoding_v2,
17+
CompressorLike,
1618
create_array,
19+
from_array,
1720
get_array_metadata,
1821
)
19-
from zarr.core.array_spec import ArrayConfig, ArrayConfigLike, ArrayConfigParams
22+
from zarr.core.array_spec import ArrayConfigLike, parse_array_config
2023
from zarr.core.buffer import NDArrayLike
2124
from zarr.core.common import (
2225
JSON,
@@ -28,15 +31,16 @@
2831
_default_zarr_format,
2932
_warn_write_empty_chunks_kwarg,
3033
)
31-
from zarr.core.dtype import ZDTypeLike, get_data_type_from_native_dtype, parse_data_type
34+
from zarr.core.dtype import ZDTypeLike, get_data_type_from_native_dtype
3235
from zarr.core.group import (
3336
AsyncGroup,
3437
ConsolidatedMetadata,
3538
GroupMetadata,
3639
create_hierarchy,
3740
)
3841
from zarr.core.metadata import ArrayMetadataDict, ArrayV2Metadata, ArrayV3Metadata
39-
from zarr.errors import NodeTypeValidationError
42+
from zarr.errors import GroupNotFoundError, NodeTypeValidationError
43+
from zarr.storage import StorePath
4044
from zarr.storage._common import make_store_path
4145

4246
if TYPE_CHECKING:
@@ -857,7 +861,7 @@ async def open_group(
857861
async def create(
858862
shape: ChunkCoords | int,
859863
*, # Note: this is a change from v2
860-
chunks: ChunkCoords | int | None = None, # TODO: v2 allowed chunks=True
864+
chunks: ChunkCoords | int | bool | None = None,
861865
dtype: ZDTypeLike | None = None,
862866
compressor: CompressorLike = "auto",
863867
fill_value: Any | None = DEFAULT_FILL_VALUE,
@@ -1005,21 +1009,6 @@ async def create(
10051009
_handle_zarr_version_or_format(zarr_version=zarr_version, zarr_format=zarr_format)
10061010
or _default_zarr_format()
10071011
)
1008-
zdtype = parse_data_type(dtype, zarr_format=zarr_format)
1009-
if zarr_format == 2:
1010-
if chunks is None:
1011-
chunks = shape
1012-
default_filters, default_compressor = _get_default_chunk_encoding_v2(zdtype)
1013-
if not filters:
1014-
filters = default_filters # type: ignore[assignment]
1015-
if compressor is None:
1016-
compressor = default_compressor
1017-
elif zarr_format == 3 and chunk_shape is None: # type: ignore[redundant-expr]
1018-
if chunks is not None:
1019-
chunk_shape = chunks
1020-
chunks = None
1021-
else:
1022-
chunk_shape = shape
10231012

10241013
if synchronizer is not None:
10251014
warnings.warn("synchronizer is not yet implemented", RuntimeWarning, stacklevel=2)
@@ -1060,7 +1049,7 @@ async def create(
10601049
store_path,
10611050
shape=shape,
10621051
chunks=chunks,
1063-
dtype=zdtype,
1052+
dtype=dtype,
10641053
compressor=compressor,
10651054
fill_value=fill_value,
10661055
overwrite=overwrite,

src/zarr/api/synchronous.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ def create(
602602
chunks: ChunkCoords | int | bool | None = None,
603603
dtype: ZDTypeLike | None = None,
604604
compressor: CompressorLike = "auto",
605-
fill_value: Any | None = None, # TODO: need type
605+
fill_value: Any | None = DEFAULT_FILL_VALUE, # TODO: need type
606606
order: MemoryOrder | None = None,
607607
store: str | StoreLike | None = None,
608608
synchronizer: Any | None = None,

src/zarr/codecs/blosc.py

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import asyncio
44
from collections.abc import Mapping
55
from dataclasses import dataclass, replace
6-
from enum import Enum
76
from functools import cached_property
87
from typing import TYPE_CHECKING, Final, Literal, NotRequired, TypedDict, TypeGuard, overload
98

@@ -17,7 +16,6 @@
1716
JSON,
1817
NamedRequiredConfig,
1918
ZarrFormat,
20-
parse_named_configuration,
2119
)
2220
from zarr.core.dtype.common import HasItemSize
2321
from zarr.registry import register_codec
@@ -42,18 +40,21 @@ class BloscConfigV2(TypedDict):
4240
blocksize: int
4341
typesize: NotRequired[int]
4442

43+
4544
class BloscConfigV3(TypedDict):
4645
cname: BloscCname
4746
clevel: int
4847
shuffle: BloscShuffle
4948
blocksize: int
5049
typesize: int
5150

51+
5252
class BloscJSON_V2(CodecJSON_V2[Literal["blosc"]], BloscConfigV2):
5353
"""
5454
The JSON form of the Blosc codec in Zarr V2.
5555
"""
5656

57+
5758
class BloscJSON_V3(NamedRequiredConfig[Literal["blosc"], BloscConfigV3]):
5859
"""
5960
The JSON form of the Blosc codec in Zarr V3.
@@ -81,11 +82,10 @@ def check_json_v3(data: CodecJSON) -> TypeGuard[BloscJSON_V3]:
8182

8283
def parse_cname(value: object) -> BloscCname:
8384
if value not in BLOSC_CNAME:
84-
raise ValueError(
85-
f"Value must be one of {BLOSC_CNAME}. Got {value} instead."
86-
)
85+
raise ValueError(f"Value must be one of {BLOSC_CNAME}. Got {value} instead.")
8786
return value
8887

88+
8989
# See https://zarr.readthedocs.io/en/stable/user-guide/performance.html#configuring-blosc
9090
numcodecs.blosc.use_threads = False
9191

@@ -154,21 +154,9 @@ def __init__(
154154
@classmethod
155155
def from_dict(cls, data: dict[str, JSON]) -> Self:
156156
return cls.from_json(data, zarr_format=3)
157-
_, configuration_parsed = parse_named_configuration(data, "blosc")
158-
return cls(**configuration_parsed) # type: ignore[arg-type]
159157

160158
def to_dict(self) -> dict[str, JSON]:
161159
return self.to_json(zarr_format=3)
162-
return {
163-
"name": "blosc",
164-
"configuration": {
165-
"typesize": self.typesize,
166-
"cname": self.cname,
167-
"clevel": self.clevel,
168-
"shuffle": self.shuffle,
169-
"blocksize": self.blocksize,
170-
},
171-
}
172160

173161
@classmethod
174162
def _from_json_v2(cls, data: CodecJSON) -> Self:
@@ -213,22 +201,23 @@ def to_json(self, zarr_format: ZarrFormat) -> BloscJSON_V2 | BloscJSON_V3:
213201
raise ValueError("typesize and blocksize need to be set for encoding.")
214202
if zarr_format == 2:
215203
return {
216-
"id": "blosc",
204+
"id": "blosc",
217205
"clevel": self.clevel,
218206
"cname": self.cname,
219207
"shuffle": BLOSC_SHUFFLE.index(self.shuffle),
220-
"blocksize": self.blocksize
221-
}
208+
"blocksize": self.blocksize,
209+
}
222210
elif zarr_format == 3:
223211
return {
224-
"name": "blosc",
212+
"name": "blosc",
225213
"configuration": {
226214
"clevel": self.clevel,
227215
"cname": self.cname,
228216
"shuffle": self.shuffle,
229217
"typesize": self.typesize,
230-
"blocksize": self.blocksize
231-
}}
218+
"blocksize": self.blocksize,
219+
},
220+
}
232221
raise ValueError(
233222
f"Unsupported Zarr format {zarr_format}. Expected 2 or 3."
234223
) # pragma: no cover
@@ -241,10 +230,7 @@ def evolve_from_array_spec(self, array_spec: ArraySpec) -> Self:
241230
if new_codec.typesize is None:
242231
new_codec = replace(new_codec, typesize=item_size)
243232
if new_codec.shuffle is None:
244-
new_codec = replace(
245-
new_codec,
246-
shuffle="bitshuffle" if item_size == 1 else "shuffle"),
247-
)
233+
new_codec = replace(new_codec, shuffle="bitshuffle" if item_size == 1 else "shuffle")
248234

249235
return new_codec
250236

src/zarr/codecs/bytes.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from zarr.core.buffer import Buffer, NDArrayLike, NDBuffer
1313
from zarr.core.common import JSON, NamedConfig, ZarrFormat
1414
from zarr.core.dtype.common import HasEndianness
15-
from zarr.core.dtype.npy.common import endianness_to_numpy_str
1615
from zarr.registry import register_codec
1716

1817
if TYPE_CHECKING:

src/zarr/codecs/gzip.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def parse_gzip_level(data: JSON) -> int:
3636
class GZipConfig(TypedDict):
3737
level: int
3838

39+
3940
class GZipJSON_V2(CodecJSON_V2[Literal["gzip"]], GZipConfig):
4041
"""
4142
The JSON form of the GZip codec in Zarr V2.

src/zarr/codecs/numcodec.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
import asyncio
88
from dataclasses import dataclass
9-
from typing import TYPE_CHECKING, Callable, Literal, Self, TypeGuard, overload
9+
from typing import TYPE_CHECKING, Literal, Self, TypeGuard, overload
1010

11-
import numcodecs
12-
import numcodecs.registry as numcodecs_registry
1311
import numpy as np
1412
from typing_extensions import Protocol, runtime_checkable
1513

@@ -31,6 +29,7 @@
3129

3230
BufferOrNDArray = Buffer | np.ndarray[tuple[int, ...], np.dtype[np.generic]] | NDArrayLike
3331

32+
3433
def get_numcodec_class(name: str) -> type[Numcodec]:
3534
"""Obtain a numcodec codec class by name.
3635
@@ -52,6 +51,8 @@ def get_numcodec_class(name: str) -> type[Numcodec]:
5251
Zlib(level=1)
5352
5453
"""
54+
import numcodecs.registry as numcodecs_registry
55+
5556
cls = numcodecs_registry.codec_registry.get(name)
5657
if cls is None and name in numcodecs_registry.entries:
5758
cls = numcodecs_registry.entries[name].load()
@@ -60,9 +61,6 @@ def get_numcodec_class(name: str) -> type[Numcodec]:
6061
return cls
6162
raise KeyError(name)
6263

63-
def resolve_numcodec(config: CodecJSON_V2[str]) -> Numcodec:
64-
return numcodecs.get_codec(config) # type: ignore[no-any-return]
65-
6664

6765
@runtime_checkable
6866
class Numcodec(Protocol):
@@ -83,25 +81,26 @@ def get_config(self) -> CodecJSON_V2[str]: ...
8381
@classmethod
8482
def from_config(cls, config: CodecJSON_V2[str]) -> Self: ...
8583

84+
8685
def is_numcodec_cls(obj: object) -> TypeGuard[type[Numcodec]]:
8786
"""
8887
Check if the given object implements the Numcodec protocol. Because the @runtime_checkable
8988
decorator does not allow issubclass checks for protocols with non-method members (i.e., attributes),
9089
we need to manually check for the presence of the required attributes and methods.
9190
"""
9291
return (
93-
isinstance(obj, type) and
94-
hasattr(obj, "codec_id") and
95-
isinstance(obj.codec_id, str) and
96-
hasattr(obj, "encode") and
97-
callable(obj.encode) and
98-
hasattr(obj, "decode") and
99-
callable(obj.decode) and
100-
hasattr(obj, "get_config") and
101-
callable(obj.get_config) and
102-
hasattr(obj, "from_config") and
103-
callable(obj.from_config)
104-
)
92+
isinstance(obj, type)
93+
and hasattr(obj, "codec_id")
94+
and isinstance(obj.codec_id, str)
95+
and hasattr(obj, "encode")
96+
and callable(obj.encode)
97+
and hasattr(obj, "decode")
98+
and callable(obj.decode)
99+
and hasattr(obj, "get_config")
100+
and callable(obj.get_config)
101+
and hasattr(obj, "from_config")
102+
and callable(obj.from_config)
103+
)
105104

106105

107106
@dataclass(frozen=True, kw_only=True)
@@ -113,9 +112,7 @@ def to_json(self, zarr_format: Literal[2]) -> CodecJSON_V2[str]: ...
113112
@overload
114113
def to_json(self, zarr_format: Literal[3]) -> NamedConfig[str, BaseConfig]: ...
115114

116-
def to_json(
117-
self, zarr_format: ZarrFormat
118-
) -> CodecJSON_V2[str] | NamedConfig[str, BaseConfig]:
115+
def to_json(self, zarr_format: ZarrFormat) -> CodecJSON_V2[str] | NamedConfig[str, BaseConfig]:
119116
if zarr_format == 2:
120117
return self.codec.get_config()
121118
elif zarr_format == 3:
@@ -126,7 +123,9 @@ def to_json(
126123

127124
@classmethod
128125
def _from_json_v2(cls, data: CodecJSON) -> Self:
129-
return cls(codec=resolve_numcodec(data)) # type: ignore[arg-type]
126+
raise NotADirectoryError(
127+
"This class does not support creating instances from JSON data for Zarr format 2."
128+
)
130129

131130
@classmethod
132131
def _from_json_v3(cls, data: CodecJSON) -> Self:
@@ -137,7 +136,6 @@ def _from_json_v3(cls, data: CodecJSON) -> Self:
137136
def compute_encoded_size(self, input_byte_length: int, chunk_spec: ArraySpec) -> int:
138137
raise NotImplementedError
139138

140-
141139
def to_array_array(self) -> NumcodecsArrayArrayCodec:
142140
"""
143141
Use the ``_codec`` attribute to create a NumcodecsArrayArrayCodec.

src/zarr/codecs/sharding.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
ChunkCoordsLike,
5656
NamedRequiredConfig,
5757
parse_enum,
58-
parse_named_configuration,
5958
parse_shapelike,
6059
product,
6160
)
@@ -453,8 +452,6 @@ def __setstate__(self, state: dict[str, Any]) -> None:
453452
@classmethod
454453
def from_dict(cls, data: dict[str, JSON]) -> Self:
455454
return cls.from_json(data, zarr_format=3)
456-
_, configuration_parsed = parse_named_configuration(data, "sharding_indexed")
457-
return cls(**configuration_parsed) # type: ignore[arg-type]
458455

459456
@classmethod
460457
def _from_json_v2(cls, data: CodecJSON) -> Self:
@@ -493,15 +490,6 @@ def codec_pipeline(self) -> CodecPipeline:
493490

494491
def to_dict(self) -> dict[str, JSON]:
495492
return self.to_json(zarr_format=3)
496-
return {
497-
"name": "sharding_indexed",
498-
"configuration": {
499-
"chunk_shape": self.chunk_shape,
500-
"codecs": tuple(s.to_dict() for s in self.codecs),
501-
"index_codecs": tuple(s.to_dict() for s in self.index_codecs),
502-
"index_location": self.index_location.value,
503-
},
504-
}
505493

506494
@overload
507495
def to_json(self, zarr_format: Literal[2]) -> ShardingJSON_V2: ...

0 commit comments

Comments
 (0)