@@ -319,19 +319,18 @@ def flatten(
319319 children : dict [str , ArrayV2Metadata | ArrayV3Metadata | GroupMetadata ] = {}
320320 if isinstance (group , ArrayV2Metadata | ArrayV3Metadata ):
321321 children [key ] = group
322+ elif group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
323+ children [key ] = replace (
324+ group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
325+ )
326+ for name , val in group .consolidated_metadata .metadata .items ():
327+ full_key = f"{ key } /{ name } "
328+ if isinstance (val , GroupMetadata ):
329+ children .update (flatten (full_key , val ))
330+ else :
331+ children [full_key ] = val
322332 else :
323- if group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
324- children [key ] = replace (
325- group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
326- )
327- for name , val in group .consolidated_metadata .metadata .items ():
328- full_key = f"{ key } /{ name } "
329- if isinstance (val , GroupMetadata ):
330- children .update (flatten (full_key , val ))
331- else :
332- children [full_key ] = val
333- else :
334- children [key ] = replace (group , consolidated_metadata = None )
333+ children [key ] = replace (group , consolidated_metadata = None )
335334 return children
336335
337336 for k , v in self .metadata .items ():
@@ -1272,9 +1271,8 @@ async def require_array(
12721271 if exact :
12731272 if ds .dtype != dtype :
12741273 raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1275- else :
1276- if not np .can_cast (ds .dtype , dtype ):
1277- raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1274+ elif not np .can_cast (ds .dtype , dtype ):
1275+ raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
12781276 except KeyError :
12791277 ds = await self .create_array (name , shape = shape , dtype = dtype , ** kwargs )
12801278
@@ -3274,24 +3272,23 @@ async def create_hierarchy(
32743272 else :
32753273 # Any other exception is a real error
32763274 raise extant_node
3277- else :
3278- # this is a node that already exists, but a node with the same key was specified
3279- # in nodes_parsed.
3280- if isinstance (extant_node , GroupMetadata ):
3281- # a group already exists where we want to create a group
3282- if isinstance (proposed_node , ImplicitGroupMarker ):
3283- # we have proposed an implicit group, which is OK -- we will just skip
3284- # creating this particular metadata document
3285- redundant_implicit_groups .append (key )
3286- else :
3287- # we have proposed an explicit group, which is an error, given that a
3288- # group already exists.
3289- msg = f"A group exists in store { store !r} at path { key !r} ."
3290- raise ContainsGroupError (msg )
3291- elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3292- # we are trying to overwrite an existing array. this is an error.
3293- msg = f"An array exists in store { store !r} at path { key !r} ."
3294- raise ContainsArrayError (msg )
3275+ # this is a node that already exists, but a node with the same key was specified
3276+ # in nodes_parsed.
3277+ elif isinstance (extant_node , GroupMetadata ):
3278+ # a group already exists where we want to create a group
3279+ if isinstance (proposed_node , ImplicitGroupMarker ):
3280+ # we have proposed an implicit group, which is OK -- we will just skip
3281+ # creating this particular metadata document
3282+ redundant_implicit_groups .append (key )
3283+ else :
3284+ # we have proposed an explicit group, which is an error, given that a
3285+ # group already exists.
3286+ msg = f"A group exists in store { store !r} at path { key !r} ."
3287+ raise ContainsGroupError (msg )
3288+ elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3289+ # we are trying to overwrite an existing array. this is an error.
3290+ msg = f"An array exists in store { store !r} at path { key !r} ."
3291+ raise ContainsArrayError (msg )
32953292
32963293 nodes_explicit : dict [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ] = {}
32973294
@@ -3451,13 +3448,12 @@ def _parse_hierarchy_dict(
34513448 # If a component is not already in the output dict, add ImplicitGroupMetadata
34523449 if subpath not in out :
34533450 out [subpath ] = ImplicitGroupMarker (zarr_format = v .zarr_format )
3454- else :
3455- if not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3456- msg = (
3457- f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3458- "This is invalid. Only Zarr groups can contain other nodes."
3459- )
3460- raise ValueError (msg )
3451+ elif not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3452+ msg = (
3453+ f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3454+ "This is invalid. Only Zarr groups can contain other nodes."
3455+ )
3456+ raise ValueError (msg )
34613457 return out
34623458
34633459
@@ -3665,12 +3661,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
36653661 # return the array metadata.
36663662 if zarray_bytes is not None :
36673663 zmeta = json .loads (zarray_bytes .to_bytes ())
3664+ elif zgroup_bytes is None :
3665+ # neither .zarray or .zgroup were found results in KeyError
3666+ raise FileNotFoundError (path )
36683667 else :
3669- if zgroup_bytes is None :
3670- # neither .zarray or .zgroup were found results in KeyError
3671- raise FileNotFoundError (path )
3672- else :
3673- zmeta = json .loads (zgroup_bytes .to_bytes ())
3668+ zmeta = json .loads (zgroup_bytes .to_bytes ())
36743669
36753670 return _build_metadata_v2 (zmeta , zattrs )
36763671
0 commit comments