Skip to content

Commit 69ad5e7

Browse files
authored
Configure Ruff to apply flake8-bugbear/isort/pyupgrade (zarr-developers#1890)
* config change * auto changes * manual fixes * ci
1 parent 846c085 commit 69ad5e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+455
-305
lines changed

bench/compress_normal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import sys
22
import timeit
33

4+
import line_profiler
45
import numpy as np
56

6-
import line_profiler
77
import zarr
88
from zarr import blosc
99

pyproject.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,17 @@ extend-exclude = [
160160
"build",
161161
"dist",
162162
"venv",
163-
"docs"
163+
"docs",
164+
"src/zarr/v2/",
165+
"tests/v2/",
164166
]
165167

166168
[tool.ruff.lint]
167169
extend-select = [
168-
"RUF"
170+
"B", # flake8-bugbear
171+
"I", # isort
172+
"UP", # pyupgrade
173+
"RUF",
169174
]
170175
ignore = [
171176
"RUF003",

src/zarr/__init__.py

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

33
import zarr.codecs # noqa: F401
4+
from zarr._version import version as __version__
45
from zarr.array import Array, AsyncArray
56
from zarr.config import config # noqa: F401
67
from zarr.group import AsyncGroup, Group
@@ -9,7 +10,6 @@
910
make_store_path,
1011
)
1112
from zarr.sync import sync as _sync
12-
from zarr._version import version as __version__
1313

1414
# in case setuptools scm screw up and find version to be 0.0.0
1515
assert not __version__.startswith("0.0.0")

src/zarr/abc/codec.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
from __future__ import annotations
22

33
from abc import abstractmethod
4-
from typing import TYPE_CHECKING, Generic, Iterable, TypeVar
4+
from collections.abc import Iterable
5+
from typing import TYPE_CHECKING, Generic, TypeVar
56

67
from zarr.abc.metadata import Metadata
78
from zarr.abc.store import ByteGetter, ByteSetter
89
from zarr.buffer import Buffer, NDBuffer
910

10-
1111
if TYPE_CHECKING:
1212
from typing_extensions import Self
13+
1314
from zarr.common import ArraySpec, SliceSelection
1415
from zarr.metadata import ArrayMetadata
1516

src/zarr/abc/metadata.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from __future__ import annotations
2-
from typing import TYPE_CHECKING, Sequence
2+
3+
from collections.abc import Sequence
4+
from typing import TYPE_CHECKING
35

46
if TYPE_CHECKING:
5-
from typing import Dict
67
from typing_extensions import Self
78

8-
from dataclasses import fields, dataclass
9+
from dataclasses import dataclass, fields
910

1011
from zarr.common import JSON
1112

@@ -36,7 +37,7 @@ def to_dict(self) -> JSON:
3637
return out_dict
3738

3839
@classmethod
39-
def from_dict(cls, data: Dict[str, JSON]) -> Self:
40+
def from_dict(cls, data: dict[str, JSON]) -> Self:
4041
"""
4142
Create an instance of the model from a dictionary
4243
"""

src/zarr/abc/store.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
from abc import abstractmethod, ABC
1+
from abc import ABC, abstractmethod
22
from collections.abc import AsyncGenerator
3-
from typing import List, Protocol, Tuple, Optional, runtime_checkable
3+
from typing import Protocol, runtime_checkable
44

5-
from zarr.common import BytesLike
65
from zarr.buffer import Buffer
6+
from zarr.common import BytesLike
77

88

99
class Store(ABC):
1010
@abstractmethod
1111
async def get(
12-
self, key: str, byte_range: Optional[Tuple[int, Optional[int]]] = None
13-
) -> Optional[Buffer]:
12+
self, key: str, byte_range: tuple[int, int | None] | None = None
13+
) -> Buffer | None:
1414
"""Retrieve the value associated with a given key.
1515
1616
Parameters
@@ -26,8 +26,8 @@ async def get(
2626

2727
@abstractmethod
2828
async def get_partial_values(
29-
self, key_ranges: List[Tuple[str, Tuple[int, int]]]
30-
) -> List[Optional[Buffer]]:
29+
self, key_ranges: list[tuple[str, tuple[int, int]]]
30+
) -> list[Buffer | None]:
3131
"""Retrieve possibly partial values from given key_ranges.
3232
3333
Parameters
@@ -150,18 +150,14 @@ def list_dir(self, prefix: str) -> AsyncGenerator[str, None]:
150150

151151
@runtime_checkable
152152
class ByteGetter(Protocol):
153-
async def get(
154-
self, byte_range: Optional[Tuple[int, Optional[int]]] = None
155-
) -> Optional[Buffer]: ...
153+
async def get(self, byte_range: tuple[int, int | None] | None = None) -> Buffer | None: ...
156154

157155

158156
@runtime_checkable
159157
class ByteSetter(Protocol):
160-
async def get(
161-
self, byte_range: Optional[Tuple[int, Optional[int]]] = None
162-
) -> Optional[Buffer]: ...
158+
async def get(self, byte_range: tuple[int, int | None] | None = None) -> Buffer | None: ...
163159

