@@ -323,19 +323,18 @@ def flatten(
323323 children : dict [str , ArrayV2Metadata | ArrayV3Metadata | GroupMetadata ] = {}
324324 if isinstance (group , ArrayV2Metadata | ArrayV3Metadata ):
325325 children [key ] = group
326+ elif group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
327+ children [key ] = replace (
328+ group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
329+ )
330+ for name , val in group .consolidated_metadata .metadata .items ():
331+ full_key = f"{ key } /{ name } "
332+ if isinstance (val , GroupMetadata ):
333+ children .update (flatten (full_key , val ))
334+ else :
335+ children [full_key ] = val
326336 else :
327- if group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
328- children [key ] = replace (
329- group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
330- )
331- for name , val in group .consolidated_metadata .metadata .items ():
332- full_key = f"{ key } /{ name } "
333- if isinstance (val , GroupMetadata ):
334- children .update (flatten (full_key , val ))
335- else :
336- children [full_key ] = val
337- else :
338- children [key ] = replace (group , consolidated_metadata = None )
337+ children [key ] = replace (group , consolidated_metadata = None )
339338 return children
340339
341340 for k , v in self .metadata .items ():
@@ -1275,9 +1274,8 @@ async def require_array(
12751274 if exact :
12761275 if ds .dtype != dtype :
12771276 raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1278- else :
1279- if not np .can_cast (ds .dtype , dtype ):
1280- raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1277+ elif not np .can_cast (ds .dtype , dtype ):
1278+ raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
12811279 except KeyError :
12821280 ds = await self .create_array (name , shape = shape , dtype = dtype , ** kwargs )
12831281
@@ -3289,24 +3287,23 @@ async def create_hierarchy(
32893287 else :
32903288 # Any other exception is a real error
32913289 raise extant_node
3292- else :
3293- # this is a node that already exists, but a node with the same key was specified
3294- # in nodes_parsed.
3295- if isinstance (extant_node , GroupMetadata ):
3296- # a group already exists where we want to create a group
3297- if isinstance (proposed_node , ImplicitGroupMarker ):
3298- # we have proposed an implicit group, which is OK -- we will just skip
3299- # creating this particular metadata document
3300- redundant_implicit_groups .append (key )
3301- else :
3302- # we have proposed an explicit group, which is an error, given that a
3303- # group already exists.
3304- msg = f"A group exists in store { store !r} at path { key !r} ."
3305- raise ContainsGroupError (msg )
3306- elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3307- # we are trying to overwrite an existing array. this is an error.
3308- msg = f"An array exists in store { store !r} at path { key !r} ."
3309- raise ContainsArrayError (msg )
3290+ # this is a node that already exists, but a node with the same key was specified
3291+ # in nodes_parsed.
3292+ elif isinstance (extant_node , GroupMetadata ):
3293+ # a group already exists where we want to create a group
3294+ if isinstance (proposed_node , ImplicitGroupMarker ):
3295+ # we have proposed an implicit group, which is OK -- we will just skip
3296+ # creating this particular metadata document
3297+ redundant_implicit_groups .append (key )
3298+ else :
3299+ # we have proposed an explicit group, which is an error, given that a
3300+ # group already exists.
3301+ msg = f"A group exists in store { store !r} at path { key !r} ."
3302+ raise ContainsGroupError (msg )
3303+ elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3304+ # we are trying to overwrite an existing array. this is an error.
3305+ msg = f"An array exists in store { store !r} at path { key !r} ."
3306+ raise ContainsArrayError (msg )
33103307
33113308 nodes_explicit : dict [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ] = {}
33123309
@@ -3464,13 +3461,12 @@ def _parse_hierarchy_dict(
34643461 # If a component is not already in the output dict, add ImplicitGroupMetadata
34653462 if subpath not in out :
34663463 out [subpath ] = ImplicitGroupMarker (zarr_format = v .zarr_format )
3467- else :
3468- if not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3469- msg = (
3470- f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3471- "This is invalid. Only Zarr groups can contain other nodes."
3472- )
3473- raise ValueError (msg )
3464+ elif not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3465+ msg = (
3466+ f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3467+ "This is invalid. Only Zarr groups can contain other nodes."
3468+ )
3469+ raise ValueError (msg )
34743470 return out
34753471
34763472
@@ -3674,12 +3670,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
36743670 # return the array metadata.
36753671 if zarray_bytes is not None :
36763672 zmeta = json .loads (zarray_bytes .to_bytes ())
3673+ elif zgroup_bytes is None :
3674+ # neither .zarray or .zgroup were found results in KeyError
3675+ raise FileNotFoundError (path )
36773676 else :
3678- if zgroup_bytes is None :
3679- # neither .zarray or .zgroup were found results in KeyError
3680- raise FileNotFoundError (path )
3681- else :
3682- zmeta = json .loads (zgroup_bytes .to_bytes ())
3677+ zmeta = json .loads (zgroup_bytes .to_bytes ())
36833678
36843679 return _build_metadata_v2 (zmeta , zattrs )
36853680
0 commit comments