Skip to content

Commit f66dcc5

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

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
@@ -410,13 +410,12 @@ async def _read_key(
410410
):
411411
if chunk_array is None:
412412
chunk_array_batch.append(None) # type: ignore[unreachable]
413+
elif not chunk_spec.config.write_empty_chunks and chunk_array.all_equal(
414+
fill_value_or_default(chunk_spec)
415+
):
416+
chunk_array_batch.append(None)
413417
else:
414-
if not chunk_spec.config.write_empty_chunks and chunk_array.all_equal(
415-
fill_value_or_default(chunk_spec)
416-
):
417-
chunk_array_batch.append(None)
418-
else:
419-
chunk_array_batch.append(chunk_array)
418+
chunk_array_batch.append(chunk_array)
420419

421420
chunk_bytes_batch = await self.encode_batch(
422421
[

src/zarr/core/group.py

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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

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)