164-
async def set(self, value: Buffer, byte_range: Optional[Tuple[int, int]] = None) -> None: ...
160+
async def set(self, value: Buffer, byte_range: tuple[int, int] | None = None) -> None: ...
165161

166162
async def delete(self) -> None: ...
167163

src/zarr/array.py

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

3+
import json
4+
35
# Notes on what I've changed here:
46
# 1. Split Array into AsyncArray and Array
57
# 3. Added .size and .attrs methods
68
# 4. Temporarily disabled the creation of ArrayV2
79
# 5. Added from_dict to AsyncArray
8-
910
# Questions to consider:
1011
# 1. Was splitting the array into two classes really necessary?
11-
12-
1312
from asyncio import gather
13+
from collections.abc import Iterable
1414
from dataclasses import dataclass, replace
15-
16-
import json
17-
from typing import Any, Iterable, Literal
15+
from typing import Any, Literal
1816

1917
import numpy as np
2018
import numpy.typing as npt
19+
2120
from zarr.abc.codec import Codec
2221
from zarr.abc.store import set_or_delete
23-
24-
2522
from zarr.attributes import Attributes
2623
from zarr.buffer import Factory, NDArrayLike, NDBuffer
24+
from zarr.chunk_grids import RegularChunkGrid
25+
from zarr.chunk_key_encodings import ChunkKeyEncoding, DefaultChunkKeyEncoding, V2ChunkKeyEncoding
2726
from zarr.codecs import BytesCodec
2827
from zarr.common import (
2928
JSON,
@@ -36,11 +35,8 @@
3635
concurrent_map,
3736
)
3837
from zarr.config import config
39-
4038
from zarr.indexing import BasicIndexer
41-
from zarr.chunk_grids import RegularChunkGrid
42-
from zarr.chunk_key_encodings import ChunkKeyEncoding, DefaultChunkKeyEncoding, V2ChunkKeyEncoding
43-
from zarr.metadata import ArrayMetadata, ArrayV3Metadata, ArrayV2Metadata, parse_indexing_order
39+
from zarr.metadata import ArrayMetadata, ArrayV2Metadata, ArrayV3Metadata, parse_indexing_order
4440
from zarr.store import StoreLike, StorePath, make_store_path
4541
from zarr.sync import sync
4642

src/zarr/attributes.py

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

3-
from collections.abc import MutableMapping
4-
from typing import TYPE_CHECKING, Iterator
3+
from collections.abc import Iterator, MutableMapping
4+
from typing import TYPE_CHECKING
55

66
from zarr.common import JSON
77

88
if TYPE_CHECKING:
9-
from zarr.group import Group
109
from zarr.array import Array
10+
from zarr.group import Group
1111

1212

1313
class Attributes(MutableMapping[str, JSON]):

src/zarr/buffer.py

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

33
import sys
4+
from collections.abc import Callable, Iterable
45
from typing import (
56
TYPE_CHECKING,
67
Any,
7-
Callable,
8-
Iterable,
98
Literal,
10-
Optional,
119
Protocol,
12-
Tuple,
1310
TypeAlias,
1411
)
1512

1613
import numpy as np
1714

1815
if TYPE_CHECKING:
1916
from typing_extensions import Self
17+
2018
from zarr.codecs.bytes import Endian
2119
from zarr.common import BytesLike
2220

@@ -44,7 +42,7 @@ def __call__(
4442
shape: Iterable[int],
4543
dtype: np.DTypeLike,
4644
order: Literal["C", "F"],
47-
fill_value: Optional[Any],
45+
fill_value: Any | None,
4846
) -> NDBuffer:
4947
"""Factory function to create a new NDBuffer (or subclass)
5048
@@ -227,7 +225,7 @@ def __add__(self, other: Buffer) -> Self:
227225
return self.__class__(np.concatenate((self._data, other_array)))
228226

229227
def __eq__(self, other: Any) -> bool:
230-
if isinstance(other, (bytes, bytearray)):
228+
if isinstance(other, bytes | bytearray):
231229
# Many of the tests compares `Buffer` with `bytes` so we
232230
# convert the bytes to a Buffer and try again
233231
return self == self.from_bytes(other)
@@ -275,7 +273,7 @@ def create(
275273
shape: Iterable[int],
276274
dtype: np.DTypeLike,
277275
order: Literal["C", "F"] = "C",
278-
fill_value: Optional[Any] = None,
276+
fill_value: Any | None = None,
279277
) -> Self:
280278
"""Create a new buffer and its underlying ndarray-like object
281279
@@ -380,7 +378,7 @@ def dtype(self) -> np.dtype[Any]:
380378
return self._data.dtype
381379

382380
@property
383-
def shape(self) -> Tuple[int, ...]:
381+
def shape(self) -> tuple[int, ...]:
384382
return self._data.shape
385383

386384
@property

