Skip to content

Commit 25acc61

Browse files
Apply ruff/Pylint rule PLR5501
PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation
1 parent e989dc9 commit 25acc61

File tree

2 files changed

+43
-49
lines changed

2 files changed

+43
-49
lines changed

src/zarr/core/codec_pipeline.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,12 @@ async def _read_key(
405405
):
406406
if chunk_array is None:
407407
chunk_array_batch.append(None) # type: ignore[unreachable]
408+
elif not chunk_spec.config.write_empty_chunks and chunk_array.all_equal(
409+
fill_value_or_default(chunk_spec)
410+
):
411+
chunk_array_batch.append(None)
408412
else:
409-
if not chunk_spec.config.write_empty_chunks and chunk_array.all_equal(
410-
fill_value_or_default(chunk_spec)
411-
):
412-
chunk_array_batch.append(None)
413-
else:
414-
chunk_array_batch.append(chunk_array)
413+
chunk_array_batch.append(chunk_array)
415414

416415
chunk_bytes_batch = await self.encode_batch(
417416
[

src/zarr/core/group.py

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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():
@@ -1257,9 +1256,8 @@ async def require_array(
12571256
if exact:
12581257
if ds.dtype != dtype:
12591258
raise TypeError(f"Incompatible dtype ({ds.dtype} vs {dtype})")
1260-
else:
1261-
if not np.can_cast(ds.dtype, dtype):
1262-
raise TypeError(f"Incompatible dtype ({ds.dtype} vs {dtype})")
1259+
elif not np.can_cast(ds.dtype, dtype):
1260+
raise TypeError(f"Incompatible dtype ({ds.dtype} vs {dtype})")
12631261
except KeyError:
12641262
ds = await self.create_array(name, shape=shape, dtype=dtype, **kwargs)
12651263

@@ -3103,22 +3101,21 @@ async def create_hierarchy(
31033101
else:
31043102
# Any other exception is a real error
31053103
raise extant_node
3106-
else:
3107-
# this is a node that already exists, but a node with the same key was specified
3108-
# in nodes_parsed.
3109-
if isinstance(extant_node, GroupMetadata):
3110-
# a group already exists where we want to create a group
3111-
if isinstance(proposed_node, ImplicitGroupMarker):
3112-
# we have proposed an implicit group, which is OK -- we will just skip
3113-
# creating this particular metadata document
3114-
redundant_implicit_groups.append(key)
3115-
else:
3116-
# we have proposed an explicit group, which is an error, given that a
3117-
# group already exists.
3118-
raise ContainsGroupError(store, key)
3119-
elif isinstance(extant_node, ArrayV2Metadata | ArrayV3Metadata):
3120-
# we are trying to overwrite an existing array. this is an error.
3121-
raise ContainsArrayError(store, key)
3104+
# this is a node that already exists, but a node with the same key was specified
3105+
# in nodes_parsed.
3106+
elif isinstance(extant_node, GroupMetadata):
3107+
# a group already exists where we want to create a group
3108+
if isinstance(proposed_node, ImplicitGroupMarker):
3109+
# we have proposed an implicit group, which is OK -- we will just skip
3110+
# creating this particular metadata document
3111+
redundant_implicit_groups.append(key)
3112+
else:
3113+
# we have proposed an explicit group, which is an error, given that a
3114+
# group already exists.
3115+
raise ContainsGroupError(store, key)
3116+
elif isinstance(extant_node, ArrayV2Metadata | ArrayV3Metadata):
3117+
# we are trying to overwrite an existing array. this is an error.
3118+
raise ContainsArrayError(store, key)
31223119

31233120
nodes_explicit: dict[str, GroupMetadata | ArrayV2Metadata | ArrayV3Metadata] = {}
31243121

@@ -3278,13 +3275,12 @@ def _parse_hierarchy_dict(
32783275
# If a component is not already in the output dict, add ImplicitGroupMetadata
32793276
if subpath not in out:
32803277
out[subpath] = ImplicitGroupMarker(zarr_format=v.zarr_format)
3281-
else:
3282-
if not isinstance(out[subpath], GroupMetadata | ImplicitGroupMarker):
3283-
msg = (
3284-
f"The node at {subpath} contains other nodes, but it is not a Zarr group. "
3285-
"This is invalid. Only Zarr groups can contain other nodes."
3286-
)
3287-
raise ValueError(msg)
3278+
elif not isinstance(out[subpath], GroupMetadata | ImplicitGroupMarker):
3279+
msg = (
3280+
f"The node at {subpath} contains other nodes, but it is not a Zarr group. "
3281+
"This is invalid. Only Zarr groups can contain other nodes."
3282+
)
3283+
raise ValueError(msg)
32883284
return out
32893285

32903286

@@ -3492,12 +3488,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
34923488
# return the array metadata.
34933489
if zarray_bytes is not None:
34943490
zmeta = json.loads(zarray_bytes.to_bytes())
3491+
elif zgroup_bytes is None:
3492+
# neither .zarray or .zgroup were found results in KeyError
3493+
raise FileNotFoundError(path)
34953494
else:
3496-
if zgroup_bytes is None:
3497-
# neither .zarray or .zgroup were found results in KeyError
3498-
raise FileNotFoundError(path)
3499-
else:
3500-
zmeta = json.loads(zgroup_bytes.to_bytes())
3495+
zmeta = json.loads(zgroup_bytes.to_bytes())
35013496

35023497
return _build_metadata_v2(zmeta, zattrs)
35033498

0 commit comments

Comments
 (0)