Skip to content

Commit 781c70a

Browse files
Apply ruff/Pylint rule PLR5501
PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation
1 parent c4b5e9f commit 781c70a

File tree

3 files changed

+45
-52
lines changed

3 files changed

+45
-52
lines changed

src/zarr/core/codec_pipeline.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,12 @@ async def _read_key(
403403
):
404404
if chunk_array is None:
405405
chunk_array_batch.append(None) # type: ignore[unreachable]
406+
elif not chunk_spec.config.write_empty_chunks and chunk_array.all_equal(
407+
fill_value_or_default(chunk_spec)
408+
):
409+
chunk_array_batch.append(None)
406410
else:
407-
if not chunk_spec.config.write_empty_chunks and chunk_array.all_equal(
408-
fill_value_or_default(chunk_spec)
409-
):
410-
chunk_array_batch.append(None)
411-
else:
412-
chunk_array_batch.append(chunk_array)
411+
chunk_array_batch.append(chunk_array)
413412

414413
chunk_bytes_batch = await self.encode_batch(
415414
[

src/zarr/core/group.py

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -297,19 +297,18 @@ def flatten(
297297
children: dict[str, ArrayV2Metadata | ArrayV3Metadata | GroupMetadata] = {}
298298
if isinstance(group, ArrayV2Metadata | ArrayV3Metadata):
299299
children[key] = group
300+
elif group.consolidated_metadata and group.consolidated_metadata.metadata is not None:
301+
children[key] = replace(
302+
group, consolidated_metadata=ConsolidatedMetadata(metadata={})
303+
)
304+
for name, val in group.consolidated_metadata.metadata.items():
305+
full_key = f"{key}/{name}"
306+
if isinstance(val, GroupMetadata):
307+
children.update(flatten(full_key, val))
308+
else:
309+
children[full_key] = val
300310
else:
301-
if group.consolidated_metadata and group.consolidated_metadata.metadata is not None:
302-
children[key] = replace(
303-
group, consolidated_metadata=ConsolidatedMetadata(metadata={})
304-
)
305-
for name, val in group.consolidated_metadata.metadata.items():
306-
full_key = f"{key}/{name}"
307-
if isinstance(val, GroupMetadata):
308-
children.update(flatten(full_key, val))
309-
else:
310-
children[full_key] = val
311-
else:
312-
children[key] = replace(group, consolidated_metadata=None)
311+
children[key] = replace(group, consolidated_metadata=None)
313312
return children
314313

315314
for k, v in self.metadata.items():
@@ -1234,9 +1233,8 @@ async def require_array(
12341233
if exact:
12351234
if ds.dtype != dtype:
12361235
raise TypeError(f"Incompatible dtype ({ds.dtype} vs {dtype})")
1237-
else:
1238-
if not np.can_cast(ds.dtype, dtype):
1239-
raise TypeError(f"Incompatible dtype ({ds.dtype} vs {dtype})")
1236+
elif not np.can_cast(ds.dtype, dtype):
1237+
raise TypeError(f"Incompatible dtype ({ds.dtype} vs {dtype})")
12401238
except KeyError:
12411239
ds = await self.create_array(name, shape=shape, dtype=dtype, **kwargs)
12421240

@@ -3027,22 +3025,21 @@ async def create_hierarchy(
30273025
else:
30283026
# Any other exception is a real error
30293027
raise extant_node
3030-
else:
3031-
# this is a node that already exists, but a node with the same key was specified
3032-
# in nodes_parsed.
3033-
if isinstance(extant_node, GroupMetadata):
3034-
# a group already exists where we want to create a group
3035-
if isinstance(proposed_node, ImplicitGroupMarker):
3036-
# we have proposed an implicit group, which is OK -- we will just skip
3037-
# creating this particular metadata document
3038-
redundant_implicit_groups.append(key)
3039-
else:
3040-
# we have proposed an explicit group, which is an error, given that a
3041-
# group already exists.
3042-
raise ContainsGroupError(store, key)
3043-
elif isinstance(extant_node, ArrayV2Metadata | ArrayV3Metadata):
3044-
# we are trying to overwrite an existing array. this is an error.
3045-
raise ContainsArrayError(store, key)
3028+
# this is a node that already exists, but a node with the same key was specified
3029+
# in nodes_parsed.
3030+
elif isinstance(extant_node, GroupMetadata):
3031+
# a group already exists where we want to create a group
3032+
if isinstance(proposed_node, ImplicitGroupMarker):
3033+
# we have proposed an implicit group, which is OK -- we will just skip
3034+
# creating this particular metadata document
3035+
redundant_implicit_groups.append(key)
3036+
else:
3037+
# we have proposed an explicit group, which is an error, given that a
3038+
# group already exists.
3039+
raise ContainsGroupError(store, key)
3040+
elif isinstance(extant_node, ArrayV2Metadata | ArrayV3Metadata):
3041+
# we are trying to overwrite an existing array. this is an error.
3042+
raise ContainsArrayError(store, key)
30463043

30473044
nodes_explicit: dict[str, GroupMetadata | ArrayV2Metadata | ArrayV3Metadata] = {}
30483045

@@ -3202,13 +3199,12 @@ def _parse_hierarchy_dict(
32023199
# If a component is not already in the output dict, add ImplicitGroupMetadata
32033200
if subpath not in out:
32043201
out[subpath] = ImplicitGroupMarker(zarr_format=v.zarr_format)
3205-
else:
3206-
if not isinstance(out[subpath], GroupMetadata | ImplicitGroupMarker):
3207-
msg = (
3208-
f"The node at {subpath} contains other nodes, but it is not a Zarr group. "
3209-
"This is invalid. Only Zarr groups can contain other nodes."
3210-
)
3211-
raise ValueError(msg)
3202+
elif not isinstance(out[subpath], GroupMetadata | ImplicitGroupMarker):
3203+
msg = (
3204+
f"The node at {subpath} contains other nodes, but it is not a Zarr group. "
3205+
"This is invalid. Only Zarr groups can contain other nodes."
3206+
)
3207+
raise ValueError(msg)
32123208
return out
32133209

32143210

@@ -3400,12 +3396,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
34003396
# return the array metadata.
34013397
if zarray_bytes is not None:
34023398
zmeta = json.loads(zarray_bytes.to_bytes())
3399+
elif zgroup_bytes is None:
3400+
# neither .zarray or .zgroup were found results in KeyError
3401+
raise FileNotFoundError(path)
34033402
else:
3404-
if zgroup_bytes is None:
3405-
# neither .zarray or .zgroup were found results in KeyError
3406-
raise FileNotFoundError(path)
3407-
else:
3408-
zmeta = json.loads(zgroup_bytes.to_bytes())
3403+
zmeta = json.loads(zgroup_bytes.to_bytes())
34093404

34103405
return _build_metadata_v2(zmeta, zattrs)
34113406

src/zarr/core/metadata/v3.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,8 @@ def parse_fill_value(
573573
and np.isclose(np.imag(fill_value), np.imag(casted_value), equal_nan=True)
574574
):
575575
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {data_type}")
576-
else:
577-
if fill_value != casted_value:
578-
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {data_type}")
576+
elif fill_value != casted_value:
577+
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {data_type}")
579578

580579
return casted_value
581580

0 commit comments

Comments
 (0)