@@ -315,19 +315,18 @@ def flatten(
315
315
children : dict [str , ArrayV2Metadata | ArrayV3Metadata | GroupMetadata ] = {}
316
316
if isinstance (group , ArrayV2Metadata | ArrayV3Metadata ):
317
317
children [key ] = group
318
+ elif group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
319
+ children [key ] = replace (
320
+ group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
321
+ )
322
+ for name , val in group .consolidated_metadata .metadata .items ():
323
+ full_key = f"{ key } /{ name } "
324
+ if isinstance (val , GroupMetadata ):
325
+ children .update (flatten (full_key , val ))
326
+ else :
327
+ children [full_key ] = val
318
328
else :
319
- if group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
320
- children [key ] = replace (
321
- group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
322
- )
323
- for name , val in group .consolidated_metadata .metadata .items ():
324
- full_key = f"{ key } /{ name } "
325
- if isinstance (val , GroupMetadata ):
326
- children .update (flatten (full_key , val ))
327
- else :
328
- children [full_key ] = val
329
- else :
330
- children [key ] = replace (group , consolidated_metadata = None )
329
+ children [key ] = replace (group , consolidated_metadata = None )
331
330
return children
332
331
333
332
for k , v in self .metadata .items ():
@@ -1272,9 +1271,8 @@ async def require_array(
1272
1271
if exact :
1273
1272
if ds .dtype != dtype :
1274
1273
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 } )" )
1278
1276
except KeyError :
1279
1277
ds = await self .create_array (name , shape = shape , dtype = dtype , ** kwargs )
1280
1278
@@ -3119,22 +3117,21 @@ async def create_hierarchy(
3119
3117
else :
3120
3118
# Any other exception is a real error
3121
3119
raise extant_node
3122
- else :
3123
- # this is a node that already exists, but a node with the same key was specified
3124
- # in nodes_parsed.
3125
- if isinstance (extant_node , GroupMetadata ):
3126
- # a group already exists where we want to create a group
3127
- if isinstance (proposed_node , ImplicitGroupMarker ):
3128
- # we have proposed an implicit group, which is OK -- we will just skip
3129
- # creating this particular metadata document
3130
- redundant_implicit_groups .append (key )
3131
- else :
3132
- # we have proposed an explicit group, which is an error, given that a
3133
- # group already exists.
3134
- raise ContainsGroupError (store , key )
3135
- elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3136
- # we are trying to overwrite an existing array. this is an error.
3137
- raise ContainsArrayError (store , key )
3120
+ # this is a node that already exists, but a node with the same key was specified
3121
+ # in nodes_parsed.
3122
+ elif isinstance (extant_node , GroupMetadata ):
3123
+ # a group already exists where we want to create a group
3124
+ if isinstance (proposed_node , ImplicitGroupMarker ):
3125
+ # we have proposed an implicit group, which is OK -- we will just skip
3126
+ # creating this particular metadata document
3127
+ redundant_implicit_groups .append (key )
3128
+ else :
3129
+ # we have proposed an explicit group, which is an error, given that a
3130
+ # group already exists.
3131
+ raise ContainsGroupError (store , key )
3132
+ elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3133
+ # we are trying to overwrite an existing array. this is an error.
3134
+ raise ContainsArrayError (store , key )
3138
3135
3139
3136
nodes_explicit : dict [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ] = {}
3140
3137
@@ -3294,13 +3291,12 @@ def _parse_hierarchy_dict(
3294
3291
# If a component is not already in the output dict, add ImplicitGroupMetadata
3295
3292
if subpath not in out :
3296
3293
out [subpath ] = ImplicitGroupMarker (zarr_format = v .zarr_format )
3297
- else :
3298
- if not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3299
- msg = (
3300
- f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3301
- "This is invalid. Only Zarr groups can contain other nodes."
3302
- )
3303
- raise ValueError (msg )
3294
+ elif not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3295
+ msg = (
3296
+ f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3297
+ "This is invalid. Only Zarr groups can contain other nodes."
3298
+ )
3299
+ raise ValueError (msg )
3304
3300
return out
3305
3301
3306
3302
@@ -3508,12 +3504,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
3508
3504
# return the array metadata.
3509
3505
if zarray_bytes is not None :
3510
3506
zmeta = json .loads (zarray_bytes .to_bytes ())
3507
+ elif zgroup_bytes is None :
3508
+ # neither .zarray or .zgroup were found results in KeyError
3509
+ raise FileNotFoundError (path )
3511
3510
else :
3512
- if zgroup_bytes is None :
3513
- # neither .zarray or .zgroup were found results in KeyError
3514
- raise FileNotFoundError (path )
3515
- else :
3516
- zmeta = json .loads (zgroup_bytes .to_bytes ())
3511
+ zmeta = json .loads (zgroup_bytes .to_bytes ())
3517
3512
3518
3513
return _build_metadata_v2 (zmeta , zattrs )
3519
3514
0 commit comments