@@ -300,19 +300,18 @@ def flatten(
300300 children : dict [str , ArrayV2Metadata | ArrayV3Metadata | GroupMetadata ] = {}
301301 if isinstance (group , ArrayV2Metadata | ArrayV3Metadata ):
302302 children [key ] = group
303+ elif group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
304+ children [key ] = replace (
305+ group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
306+ )
307+ for name , val in group .consolidated_metadata .metadata .items ():
308+ full_key = f"{ key } /{ name } "
309+ if isinstance (val , GroupMetadata ):
310+ children .update (flatten (full_key , val ))
311+ else :
312+ children [full_key ] = val
303313 else :
304- if group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
305- children [key ] = replace (
306- group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
307- )
308- for name , val in group .consolidated_metadata .metadata .items ():
309- full_key = f"{ key } /{ name } "
310- if isinstance (val , GroupMetadata ):
311- children .update (flatten (full_key , val ))
312- else :
313- children [full_key ] = val
314- else :
315- children [key ] = replace (group , consolidated_metadata = None )
314+ children [key ] = replace (group , consolidated_metadata = None )
316315 return children
317316
318317 for k , v in self .metadata .items ():
@@ -1243,9 +1242,8 @@ async def require_array(
12431242 if exact :
12441243 if ds .dtype != dtype :
12451244 raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1246- else :
1247- if not np .can_cast (ds .dtype , dtype ):
1248- raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1245+ elif not np .can_cast (ds .dtype , dtype ):
1246+ raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
12491247 except KeyError :
12501248 ds = await self .create_array (name , shape = shape , dtype = dtype , ** kwargs )
12511249
@@ -3077,22 +3075,21 @@ async def create_hierarchy(
30773075 else :
30783076 # Any other exception is a real error
30793077 raise extant_node
3080- else :
3081- # this is a node that already exists, but a node with the same key was specified
3082- # in nodes_parsed.
3083- if 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 )
3078+ # this is a node that already exists, but a node with the same key was specified
3079+ # in nodes_parsed.
3080+ elif isinstance (extant_node , GroupMetadata ):
3081+ # a group already exists where we want to create a group
3082+ if isinstance (proposed_node , ImplicitGroupMarker ):
3083+ # we have proposed an implicit group, which is OK -- we will just skip
3084+ # creating this particular metadata document
3085+ redundant_implicit_groups .append (key )
3086+ else :
3087+ # we have proposed an explicit group, which is an error, given that a
3088+ # group already exists.
3089+ raise ContainsGroupError (store , key )
3090+ elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3091+ # we are trying to overwrite an existing array. this is an error.
3092+ raise ContainsArrayError (store , key )
30963093
30973094 nodes_explicit : dict [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ] = {}
30983095
@@ -3252,13 +3249,12 @@ def _parse_hierarchy_dict(
32523249 # If a component is not already in the output dict, add ImplicitGroupMetadata
32533250 if subpath not in out :
32543251 out [subpath ] = ImplicitGroupMarker (zarr_format = v .zarr_format )
3255- else :
3256- if not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3257- msg = (
3258- f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3259- "This is invalid. Only Zarr groups can contain other nodes."
3260- )
3261- raise ValueError (msg )
3252+ elif not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3253+ msg = (
3254+ f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3255+ "This is invalid. Only Zarr groups can contain other nodes."
3256+ )
3257+ raise ValueError (msg )
32623258 return out
32633259
32643260
@@ -3466,12 +3462,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
34663462 # return the array metadata.
34673463 if zarray_bytes is not None :
34683464 zmeta = json .loads (zarray_bytes .to_bytes ())
3465+ elif zgroup_bytes is None :
3466+ # neither .zarray or .zgroup were found results in KeyError
3467+ raise FileNotFoundError (path )
34693468 else :
3470- if zgroup_bytes is None :
3471- # neither .zarray or .zgroup were found results in KeyError
3472- raise FileNotFoundError (path )
3473- else :
3474- zmeta = json .loads (zgroup_bytes .to_bytes ())
3469+ zmeta = json .loads (zgroup_bytes .to_bytes ())
34753470
34763471 return _build_metadata_v2 (zmeta , zattrs )
34773472
0 commit comments