src/zarr/chunk_grids.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from __future__ import annotations
2+
23
import itertools
3-
from typing import TYPE_CHECKING, Any, Dict, Iterator
4+
from collections.abc import Iterator
45
from dataclasses import dataclass
5-
from zarr.abc.metadata import Metadata
6+
from typing import TYPE_CHECKING, Any
67

8+
from zarr.abc.metadata import Metadata
79
from zarr.common import (
810
JSON,
911
ChunkCoords,
@@ -20,7 +22,7 @@
2022
@dataclass(frozen=True)
2123
class ChunkGrid(Metadata):
2224
@classmethod
23-
def from_dict(cls, data: Dict[str, JSON]) -> ChunkGrid:
25+
def from_dict(cls, data: dict[str, JSON]) -> ChunkGrid:
2426
if isinstance(data, ChunkGrid):
2527
return data
2628

@@ -43,15 +45,15 @@ def __init__(self, *, chunk_shape: ChunkCoordsLike) -> None:
4345
object.__setattr__(self, "chunk_shape", chunk_shape_parsed)
4446

4547
@classmethod
46-
def from_dict(cls, data: Dict[str, Any]) -> Self:
48+
def from_dict(cls, data: dict[str, Any]) -> Self:
4749
_, configuration_parsed = parse_named_configuration(data, "regular")
4850

4951
return cls(**configuration_parsed) # type: ignore[arg-type]
5052

51-
def to_dict(self) -> Dict[str, JSON]:
53+
def to_dict(self) -> dict[str, JSON]:
5254
return {"name": "regular", "configuration": {"chunk_shape": list(self.chunk_shape)}}
5355

5456
def all_chunk_coords(self, array_shape: ChunkCoords) -> Iterator[ChunkCoords]:
5557
return itertools.product(
56-
*(range(0, _ceildiv(s, c)) for s, c in zip(array_shape, self.chunk_shape))
58+
*(range(0, _ceildiv(s, c)) for s, c in zip(array_shape, self.chunk_shape, strict=False))
5759
)

src/zarr/chunk_key_encodings.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import annotations
2+
23
from abc import abstractmethod
3-
from typing import TYPE_CHECKING, Dict, Literal, cast
44
from dataclasses import dataclass
5-
from zarr.abc.metadata import Metadata
5+
from typing import TYPE_CHECKING, Literal, cast
66

7+
from zarr.abc.metadata import Metadata
78
from zarr.common import (
89
JSON,
910
ChunkCoords,
@@ -33,7 +34,7 @@ def __init__(self, *, separator: SeparatorLiteral) -> None:
3334
object.__setattr__(self, "separator", separator_parsed)
3435

3536
@classmethod
36-
def from_dict(cls, data: Dict[str, JSON]) -> ChunkKeyEncoding:
37+
def from_dict(cls, data: dict[str, JSON]) -> ChunkKeyEncoding:
3738
if isinstance(data, ChunkKeyEncoding):
3839
return data
3940

@@ -44,7 +45,7 @@ def from_dict(cls, data: Dict[str, JSON]) -> ChunkKeyEncoding:
4445
return V2ChunkKeyEncoding(**configuration_parsed) # type: ignore[arg-type]
4546
raise ValueError(f"Unknown chunk key encoding. Got {name_parsed}.")
4647

47-
def to_dict(self) -> Dict[str, JSON]:
48+
def to_dict(self) -> dict[str, JSON]:
4849
return {"name": self.name, "configuration": {"separator": self.separator}}
4950

5051
@abstractmethod

src/zarr/codecs/__init__.py

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

3-
from zarr.codecs.blosc import BloscCodec, BloscCname, BloscShuffle # noqa: F401
3+
from zarr.codecs.blosc import BloscCname, BloscCodec, BloscShuffle # noqa: F401
44
from zarr.codecs.bytes import BytesCodec, Endian # noqa: F401
55
from zarr.codecs.crc32c_ import Crc32cCodec # noqa: F401
66
from zarr.codecs.gzip import GzipCodec # noqa: F401
7+
from zarr.codecs.pipeline import BatchedCodecPipeline # noqa: F401
78
from zarr.codecs.sharding import ShardingCodec, ShardingCodecIndexLocation # noqa: F401
89
from zarr.codecs.transpose import TransposeCodec # noqa: F401
910
from zarr.codecs.zstd import ZstdCodec # noqa: F401
10-
from zarr.codecs.pipeline import BatchedCodecPipeline # noqa: F401

src/zarr/codecs/_v2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
from dataclasses import dataclass
44

5+
import numcodecs
6+
from numcodecs.compat import ensure_bytes, ensure_ndarray
7+
58
from zarr.buffer import Buffer, NDBuffer
69
from zarr.codecs.mixins import ArrayArrayCodecBatchMixin, ArrayBytesCodecBatchMixin
710
from zarr.common import JSON, ArraySpec, to_thread
811

9-
import numcodecs
10-
from numcodecs.compat import ensure_bytes, ensure_ndarray
11-
1212

1313
@dataclass(frozen=True)
1414
class V2Compressor(ArrayBytesCodecBatchMixin):

0 commit comments

Comments
 (0)