Skip to content

Commit 8d12eff

Browse files
Apply ruff/Pylint rule PLR5501
PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation
1 parent ab0d209 commit 8d12eff

File tree

3 files changed

+60
-71
lines changed

3 files changed

+60
-71
lines changed

src/zarr/core/codec_pipeline.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,12 @@ async def _read_key(
412412
):
413413
if chunk_array is None:
414414
chunk_array_batch.append(None) # type: ignore[unreachable]
415+
elif not chunk_spec.config.write_empty_chunks and chunk_array.all_equal(
416+
fill_value_or_default(chunk_spec)
417+
):
418+
chunk_array_batch.append(None)
415419
else:
416-
if not chunk_spec.config.write_empty_chunks and chunk_array.all_equal(
417-
fill_value_or_default(chunk_spec)
418-
):
419-
chunk_array_batch.append(None)
420-
else:
421-
chunk_array_batch.append(chunk_array)
420+
chunk_array_batch.append(chunk_array)
422421

423422
chunk_bytes_batch = await self.encode_batch(
424423
[

src/zarr/core/group.py

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -324,19 +324,18 @@ def flatten(
324324
children: dict[str, ArrayV2Metadata | ArrayV3Metadata | GroupMetadata] = {}
325325
if isinstance(group, ArrayV2Metadata | ArrayV3Metadata):
326326
children[key] = group
327+
elif group.consolidated_metadata and group.consolidated_metadata.metadata is not None:
328+
children[key] = replace(
329+
group, consolidated_metadata=ConsolidatedMetadata(metadata={})
330+
)
331+
for name, val in group.consolidated_metadata.metadata.items():
332+
full_key = f"{key}/{name}"
333+
if isinstance(val, GroupMetadata):
334+
children.update(flatten(full_key, val))
335+
else:
336+
children[full_key] = val
327337
else:
328-
if group.consolidated_metadata and group.consolidated_metadata.metadata is not None:
329-
children[key] = replace(
330-
group, consolidated_metadata=ConsolidatedMetadata(metadata={})
331-
)
332-
for name, val in group.consolidated_metadata.metadata.items():
333-
full_key = f"{key}/{name}"
334-
if isinstance(val, GroupMetadata):
335-
children.update(flatten(full_key, val))
336-
else:
337-
children[full_key] = val
338-
else:
339-
children[key] = replace(group, consolidated_metadata=None)
338+
children[key] = replace(group, consolidated_metadata=None)
340339
return children
341340

342341
for k, v in self.metadata.items():
@@ -1276,9 +1275,8 @@ async def require_array(
12761275
if exact:
12771276
if ds.dtype != dtype:
12781277
raise TypeError(f"Incompatible dtype ({ds.dtype} vs {dtype})")
1279-
else:
1280-
if not np.can_cast(ds.dtype, dtype):
1281-
raise TypeError(f"Incompatible dtype ({ds.dtype} vs {dtype})")
1278+
elif not np.can_cast(ds.dtype, dtype):
1279+
raise TypeError(f"Incompatible dtype ({ds.dtype} vs {dtype})")
12821280
except KeyError:
12831281
ds = await self.create_array(name, shape=shape, dtype=dtype, **kwargs)
12841282

@@ -3264,24 +3262,23 @@ async def create_hierarchy(
32643262
else:
32653263
# Any other exception is a real error
32663264
raise extant_node
3267-
else:
3268-
# this is a node that already exists, but a node with the same key was specified
3269-
# in nodes_parsed.
3270-
if isinstance(extant_node, GroupMetadata):
3271-
# a group already exists where we want to create a group
3272-
if isinstance(proposed_node, ImplicitGroupMarker):
3273-
# we have proposed an implicit group, which is OK -- we will just skip
3274-
# creating this particular metadata document
3275-
redundant_implicit_groups.append(key)
3276-
else:
3277-
# we have proposed an explicit group, which is an error, given that a
3278-
# group already exists.
3279-
msg = f"A group exists in store {store!r} at path {key!r}."
3280-
raise ContainsGroupError(msg)
3281-
elif isinstance(extant_node, ArrayV2Metadata | ArrayV3Metadata):
3282-
# we are trying to overwrite an existing array. this is an error.
3283-
msg = f"An array exists in store {store!r} at path {key!r}."
3284-
raise ContainsArrayError(msg)
3265+
# this is a node that already exists, but a node with the same key was specified
3266+
# in nodes_parsed.
3267+
elif isinstance(extant_node, GroupMetadata):
3268+
# a group already exists where we want to create a group
3269+
if isinstance(proposed_node, ImplicitGroupMarker):
3270+
# we have proposed an implicit group, which is OK -- we will just skip
3271+
# creating this particular metadata document
3272+
redundant_implicit_groups.append(key)
3273+
else:
3274+
# we have proposed an explicit group, which is an error, given that a
3275+
# group already exists.
3276+
msg = f"A group exists in store {store!r} at path {key!r}."
3277+
raise ContainsGroupError(msg)
3278+
elif isinstance(extant_node, ArrayV2Metadata | ArrayV3Metadata):
3279+
# we are trying to overwrite an existing array. this is an error.
3280+
msg = f"An array exists in store {store!r} at path {key!r}."
3281+
raise ContainsArrayError(msg)
32853282

32863283
nodes_explicit: dict[str, GroupMetadata | ArrayV2Metadata | ArrayV3Metadata] = {}
32873284

@@ -3439,13 +3436,12 @@ def _parse_hierarchy_dict(
34393436
# If a component is not already in the output dict, add ImplicitGroupMetadata
34403437
if subpath not in out:
34413438
out[subpath] = ImplicitGroupMarker(zarr_format=v.zarr_format)
3442-
else:
3443-
if not isinstance(out[subpath], GroupMetadata | ImplicitGroupMarker):
3444-
msg = (
3445-
f"The node at {subpath} contains other nodes, but it is not a Zarr group. "
3446-
"This is invalid. Only Zarr groups can contain other nodes."
3447-
)
3448-
raise ValueError(msg)
3439+
elif not isinstance(out[subpath], GroupMetadata | ImplicitGroupMarker):
3440+
msg = (
3441+
f"The node at {subpath} contains other nodes, but it is not a Zarr group. "
3442+
"This is invalid. Only Zarr groups can contain other nodes."
3443+
)
3444+
raise ValueError(msg)
34493445
return out
34503446

34513447

@@ -3649,12 +3645,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
36493645
# return the array metadata.
36503646
if zarray_bytes is not None:
36513647
zmeta = json.loads(zarray_bytes.to_bytes())
3648+
elif zgroup_bytes is None:
3649+
# neither .zarray or .zgroup were found results in KeyError
3650+
raise FileNotFoundError(path)
36523651
else:
3653-
if zgroup_bytes is None:
3654-
# neither .zarray or .zgroup were found results in KeyError
3655-
raise FileNotFoundError(path)
3656-
else:
3657-
zmeta = json.loads(zgroup_bytes.to_bytes())
3652+
zmeta = json.loads(zgroup_bytes.to_bytes())
36583653

36593654
return _build_metadata_v2(zmeta, zattrs)
36603655

tests/test_group.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -372,16 +372,13 @@ def test_group_getitem(store: Store, zarr_format: ZarrFormat, consolidated: bool
372372
group = zarr.api.synchronous.consolidate_metadata(
373373
store=store, zarr_format=zarr_format
374374
)
375-
else:
376-
if isinstance(store, ZipStore):
377-
with pytest.warns(UserWarning, match="Duplicate name: "):
378-
group = zarr.api.synchronous.consolidate_metadata(
379-
store=store, zarr_format=zarr_format
380-
)
381-
else:
375+
elif isinstance(store, ZipStore):
376+
with pytest.warns(UserWarning, match="Duplicate name: "):
382377
group = zarr.api.synchronous.consolidate_metadata(
383378
store=store, zarr_format=zarr_format
384379
)
380+
else:
381+
group = zarr.api.synchronous.consolidate_metadata(store=store, zarr_format=zarr_format)
385382
# we're going to assume that `group.metadata` is correct, and reuse that to focus
386383
# on indexing in this test. Other tests verify the correctness of group.metadata
387384
object.__setattr__(
@@ -581,12 +578,11 @@ def test_group_child_iterators(store: Store, zarr_format: ZarrFormat, consolidat
581578
group = zarr.consolidate_metadata(store)
582579
else:
583580
group = zarr.consolidate_metadata(store)
584-
else:
585-
if isinstance(store, ZipStore):
586-
with pytest.warns(UserWarning, match="Duplicate name: "):
587-
group = zarr.consolidate_metadata(store)
588-
else:
581+
elif isinstance(store, ZipStore):
582+
with pytest.warns(UserWarning, match="Duplicate name: "):
589583
group = zarr.consolidate_metadata(store)
584+
else:
585+
group = zarr.consolidate_metadata(store)
590586
if zarr_format == 2:
591587
metadata = {
592588
"subarray": {
@@ -1443,15 +1439,14 @@ async def test_members_name(store: Store, consolidate: bool, zarr_format: ZarrFo
14431439
group = zarr.api.synchronous.consolidate_metadata(store)
14441440
else:
14451441
group = zarr.api.synchronous.consolidate_metadata(store)
1446-
else:
1447-
if zarr_format == 3:
1448-
with pytest.warns(
1449-
ZarrUserWarning,
1450-
match="Consolidated metadata is currently not part in the Zarr format 3 specification.",
1451-
):
1452-
group = zarr.api.synchronous.consolidate_metadata(store)
1453-
else:
1442+
elif zarr_format == 3:
1443+
with pytest.warns(
1444+
ZarrUserWarning,
1445+
match="Consolidated metadata is currently not part in the Zarr format 3 specification.",
1446+
):
14541447
group = zarr.api.synchronous.consolidate_metadata(store)
1448+
else:
1449+
group = zarr.api.synchronous.consolidate_metadata(store)
14551450
result = group["a"]["b"]
14561451
assert result.name == "/a/b"
14571452

0 commit comments

Comments
 (0)