@@ -297,19 +297,18 @@ def flatten(
297297 children : dict [str , ArrayV2Metadata | ArrayV3Metadata | GroupMetadata ] = {}
298298 if isinstance (group , ArrayV2Metadata | ArrayV3Metadata ):
299299 children [key ] = group
300+ elif group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
301+ children [key ] = replace (
302+ group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
303+ )
304+ for name , val in group .consolidated_metadata .metadata .items ():
305+ full_key = f"{ key } /{ name } "
306+ if isinstance (val , GroupMetadata ):
307+ children .update (flatten (full_key , val ))
308+ else :
309+ children [full_key ] = val
300310 else :
301- if group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
302- children [key ] = replace (
303- group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
304- )
305- for name , val in group .consolidated_metadata .metadata .items ():
306- full_key = f"{ key } /{ name } "
307- if isinstance (val , GroupMetadata ):
308- children .update (flatten (full_key , val ))
309- else :
310- children [full_key ] = val
311- else :
312- children [key ] = replace (group , consolidated_metadata = None )
311+ children [key ] = replace (group , consolidated_metadata = None )
313312 return children
314313
315314 for k , v in self .metadata .items ():
@@ -1234,9 +1233,8 @@ async def require_array(
12341233 if exact :
12351234 if ds .dtype != dtype :
12361235 raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1237- else :
1238- if not np .can_cast (ds .dtype , dtype ):
1239- raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1236+ elif not np .can_cast (ds .dtype , dtype ):
1237+ raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
12401238 except KeyError :
12411239 ds = await self .create_array (name , shape = shape , dtype = dtype , ** kwargs )
12421240
@@ -3029,22 +3027,21 @@ async def create_hierarchy(
30293027 else :
30303028 # Any other exception is a real error
30313029 raise extant_node
3032- else :
3033- # this is a node that already exists, but a node with the same key was specified
3034- # in nodes_parsed.
3035- if isinstance (extant_node , GroupMetadata ):
3036- # a group already exists where we want to create a group
3037- if isinstance (proposed_node , ImplicitGroupMarker ):
3038- # we have proposed an implicit group, which is OK -- we will just skip
3039- # creating this particular metadata document
3040- redundant_implicit_groups .append (key )
3041- else :
3042- # we have proposed an explicit group, which is an error, given that a
3043- # group already exists.
3044- raise ContainsGroupError (store , key )
3045- elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3046- # we are trying to overwrite an existing array. this is an error.
3047- raise ContainsArrayError (store , key )
3030+ # this is a node that already exists, but a node with the same key was specified
3031+ # in nodes_parsed.
3032+ elif isinstance (extant_node , GroupMetadata ):
3033+ # a group already exists where we want to create a group
3034+ if isinstance (proposed_node , ImplicitGroupMarker ):
3035+ # we have proposed an implicit group, which is OK -- we will just skip
3036+ # creating this particular metadata document
3037+ redundant_implicit_groups .append (key )
3038+ else :
3039+ # we have proposed an explicit group, which is an error, given that a
3040+ # group already exists.
3041+ raise ContainsGroupError (store , key )
3042+ elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3043+ # we are trying to overwrite an existing array. this is an error.
3044+ raise ContainsArrayError (store , key )
30483045
30493046 nodes_explicit : dict [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ] = {}
30503047
@@ -3204,13 +3201,12 @@ def _parse_hierarchy_dict(
32043201 # If a component is not already in the output dict, add ImplicitGroupMetadata
32053202 if subpath not in out :
32063203 out [subpath ] = ImplicitGroupMarker (zarr_format = v .zarr_format )
3207- else :
3208- if not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3209- msg = (
3210- f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3211- "This is invalid. Only Zarr groups can contain other nodes."
3212- )
3213- raise ValueError (msg )
3204+ elif not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3205+ msg = (
3206+ f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3207+ "This is invalid. Only Zarr groups can contain other nodes."
3208+ )
3209+ raise ValueError (msg )
32143210 return out
32153211
32163212
@@ -3402,12 +3398,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
34023398 # return the array metadata.
34033399 if zarray_bytes is not None :
34043400 zmeta = json .loads (zarray_bytes .to_bytes ())
3401+ elif zgroup_bytes is None :
3402+ # neither .zarray or .zgroup were found results in KeyError
3403+ raise FileNotFoundError (path )
34053404 else :
3406- if zgroup_bytes is None :
3407- # neither .zarray or .zgroup were found results in KeyError
3408- raise FileNotFoundError (path )
3409- else :
3410- zmeta = json .loads (zgroup_bytes .to_bytes ())
3405+ zmeta = json .loads (zgroup_bytes .to_bytes ())
34113406
34123407 return _build_metadata_v2 (zmeta , zattrs )
34133408
0 commit comments