Skip to content

Commit 9dd9ac6

Browse files
Enforce ruff/refurb rules (FURB) (#2373)
* Apply ruff/refurb rule FURB110 FURB110 Replace ternary `if` expression with `or` operator * Apply ruff/refurb rule FURB118 FURB118 Use `operator.itemgetter(0)` instead of defining a lambda * Apply ruff/refurb rule FURB140 FURB140 Use `itertools.starmap` instead of the generator * Apply ruff/refurb rule FURB188 FURB188 Prefer `removesuffix` over conditionally replacing with slice. * Apply ruff/refurb rules (FURB) --------- Co-authored-by: Joe Hamman <joe@earthmover.io>
1 parent afdbb75 commit 9dd9ac6

File tree

9 files changed

+25
-18
lines changed

9 files changed

+25
-18
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ extend-select = [
214214
"B", # flake8-bugbear
215215
"C4", # flake8-comprehensions
216216
"FLY", # flynt
217+
"FURB", # refurb
217218
"G", # flake8-logging-format
218219
"I", # isort
219220
"ISC", # flake8-implicit-str-concat

src/zarr/abc/store.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from abc import ABC, abstractmethod
44
from asyncio import gather
5+
from itertools import starmap
56
from typing import TYPE_CHECKING, NamedTuple, Protocol, runtime_checkable
67

78
if TYPE_CHECKING:
@@ -282,7 +283,7 @@ async def _set_many(self, values: Iterable[tuple[str, Buffer]]) -> None:
282283
"""
283284
Insert multiple (key, value) pairs into storage.
284285
"""
285-
await gather(*(self.set(key, value) for key, value in values))
286+
await gather(*starmap(self.set, values))
286287
return
287288

288289
@property

src/zarr/core/array.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
from asyncio import gather
55
from dataclasses import dataclass, field, replace
6+
from itertools import starmap
67
from logging import getLogger
78
from typing import TYPE_CHECKING, Any, Generic, Literal, cast, overload
89

@@ -816,7 +817,7 @@ def cdata_shape(self) -> ChunkCoords:
816817
Tuple[int]
817818
The shape of the chunk grid for this array.
818819
"""
819-
return tuple(ceildiv(s, c) for s, c in zip(self.shape, self.chunks, strict=False))
820+
return tuple(starmap(ceildiv, zip(self.shape, self.chunks, strict=False)))
820821

821822
@property
822823
def nchunks(self) -> int:
@@ -1385,7 +1386,7 @@ def cdata_shape(self) -> ChunkCoords:
13851386
"""
13861387
The shape of the chunk grid for this array.
13871388
"""
1388-
return tuple(ceildiv(s, c) for s, c in zip(self.shape, self.chunks, strict=False))
1389+
return tuple(starmap(ceildiv, zip(self.shape, self.chunks, strict=False)))
13891390

13901391
@property
13911392
def nchunks(self) -> int:

src/zarr/core/chunk_grids.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,6 @@ def all_chunk_coords(self, array_shape: ChunkCoords) -> Iterator[ChunkCoords]:
188188
def get_nchunks(self, array_shape: ChunkCoords) -> int:
189189
return reduce(
190190
operator.mul,
191-
(ceildiv(s, c) for s, c in zip(array_shape, self.chunk_shape, strict=True)),
191+
itertools.starmap(ceildiv, zip(array_shape, self.chunk_shape, strict=True)),
192192
1,
193193
)

src/zarr/core/common.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import operator
66
from collections.abc import Iterable, Mapping
77
from enum import Enum
8+
from itertools import starmap
89
from typing import (
910
TYPE_CHECKING,
1011
Any,
@@ -52,7 +53,7 @@ async def concurrent_map(
5253
items: Iterable[T], func: Callable[..., Awaitable[V]], limit: int | None = None
5354
) -> list[V]:
5455
if limit is None:
55-
return await asyncio.gather(*[func(*item) for item in items])
56+
return await asyncio.gather(*list(starmap(func, items)))
5657

5758
else:
5859
sem = asyncio.Semaphore(limit)

src/zarr/core/indexing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ def __init__(
11291129
chunks_multi_index_broadcast = np.broadcast_arrays(*chunks_multi_index)
11301130

11311131
# remember shape of selection, because we will flatten indices for processing
1132-
sel_shape = selection_broadcast[0].shape if selection_broadcast[0].shape else (1,)
1132+
sel_shape = selection_broadcast[0].shape or (1,)
11331133

11341134
# flatten selection
11351135
selection_broadcast = tuple(dim_sel.reshape(-1) for dim_sel in selection_broadcast)
@@ -1150,7 +1150,7 @@ def __init__(
11501150
else:
11511151
sel_sort = None
11521152

1153-
shape = selection_broadcast[0].shape if selection_broadcast[0].shape else (1,)
1153+
shape = selection_broadcast[0].shape or (1,)
11541154

11551155
# precompute number of selected items for each chunk
11561156
chunk_nitems = np.bincount(chunks_raveled_indices, minlength=nchunks)

src/zarr/storage/memory.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ async def list_prefix(self, prefix: str) -> AsyncGenerator[str, None]:
156156

157157
async def list_dir(self, prefix: str) -> AsyncGenerator[str, None]:
158158
# docstring inherited
159-
if prefix.endswith("/"):
160-
prefix = prefix[:-1]
159+
prefix = prefix.rstrip("/")
161160

162161
if prefix == "":
163162
keys_unique = {k.split("/")[0] for k in self._store_dict}

src/zarr/storage/zip.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ async def list_prefix(self, prefix: str) -> AsyncGenerator[str, None]:
245245

246246
async def list_dir(self, prefix: str) -> AsyncGenerator[str, None]:
247247
# docstring inherited
248-
if prefix.endswith("/"):
249-
prefix = prefix[:-1]
248+
prefix = prefix.rstrip("/")
250249

251250
keys = self._zf.namelist()
252251
seen = set()

tests/test_group.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import contextlib
4+
import operator
45
import pickle
56
import warnings
67
from typing import TYPE_CHECKING, Any, Literal
@@ -533,14 +534,14 @@ def test_group_child_iterators(store: Store, zarr_format: ZarrFormat, consolidat
533534
ConsolidatedMetadata(metadata={}),
534535
)
535536

536-
result = sorted(group.groups(), key=lambda x: x[0])
537+
result = sorted(group.groups(), key=operator.itemgetter(0))
537538
assert result == expected_groups
538539

539-
assert sorted(group.groups(), key=lambda x: x[0]) == expected_groups
540+
assert sorted(group.groups(), key=operator.itemgetter(0)) == expected_groups
540541
assert sorted(group.group_keys()) == expected_group_keys
541542
assert sorted(group.group_values(), key=lambda x: x.name) == expected_group_values
542543

543-
assert sorted(group.arrays(), key=lambda x: x[0]) == expected_arrays
544+
assert sorted(group.arrays(), key=operator.itemgetter(0)) == expected_arrays
544545
assert sorted(group.array_keys()) == expected_array_keys
545546
assert sorted(group.array_values(), key=lambda x: x.name) == expected_array_values
546547

@@ -1000,7 +1001,7 @@ async def test_group_members_async(store: Store, consolidated_metadata: bool) ->
10001001
g2 = await g1.create_group("g2")
10011002

10021003
# immediate children
1003-
children = sorted([x async for x in group.members()], key=lambda x: x[0])
1004+
children = sorted([x async for x in group.members()], key=operator.itemgetter(0))
10041005
assert children == [
10051006
("a0", a0),
10061007
("g0", g0),
@@ -1010,7 +1011,7 @@ async def test_group_members_async(store: Store, consolidated_metadata: bool) ->
10101011
assert nmembers == 2
10111012

10121013
# partial
1013-
children = sorted([x async for x in group.members(max_depth=1)], key=lambda x: x[0])
1014+
children = sorted([x async for x in group.members(max_depth=1)], key=operator.itemgetter(0))
10141015
expected = [
10151016
("a0", a0),
10161017
("g0", g0),
@@ -1022,7 +1023,9 @@ async def test_group_members_async(store: Store, consolidated_metadata: bool) ->
10221023
assert nmembers == 4
10231024

10241025
# all children
1025-
all_children = sorted([x async for x in group.members(max_depth=None)], key=lambda x: x[0])
1026+
all_children = sorted(
1027+
[x async for x in group.members(max_depth=None)], key=operator.itemgetter(0)
1028+
)
10261029
expected = [
10271030
("a0", a0),
10281031
("g0", g0),
@@ -1053,7 +1056,9 @@ async def test_group_members_async(store: Store, consolidated_metadata: bool) ->
10531056
"consolidated_metadata",
10541057
None,
10551058
)
1056-
all_children = sorted([x async for x in group.members(max_depth=None)], key=lambda x: x[0])
1059+
all_children = sorted(
1060+
[x async for x in group.members(max_depth=None)], key=operator.itemgetter(0)
1061+
)
10571062
assert len(all_children) == 4
10581063
nmembers = await group.nmembers(max_depth=None)
10591064
assert nmembers == 4

0 commit comments

Comments
 (0)