Skip to content

Commit c08b008

Browse files
Enforce ruff/flake8-type-checking rules (TCH) (#2110)
* Apply ruff/flake8-type-checking rule TCH001 TCH001 Move application import into a type-checking block * Apply ruff/flake8-type-checking rule TCH002 TCH002 Move third-party import into a type-checking block * Apply ruff/flake8-type-checking rule TCH003 TCH003 Move standard library import into a type-checking block * Apply ruff/flake8-type-checking rule TCH005 TCH005 Found empty type-checking block * Enforce ruff/flake8-type-checking rules (TCH)
1 parent de075de commit c08b008

35 files changed

+118
-83
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ extend-select = [
212212
"UP", # pyupgrade
213213
"RSE",
214214
"RUF",
215+
"TCH", # flake8-type-checking
215216
"TRY", # tryceratops
216217
]
217218
ignore = [

src/zarr/abc/codec.py

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

33
from abc import abstractmethod
4-
from collections.abc import Awaitable, Callable, Iterable
54
from typing import TYPE_CHECKING, Any, Generic, TypeVar
65

7-
import numpy as np
8-
96
from zarr.abc.metadata import Metadata
10-
from zarr.abc.store import ByteGetter, ByteSetter
117
from zarr.core.buffer import Buffer, NDBuffer
12-
from zarr.core.chunk_grids import ChunkGrid
138
from zarr.core.common import ChunkCoords, concurrent_map
149
from zarr.core.config import config
1510

1611
if TYPE_CHECKING:
12+
from collections.abc import Awaitable, Callable, Iterable
13+
14+
import numpy as np
1715
from typing_extensions import Self
1816

17+
from zarr.abc.store import ByteGetter, ByteSetter
1918
from zarr.core.array_spec import ArraySpec
19+
from zarr.core.chunk_grids import ChunkGrid
2020
from zarr.core.common import JSON
2121
from zarr.core.indexing import SelectorTuple
2222

src/zarr/abc/metadata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
if TYPE_CHECKING:
77
from typing_extensions import Self
88

9-
from dataclasses import dataclass, fields
9+
from zarr.core.common import JSON
1010

11-
from zarr.core.common import JSON
11+
from dataclasses import dataclass, fields
1212

1313
__all__ = ["Metadata"]
1414

src/zarr/api/asynchronous.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
import asyncio
44
import warnings
5-
from collections.abc import Iterable
6-
from typing import Any, Literal, Union, cast
5+
from typing import TYPE_CHECKING, Any, Literal, Union, cast
76

87
import numpy as np
98
import numpy.typing as npt
109

11-
from zarr.abc.codec import Codec
1210
from zarr.core.array import Array, AsyncArray
13-
from zarr.core.buffer import NDArrayLike
14-
from zarr.core.chunk_key_encodings import ChunkKeyEncoding
1511
from zarr.core.common import JSON, AccessModeLiteral, ChunkCoords, MemoryOrder, ZarrFormat
1612
from zarr.core.group import AsyncGroup
1713
from zarr.core.metadata import ArrayV2Metadata, ArrayV3Metadata
@@ -20,6 +16,13 @@
2016
make_store_path,
2117
)
2218

19+
if TYPE_CHECKING:
20+
from collections.abc import Iterable
21+
22+
from zarr.abc.codec import Codec
23+
from zarr.core.buffer import NDArrayLike
24+
from zarr.core.chunk_key_encodings import ChunkKeyEncoding
25+
2326
__all__ = [
2427
"consolidate_metadata",
2528
"copy",

src/zarr/api/synchronous.py

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

3-
from typing import Any
3+
from typing import TYPE_CHECKING, Any
44

55
import zarr.api.asynchronous as async_api
66
from zarr.core.array import Array, AsyncArray
7-
from zarr.core.buffer import NDArrayLike
8-
from zarr.core.common import JSON, AccessModeLiteral, ChunkCoords, ZarrFormat
97
from zarr.core.group import Group
108
from zarr.core.sync import sync
11-
from zarr.store import StoreLike
9+
10+
if TYPE_CHECKING:
11+
from zarr.core.buffer import NDArrayLike
12+
from zarr.core.common import JSON, AccessModeLiteral, ChunkCoords, ZarrFormat
13+
from zarr.store import StoreLike
1214

1315
__all__ = [
1416
"consolidate_metadata",

src/zarr/codecs/_v2.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass
4+
from typing import TYPE_CHECKING
45

56
import numcodecs
67
from numcodecs.compat import ensure_bytes, ensure_ndarray
78

89
from zarr.abc.codec import ArrayArrayCodec, ArrayBytesCodec
9-
from zarr.core.array_spec import ArraySpec
1010
from zarr.core.buffer import Buffer, NDBuffer, default_buffer_prototype
1111
from zarr.core.common import JSON, to_thread
1212
from zarr.registry import get_ndbuffer_class
1313

14+
if TYPE_CHECKING:
15+
from zarr.core.array_spec import ArraySpec
16+
1417

1518
@dataclass(frozen=True)
1619
class V2Compressor(ArrayBytesCodec):

src/zarr/codecs/blosc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
from numcodecs.blosc import Blosc
1010

1111
from zarr.abc.codec import BytesBytesCodec
12-
from zarr.core.array_spec import ArraySpec
13-
from zarr.core.buffer import Buffer
1412
from zarr.core.buffer.cpu import as_numpy_array_wrapper
1513
from zarr.core.common import JSON, parse_enum, parse_named_configuration, to_thread
1614
from zarr.registry import register_codec
1715

1816
if TYPE_CHECKING:
1917
from typing_extensions import Self
2018

19+
from zarr.core.array_spec import ArraySpec
20+
from zarr.core.buffer import Buffer
21+
2122

2223
class BloscShuffle(Enum):
2324
noshuffle = "noshuffle"

src/zarr/codecs/bytes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
import numpy as np
99

1010
from zarr.abc.codec import ArrayBytesCodec
11-
from zarr.core.array_spec import ArraySpec
1211
from zarr.core.buffer import Buffer, NDArrayLike, NDBuffer
1312
from zarr.core.common import JSON, parse_enum, parse_named_configuration
1413
from zarr.registry import register_codec
1514

1615
if TYPE_CHECKING:
1716
from typing_extensions import Self
1817

18+
from zarr.core.array_spec import ArraySpec
19+
1920

2021
class Endian(Enum):
2122
big = "big"

src/zarr/codecs/crc32c_.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
from crc32c import crc32c
99

1010
from zarr.abc.codec import BytesBytesCodec
11-
from zarr.core.array_spec import ArraySpec
12-
from zarr.core.buffer import Buffer
1311
from zarr.core.common import JSON, parse_named_configuration
1412
from zarr.registry import register_codec
1513

1614
if TYPE_CHECKING:
1715
from typing_extensions import Self
1816

17+
from zarr.core.array_spec import ArraySpec
18+
from zarr.core.buffer import Buffer
19+
1920

2021
@dataclass(frozen=True)
2122
class Crc32cCodec(BytesBytesCodec):

src/zarr/codecs/gzip.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
from numcodecs.gzip import GZip
77

88
from zarr.abc.codec import BytesBytesCodec
9-
from zarr.core.array_spec import ArraySpec
10-
from zarr.core.buffer import Buffer
119
from zarr.core.buffer.cpu import as_numpy_array_wrapper
1210
from zarr.core.common import JSON, parse_named_configuration, to_thread
1311
from zarr.registry import register_codec
1412

1513
if TYPE_CHECKING:
1614
from typing_extensions import Self
1715

16+
from zarr.core.array_spec import ArraySpec
17+
from zarr.core.buffer import Buffer
18+
1819

1920
def parse_gzip_level(data: JSON) -> int:
2021
if not isinstance(data, (int)):

0 commit comments

Comments
 (0)