2222import warnings
2323from collections .abc import Callable , Iterable , Mapping , Sequence
2424from itertools import chain , repeat
25- from typing import TYPE_CHECKING , Final , TypeGuard
25+ from typing import TYPE_CHECKING , Final , TypeGuard , cast
2626
2727from zarr .core ._json import json_equal
2828from zarr .core .chunk_grids import full_span_chunk_size
@@ -137,20 +137,19 @@ def _read_codec(codec: JSON) -> JSON:
137137 """Read a stored codec: the inner chunk shape of a sharding codec is read as a chunk
138138 shape with no known axis lengths (see `_read_chunk_shape`), and so are those of the
139139 sharding codecs nested in its codecs."""
140- if not (isinstance (codec , Mapping ) and codec .get ("name" ) == "sharding_indexed" ):
141- return codec
142- configuration = codec .get ("configuration" )
143- if not isinstance (configuration , Mapping ):
144- return codec
145- upgraded = dict (configuration )
146- stored = configuration .get ("chunk_shape" )
147- if isinstance (stored , list ):
148- match _read_chunk_shape (stored , [None ] * len (stored )):
149- case chunk_shape , _:
150- upgraded ["chunk_shape" ] = list (chunk_shape )
151- if isinstance (codecs := configuration .get ("codecs" ), list ):
152- upgraded ["codecs" ] = [_read_codec (inner ) for inner in codecs ]
153- return {** codec , "configuration" : upgraded }
140+ match codec :
141+ case {"name" : "sharding_indexed" , "configuration" : Mapping () as configuration }:
142+ upgraded = dict (configuration )
143+ stored = configuration .get ("chunk_shape" )
144+ if isinstance (stored , list ) and (
145+ read := _read_chunk_shape (stored , [None ] * len (stored ))
146+ ):
147+ upgraded ["chunk_shape" ] = read [0 ]
148+ if isinstance (codecs := configuration .get ("codecs" ), list ):
149+ upgraded ["codecs" ] = [_read_codec (inner ) for inner in codecs ]
150+ # The mapping pattern does not narrow `codec` for mypy.
151+ return {** cast ("Mapping[str, JSON]" , codec ), "configuration" : upgraded }
152+ return codec
154153
155154
156155def _invalid_inner_chunk_sizes_v3 (doc : ArrayDocument ) -> tuple [ArrayDocument , str | None ] | None :
@@ -163,18 +162,16 @@ def _invalid_inner_chunk_sizes_v3(doc: ArrayDocument) -> tuple[ArrayDocument, st
163162 return {** doc , "codecs" : codecs }, None
164163
165164
166- def _inner_chunk_shape (doc : ArrayDocument ) -> list [int ]:
167- """The inner chunk shape of the sharding codec of a Zarr format 3 array document, if
168- it has one and that is a list of integers; else `[]`."""
169- codecs = doc .get ("codecs" )
170- if isinstance (codecs , list ):
171- for codec in codecs :
172- match codec :
173- case {
174- "name" : "sharding_indexed" ,
175- "configuration" : {"chunk_shape" : list () as inner },
176- }:
177- return inner if _is_int_list (inner ) else []
165+ def _inner_chunk_shape (doc : ArrayDocument ) -> list [int ] | None :
166+ """The inner chunk shape of the sharding codec of a Zarr format 3 array document:
167+ `[]` if it has none, `None` if its inner chunk sizes are not all integers of at
168+ least 1 (the unit of its outer chunk shape is then unknown)."""
169+ match doc .get ("codecs" ):
170+ case list () as codecs :
171+ for codec in codecs :
172+ match codec :
173+ case {"name" : "sharding_indexed" , "configuration" : {"chunk_shape" : inner }}:
174+ return inner if _is_int_list (inner ) and min (inner , default = 1 ) >= 1 else None
178175 return []
179176
180177
@@ -183,11 +180,13 @@ def _invalid_chunk_sizes_v3(doc: ArrayDocument) -> tuple[ArrayDocument, str | No
183180 shape = doc .get ("shape" )
184181 if not (isinstance (grid , Mapping ) and grid .get ("name" ) == "regular" and _is_int_list (shape )):
185182 return None
183+ if (units := _inner_chunk_shape (doc )) is None :
184+ return None
186185 configuration = grid .get ("configuration" )
187186 if not isinstance (configuration , Mapping ):
188187 return None
189188 stored = configuration .get ("chunk_shape" )
190- match _read_chunk_shape (stored , shape , _inner_chunk_shape ( doc ) ):
189+ match _read_chunk_shape (stored , shape , units ):
191190 case chunk_shape , reading if not json_equal (chunk_shape , stored ):
192191 upgraded = {** configuration , "chunk_shape" : chunk_shape }
193192 return {** doc , "chunk_grid" : {** grid , "configuration" : upgraded }}, reading
0 commit comments