@@ -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 ():
@@ -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
@@ -3277,24 +3275,23 @@ async def create_hierarchy(
32773275 else :
32783276 # Any other exception is a real error
32793277 raise extant_node
3280- else :
3281- # this is a node that already exists, but a node with the same key was specified
3282- # in nodes_parsed.
3283- if isinstance (extant_node , GroupMetadata ):
3284- # a group already exists where we want to create a group
3285- if isinstance (proposed_node , ImplicitGroupMarker ):
3286- # we have proposed an implicit group, which is OK -- we will just skip
3287- # creating this particular metadata document
3288- redundant_implicit_groups .append (key )
3289- else :
3290- # we have proposed an explicit group, which is an error, given that a
3291- # group already exists.
3292- msg = f"A group exists in store { store !r} at path { key !r} ."
3293- raise ContainsGroupError (msg )
3294- elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3295- # we are trying to overwrite an existing array. this is an error.
3296- msg = f"An array exists in store { store !r} at path { key !r} ."
3297- raise ContainsArrayError (msg )
3278+ # this is a node that already exists, but a node with the same key was specified
3279+ # in nodes_parsed.
3280+ elif 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 )
32983295
32993296 nodes_explicit : dict [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ] = {}
33003297
@@ -3454,13 +3451,12 @@ def _parse_hierarchy_dict(
34543451 # If a component is not already in the output dict, add ImplicitGroupMetadata
34553452 if subpath not in out :
34563453 out [subpath ] = ImplicitGroupMarker (zarr_format = v .zarr_format )
3457- else :
3458- if not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3459- msg = (
3460- f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3461- "This is invalid. Only Zarr groups can contain other nodes."
3462- )
3463- raise ValueError (msg )
3454+ elif not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3455+ msg = (
3456+ f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3457+ "This is invalid. Only Zarr groups can contain other nodes."
3458+ )
3459+ raise ValueError (msg )
34643460 return out
34653461
34663462
@@ -3668,12 +3664,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
36683664 # return the array metadata.
36693665 if zarray_bytes is not None :
36703666 zmeta = json .loads (zarray_bytes .to_bytes ())
3667+ elif zgroup_bytes is None :
3668+ # neither .zarray or .zgroup were found results in KeyError
3669+ raise FileNotFoundError (path )
36713670 else :
3672- if zgroup_bytes is None :
3673- # neither .zarray or .zgroup were found results in KeyError
3674- raise FileNotFoundError (path )
3675- else :
3676- zmeta = json .loads (zgroup_bytes .to_bytes ())
3671+ zmeta = json .loads (zgroup_bytes .to_bytes ())
36773672
36783673 return _build_metadata_v2 (zmeta , zattrs )
36793674
0 commit comments