7
7
from zarr .errors import MetadataError
8
8
from zarr .util import json_dumps , json_loads
9
9
10
+ from typing import Union , Any , List , Mapping as MappingType
11
+
10
12
ZARR_FORMAT = 2
11
13
12
14
13
- def parse_metadata (s ) :
15
+ def parse_metadata (s : Union [ MappingType , str ]) -> MappingType [ str , Any ] :
14
16
15
17
# Here we allow that a store may return an already-parsed metadata object,
16
18
# or a string of JSON that we will parse here. We allow for an already-parsed
@@ -28,7 +30,7 @@ def parse_metadata(s):
28
30
return meta
29
31
30
32
31
- def decode_array_metadata (s ) :
33
+ def decode_array_metadata (s : Union [ MappingType , str ]) -> MappingType [ str , Any ] :
32
34
meta = parse_metadata (s )
33
35
34
36
# check metadata format
@@ -56,7 +58,7 @@ def decode_array_metadata(s):
56
58
return meta
57
59
58
60
59
- def encode_array_metadata (meta ) :
61
+ def encode_array_metadata (meta : MappingType [ str , Any ]) -> bytes :
60
62
dtype = meta ['dtype' ]
61
63
sdshape = ()
62
64
if dtype .subdtype is not None :
@@ -74,27 +76,27 @@ def encode_array_metadata(meta):
74
76
return json_dumps (meta )
75
77
76
78
77
- def encode_dtype (d ) :
79
+ def encode_dtype (d : np . dtype ) -> str :
78
80
if d .fields is None :
79
81
return d .str
80
82
else :
81
83
return d .descr
82
84
83
85
84
- def _decode_dtype_descr (d ):
86
+ def _decode_dtype_descr (d ) -> List [ Any ] :
85
87
# need to convert list of lists to list of tuples
86
88
if isinstance (d , list ):
87
89
# recurse to handle nested structures
88
90
d = [(k [0 ], _decode_dtype_descr (k [1 ])) + tuple (k [2 :]) for k in d ]
89
91
return d
90
92
91
93
92
- def decode_dtype (d ):
94
+ def decode_dtype (d ) -> np . dtype :
93
95
d = _decode_dtype_descr (d )
94
96
return np .dtype (d )
95
97
96
98
97
- def decode_group_metadata (s ) :
99
+ def decode_group_metadata (s : Union [ MappingType , str ]) -> MappingType [ str , Any ] :
98
100
meta = parse_metadata (s )
99
101
100
102
# check metadata format version
@@ -108,7 +110,7 @@ def decode_group_metadata(s):
108
110
109
111
# N.B., keep `meta` parameter as a placeholder for future
110
112
# noinspection PyUnusedLocal
111
- def encode_group_metadata (meta = None ):
113
+ def encode_group_metadata (meta = None ) -> bytes :
112
114
meta = dict (
113
115
zarr_format = ZARR_FORMAT ,
114
116
)
@@ -161,7 +163,7 @@ def decode_fill_value(v, dtype):
161
163
return np .array (v , dtype = dtype )[()]
162
164
163
165
164
- def encode_fill_value (v , dtype ) :
166
+ def encode_fill_value (v : Any , dtype : np . dtype ) -> Any :
165
167
# early out
166
168
if v is None :
167
169
return v
0 commit comments