2
2
3
3
import asyncio
4
4
from dataclasses import dataclass
5
- from functools import cached_property
6
- from typing import TYPE_CHECKING , Literal , Self , overload
5
+ from typing import TYPE_CHECKING , Literal , Self , cast , overload
7
6
8
7
import numpy as np
9
8
from numcodecs .compat import ensure_bytes , ensure_ndarray_like
15
14
CodecJSON ,
16
15
CodecJSON_V2 ,
17
16
)
18
- from zarr .registry import get_ndbuffer_class
17
+ from zarr .registry import _get_codec_v2 , _get_codec_v3 , get_ndbuffer_class
19
18
20
19
if TYPE_CHECKING :
21
- from collections .abc import Mapping
22
-
23
20
from zarr .abc .numcodec import Numcodec
24
21
from zarr .core .array_spec import ArraySpec
25
22
from zarr .core .buffer import Buffer , NDBuffer
@@ -115,21 +112,16 @@ def compute_encoded_size(self, _input_byte_length: int, _chunk_spec: ArraySpec)
115
112
116
113
@dataclass (frozen = True , kw_only = True )
117
114
class NumcodecsWrapper :
118
- codec_cls : type [Numcodec ]
119
- config : Mapping [str , object ]
120
-
121
- @cached_property
122
- def codec (self ) -> Numcodec :
123
- return self .codec_cls (** self .config )
115
+ codec : Numcodec
124
116
125
117
@overload
126
- def to_json (self , zarr_format : Literal [2 ]) -> CodecJSON_V2 [ str ] : ...
118
+ def to_json (self , zarr_format : Literal [2 ]) -> CodecJSON_V2 : ...
127
119
@overload
128
120
def to_json (self , zarr_format : Literal [3 ]) -> NamedConfig [str , BaseConfig ]: ...
129
121
130
- def to_json (self , zarr_format : ZarrFormat ) -> CodecJSON_V2 [ str ] | NamedConfig [str , BaseConfig ]:
122
+ def to_json (self , zarr_format : ZarrFormat ) -> CodecJSON_V2 | NamedConfig [str , BaseConfig ]:
131
123
if zarr_format == 2 :
132
- return self .config
124
+ return cast ( CodecJSON_V2 , self .codec . get_config ())
133
125
elif zarr_format == 3 :
134
126
config = self .codec .get_config ()
135
127
config_no_id = {k : v for k , v in config .items () if k != "id" }
@@ -138,11 +130,13 @@ def to_json(self, zarr_format: ZarrFormat) -> CodecJSON_V2[str] | NamedConfig[st
138
130
139
131
@classmethod
140
132
def _from_json_v2 (cls , data : CodecJSON ) -> Self :
141
- return cls (config = data )
133
+ codec = _get_codec_v2 (data )
134
+ return cls (codec = codec )
142
135
143
136
@classmethod
144
137
def _from_json_v3 (cls , data : CodecJSON ) -> Self :
145
- return cls (config = data .get ("configuration" , {}))
138
+ codec = _get_codec_v3 (data )
139
+ return cls (codec = codec )
146
140
147
141
def compute_encoded_size (self , input_byte_length : int , chunk_spec : ArraySpec ) -> int :
148
142
raise NotImplementedError
@@ -185,19 +179,19 @@ def to_array_array(self) -> NumcodecsArrayArrayCodec:
185
179
"""
186
180
Use the ``_codec`` attribute to create a NumcodecsArrayArrayCodec.
187
181
"""
188
- return NumcodecsArrayArrayCodec (cls = self .codec_cls , config = self . config )
182
+ return NumcodecsArrayArrayCodec (codec = self .codec )
189
183
190
184
def to_bytes_bytes (self ) -> NumcodecsBytesBytesCodec :
191
185
"""
192
186
Use the ``_codec`` attribute to create a NumcodecsBytesBytesCodec.
193
187
"""
194
- return NumcodecsBytesBytesCodec (cls = self .codec_cls , config = self . config )
188
+ return NumcodecsBytesBytesCodec (codec = self .codec )
195
189
196
190
def to_array_bytes (self ) -> NumcodecsArrayBytesCodec :
197
191
"""
198
192
Use the ``_codec`` attribute to create a NumcodecsArrayBytesCodec.
199
193
"""
200
- return NumcodecsArrayBytesCodec (codec_cls = self .codec_cls , config = self . config )
194
+ return NumcodecsArrayBytesCodec (codec = self .codec )
201
195
202
196
203
197
class NumcodecsBytesBytesCodec (NumcodecsWrapper , BytesBytesCodec ):
@@ -226,12 +220,12 @@ class NumcodecsArrayArrayCodec(NumcodecsWrapper, ArrayArrayCodec):
226
220
async def _decode_single (self , chunk_data : NDBuffer , chunk_spec : ArraySpec ) -> NDBuffer :
227
221
chunk_ndarray = chunk_data .as_ndarray_like ()
228
222
out = await asyncio .to_thread (self .codec .decode , chunk_ndarray )
229
- return chunk_spec .prototype .nd_buffer .from_ndarray_like (out .reshape (chunk_spec .shape )) # type: ignore[union-attr]
223
+ return chunk_spec .prototype .nd_buffer .from_ndarray_like (out .reshape (chunk_spec .shape ))
230
224
231
225
async def _encode_single (self , chunk_data : NDBuffer , chunk_spec : ArraySpec ) -> NDBuffer :
232
226
chunk_ndarray = chunk_data .as_ndarray_like ()
233
227
out = await asyncio .to_thread (self .codec .encode , chunk_ndarray )
234
- return chunk_spec .prototype .nd_buffer .from_ndarray_like (out ) # type: ignore[arg-type]
228
+ return chunk_spec .prototype .nd_buffer .from_ndarray_like (out )
235
229
236
230
237
231
@dataclass (kw_only = True , frozen = True )
0 commit comments