@@ -298,19 +298,18 @@ def flatten(
298298 children : dict [str , ArrayV2Metadata | ArrayV3Metadata | GroupMetadata ] = {}
299299 if isinstance (group , ArrayV2Metadata | ArrayV3Metadata ):
300300 children [key ] = group
301+ elif 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
301311 else :
302- if group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
303- children [key ] = replace (
304- group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
305- )
306- for name , val in group .consolidated_metadata .metadata .items ():
307- full_key = f"{ key } /{ name } "
308- if isinstance (val , GroupMetadata ):
309- children .update (flatten (full_key , val ))
310- else :
311- children [full_key ] = val
312- else :
313- children [key ] = replace (group , consolidated_metadata = None )
312+ children [key ] = replace (group , consolidated_metadata = None )
314313 return children
315314
316315 for k , v in self .metadata .items ():
@@ -1246,9 +1245,8 @@ async def require_array(
12461245 if exact :
12471246 if ds .dtype != dtype :
12481247 raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1249- else :
1250- if not np .can_cast (ds .dtype , dtype ):
1251- raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1248+ elif not np .can_cast (ds .dtype , dtype ):
1249+ raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
12521250 except KeyError :
12531251 ds = await self .create_array (name , shape = shape , dtype = dtype , ** kwargs )
12541252
@@ -3080,22 +3078,21 @@ async def create_hierarchy(
30803078 else :
30813079 # Any other exception is a real error
30823080 raise extant_node
3083- else :
3084- # this is a node that already exists, but a node with the same key was specified
3085- # in nodes_parsed.
3086- if isinstance (extant_node , GroupMetadata ):
3087- # a group already exists where we want to create a group
3088- if isinstance (proposed_node , ImplicitGroupMarker ):
3089- # we have proposed an implicit group, which is OK -- we will just skip
3090- # creating this particular metadata document
3091- redundant_implicit_groups .append (key )
3092- else :
3093- # we have proposed an explicit group, which is an error, given that a
3094- # group already exists.
3095- raise ContainsGroupError (store , key )
3096- elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3097- # we are trying to overwrite an existing array. this is an error.
3098- raise ContainsArrayError (store , key )
3081+ # this is a node that already exists, but a node with the same key was specified
3082+ # in nodes_parsed.
3083+ elif isinstance (extant_node , GroupMetadata ):
3084+ # a group already exists where we want to create a group
3085+ if isinstance (proposed_node , ImplicitGroupMarker ):
3086+ # we have proposed an implicit group, which is OK -- we will just skip
3087+ # creating this particular metadata document
3088+ redundant_implicit_groups .append (key )
3089+ else :
3090+ # we have proposed an explicit group, which is an error, given that a
3091+ # group already exists.
3092+ raise ContainsGroupError (store , key )
3093+ elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3094+ # we are trying to overwrite an existing array. this is an error.
3095+ raise ContainsArrayError (store , key )
30993096
31003097 nodes_explicit : dict [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ] = {}
31013098
@@ -3255,13 +3252,12 @@ def _parse_hierarchy_dict(
32553252 # If a component is not already in the output dict, add ImplicitGroupMetadata
32563253 if subpath not in out :
32573254 out [subpath ] = ImplicitGroupMarker (zarr_format = v .zarr_format )
3258- else :
3259- if not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3260- msg = (
3261- f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3262- "This is invalid. Only Zarr groups can contain other nodes."
3263- )
3264- raise ValueError (msg )
3255+ elif not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3256+ msg = (
3257+ f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3258+ "This is invalid. Only Zarr groups can contain other nodes."
3259+ )
3260+ raise ValueError (msg )
32653261 return out
32663262
32673263
@@ -3469,12 +3465,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
34693465 # return the array metadata.
34703466 if zarray_bytes is not None :
34713467 zmeta = json .loads (zarray_bytes .to_bytes ())
3468+ elif zgroup_bytes is None :
3469+ # neither .zarray or .zgroup were found results in KeyError
3470+ raise FileNotFoundError (path )
34723471 else :
3473- if zgroup_bytes is None :
3474- # neither .zarray or .zgroup were found results in KeyError
3475- raise FileNotFoundError (path )
3476- else :
3477- zmeta = json .loads (zgroup_bytes .to_bytes ())
3472+ zmeta = json .loads (zgroup_bytes .to_bytes ())
34783473
34793474 return _build_metadata_v2 (zmeta , zattrs )
34803475
0 commit comments