Skip to content

Commit e968ac5

Browse files
Enforce ruff/flake8-comprehensions rules (C4) (zarr-developers#2239)
* Apply ruff/flake8-comprehensions rule C401 C401 Unnecessary generator (rewrite as a `set` comprehension) * Apply ruff/flake8-comprehensions rule C408 C408 Unnecessary `dict` call (rewrite as a literal) * Apply ruff/flake8-comprehensions rule C409 C409 Unnecessary list comprehension passed to `tuple()` (rewrite as a generator) * Apply ruff/flake8-comprehensions rule C416 C416 Unnecessary `list` comprehension (rewrite using `list()`) * Enforce ruff/flake8-comprehensions rules (C4) --------- Co-authored-by: Joe Hamman <[email protected]>
1 parent 3ce7670 commit e968ac5

File tree

7 files changed

+10
-9
lines changed

7 files changed

+10
-9
lines changed

bench/compress_normal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
a,
1717
chunks=1000000,
1818
compression="blosc",
19-
compression_opts=dict(cname="lz4", clevel=5, shuffle=2),
19+
compression_opts={"cname": "lz4", "clevel": 5, "shuffle": 2},
2020
)
2121
print(z)
2222

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ extend-exclude = [
208208
[tool.ruff.lint]
209209
extend-select = [
210210
"B", # flake8-bugbear
211+
"C4", # flake8-comprehensions
211212
"FLY", # flynt
212213
"I", # isort
213214
"ISC", # flake8-implicit-str-concat

src/zarr/codecs/sharding.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def is_dense(self, chunk_byte_length: int) -> bool:
151151

152152
# Are all non-empty offsets unique?
153153
if len(
154-
set(offset for offset, _ in sorted_offsets_and_lengths if offset != MAX_UINT_64)
154+
{offset for offset, _ in sorted_offsets_and_lengths if offset != MAX_UINT_64}
155155
) != len(sorted_offsets_and_lengths):
156156
return False
157157

@@ -380,8 +380,8 @@ def to_dict(self) -> dict[str, JSON]:
380380
"name": "sharding_indexed",
381381
"configuration": {
382382
"chunk_shape": self.chunk_shape,
383-
"codecs": tuple([s.to_dict() for s in self.codecs]),
384-
"index_codecs": tuple([s.to_dict() for s in self.index_codecs]),
383+
"codecs": tuple(s.to_dict() for s in self.codecs),
384+
"index_codecs": tuple(s.to_dict() for s in self.index_codecs),
385385
"index_location": self.index_location.value,
386386
},
387387
}
@@ -477,7 +477,7 @@ async def _decode_partial_single(
477477
)
478478

479479
indexed_chunks = list(indexer)
480-
all_chunk_coords = set(chunk_coords for chunk_coords, _, _ in indexed_chunks)
480+
all_chunk_coords = {chunk_coords for chunk_coords, _, _ in indexed_chunks}
481481

482482
# reading bytes of all requested chunks
483483
shard_dict: ShardMapping = {}

src/zarr/store/memory.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ async def list_dir(self, prefix: str) -> AsyncGenerator[str, None]:
135135
prefix = prefix[:-1]
136136

137137
if prefix == "":
138-
keys_unique = set(k.split("/")[0] for k in self._store_dict)
138+
keys_unique = {k.split("/")[0] for k in self._store_dict}
139139
else:
140140
# Our dictionary doesn't contain directory markers, but we want to include
141141
# a pseudo directory when there's a nested item and we're listing an

src/zarr/store/zip.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ async def list_dir(self, prefix: str) -> AsyncGenerator[str, None]:
232232
keys = self._zf.namelist()
233233
seen = set()
234234
if prefix == "":
235-
keys_unique = set(k.split("/")[0] for k in keys)
235+
keys_unique = {k.split("/")[0] for k in keys}
236236
for key in keys_unique:
237237
if key not in seen:
238238
seen.add(key)

tests/v3/test_group.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def test_group_iter(store: Store, zarr_format: ZarrFormat) -> None:
271271

272272
group = Group.from_store(store, zarr_format=zarr_format)
273273
with pytest.raises(NotImplementedError):
274-
[x for x in group]
274+
list(group)
275275

276276

277277
def test_group_len(store: Store, zarr_format: ZarrFormat) -> None:

tests/v3/test_store/test_remote.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async def test_basic() -> None:
9292
store = RemoteStore.from_url(
9393
f"s3://{test_bucket_name}",
9494
mode="w",
95-
storage_options=dict(endpoint_url=endpoint_url, anon=False),
95+
storage_options={"endpoint_url": endpoint_url, "anon": False},
9696
)
9797
assert await _collect_aiterator(store.list()) == ()
9898
assert not await store.exists("foo")

0 commit comments

Comments
 (0)