1
1
from __future__ import annotations
2
2
3
3
from abc import abstractmethod
4
- from typing import TYPE_CHECKING , Awaitable , Callable , Iterable , Optional , Tuple , TypeVar
4
+ from typing import (
5
+ TYPE_CHECKING ,
6
+ Awaitable ,
7
+ Callable ,
8
+ Iterable ,
9
+ Optional ,
10
+ Protocol ,
11
+ Tuple ,
12
+ TypeVar ,
13
+ runtime_checkable ,
14
+ )
5
15
6
16
import numpy as np
7
17
from zarr .abc .metadata import Metadata
8
18
9
19
from zarr .common import ArraySpec , concurrent_map
10
- from zarr .store import StorePath
11
20
12
21
13
22
if TYPE_CHECKING :
21
30
22
31
23
32
def noop_for_none (
33
+ << << << < HEAD :src / zarr / abc / codec .py
24
34
func : Callable [[Optional [T ], ArraySpec , RuntimeConfiguration ], Awaitable [U ]],
25
35
) -> Callable [[T , ArraySpec , RuntimeConfiguration ], Awaitable [U ]]:
36
+ == == == =
37
+ func : Callable [[T , ArraySpec , RuntimeConfiguration ], Awaitable [Optional [U ]]],
38
+ ) -> Callable [[Optional [T ], ArraySpec , RuntimeConfiguration ], Awaitable [Optional [U ]]]:
39
+ >> >> >> > 51 d3c921 (refactors CodecPipelines ):src / zarr / v3 / abc / codec .py
26
40
async def wrap (
27
41
chunk : Optional [T ], chunk_spec : ArraySpec , runtime_configuration : RuntimeConfiguration
28
- ) -> U :
42
+ ) -> Optional [ U ] :
29
43
if chunk is None :
30
44
return None
31
45
return await func (chunk , chunk_spec , runtime_configuration )
32
46
33
47
return wrap
34
48
35
49
50
+ @runtime_checkable
51
+ class ByteGetter (Protocol ):
52
+ async def get (
53
+ self , byte_range : Optional [Tuple [int , Optional [int ]]] = None
54
+ ) -> Optional [BytesLike ]:
55
+ ...
56
+
57
+
58
+ @runtime_checkable
59
+ class ByteSetter (Protocol ):
60
+ async def get (
61
+ self , byte_range : Optional [Tuple [int , Optional [int ]]] = None
62
+ ) -> Optional [BytesLike ]:
63
+ ...
64
+
65
+ async def set (self , value : BytesLike , byte_range : Optional [Tuple [int , int ]] = None ) -> None :
66
+ ...
67
+
68
+ async def delete (self ) -> None :
69
+ ...
70
+
71
+
36
72
class Codec (Metadata ):
37
73
is_fixed_size : bool
38
74
@@ -62,9 +98,9 @@ async def decode(
62
98
63
99
async def decode_batch (
64
100
self ,
65
- chunk_arrays_and_specs : Iterable [Tuple [np .ndarray , ArraySpec ]],
101
+ chunk_arrays_and_specs : Iterable [Tuple [Optional [ np .ndarray ] , ArraySpec ]],
66
102
runtime_configuration : RuntimeConfiguration ,
67
- ) -> Iterable [np .ndarray ]:
103
+ ) -> Iterable [Optional [ np .ndarray ] ]:
68
104
return await concurrent_map (
69
105
[
70
106
(chunk_array , chunk_spec , runtime_configuration )
@@ -110,9 +146,9 @@ async def decode(
110
146
111
147
async def decode_batch (
112
148
self ,
113
- chunk_bytes_and_specs : Iterable [Tuple [BytesLike , ArraySpec ]],
149
+ chunk_bytes_and_specs : Iterable [Tuple [Optional [ BytesLike ] , ArraySpec ]],
114
150
runtime_configuration : RuntimeConfiguration ,
115
- ) -> Iterable [np .ndarray ]:
151
+ ) -> Iterable [Optional [ np .ndarray ] ]:
116
152
return await concurrent_map (
117
153
[
118
154
(chunk_bytes , chunk_spec , runtime_configuration )
@@ -150,7 +186,7 @@ class ArrayBytesCodecPartialDecodeMixin:
150
186
@abstractmethod
151
187
async def decode_partial (
152
188
self ,
153
- store_path : StorePath ,
189
+ byte_getter : ByteGetter ,
154
190
selection : SliceSelection ,
155
191
chunk_spec : ArraySpec ,
156
192
runtime_configuration : RuntimeConfiguration ,
@@ -159,13 +195,13 @@ async def decode_partial(
159
195
160
196
async def decode_partial_batched (
161
197
self ,
162
- batch_info : Iterable [Tuple [StorePath , SliceSelection , ArraySpec ]],
198
+ batch_info : Iterable [Tuple [ByteGetter , SliceSelection , ArraySpec ]],
163
199
runtime_configuration : RuntimeConfiguration ,
164
200
) -> Iterable [Optional [np .ndarray ]]:
165
201
return await concurrent_map (
166
202
[
167
- (store_path , selection , chunk_spec , runtime_configuration )
168
- for store_path , selection , chunk_spec in batch_info
203
+ (byte_getter , selection , chunk_spec , runtime_configuration )
204
+ for byte_getter , selection , chunk_spec in batch_info
169
205
],
170
206
self .decode_partial ,
171
207
runtime_configuration .concurrency ,
@@ -176,7 +212,7 @@ class ArrayBytesCodecPartialEncodeMixin:
176
212
@abstractmethod
177
213
async def encode_partial (
178
214
self ,
179
- store_path : StorePath ,
215
+ byte_setter : ByteSetter ,
180
216
chunk_array : np .ndarray ,
181
217
selection : SliceSelection ,
182
218
chunk_spec : ArraySpec ,
@@ -186,13 +222,13 @@ async def encode_partial(
186
222
187
223
async def encode_partial_batched (
188
224
self ,
189
- batch_info : Iterable [Tuple [StorePath , np .ndarray , SliceSelection , ArraySpec ]],
225
+ batch_info : Iterable [Tuple [ByteSetter , np .ndarray , SliceSelection , ArraySpec ]],
190
226
runtime_configuration : RuntimeConfiguration ,
191
227
) -> None :
192
228
await concurrent_map (
193
229
[
194
- (store_path , chunk_array , selection , chunk_spec , runtime_configuration )
195
- for store_path , chunk_array , selection , chunk_spec in batch_info
230
+ (byte_setter , chunk_array , selection , chunk_spec , runtime_configuration )
231
+ for byte_setter , chunk_array , selection , chunk_spec in batch_info
196
232
],
197
233
self .encode_partial ,
198
234
runtime_configuration .concurrency ,
@@ -211,9 +247,9 @@ async def decode(
211
247
212
248
async def decode_batch (
213
249
self ,
214
- chunk_bytes_and_specs : Iterable [Tuple [BytesLike , ArraySpec ]],
250
+ chunk_bytes_and_specs : Iterable [Tuple [Optional [ BytesLike ] , ArraySpec ]],
215
251
runtime_configuration : RuntimeConfiguration ,
216
- ) -> Iterable [BytesLike ]:
252
+ ) -> Iterable [Optional [ BytesLike ] ]:
217
253
return await concurrent_map (
218
254
[
219
255
(chunk_bytes , chunk_spec , runtime_configuration )
0 commit comments