Skip to content

Commit

Permalink
Enforce ruff/flake8-type-checking rules (TCH) (#2110)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
DimitriPapadopoulos authored Sep 6, 2024
1 parent de075de commit c08b008
Show file tree
Hide file tree
Showing 35 changed files with 118 additions and 83 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ extend-select = [
"UP", # pyupgrade
"RSE",
"RUF",
"TCH", # flake8-type-checking
"TRY", # tryceratops
]
ignore = [
Expand Down
10 changes: 5 additions & 5 deletions src/zarr/abc/codec.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
from __future__ import annotations

from abc import abstractmethod
from collections.abc import Awaitable, Callable, Iterable
from typing import TYPE_CHECKING, Any, Generic, TypeVar

import numpy as np

from zarr.abc.metadata import Metadata
from zarr.abc.store import ByteGetter, ByteSetter
from zarr.core.buffer import Buffer, NDBuffer
from zarr.core.chunk_grids import ChunkGrid
from zarr.core.common import ChunkCoords, concurrent_map
from zarr.core.config import config

if TYPE_CHECKING:
from collections.abc import Awaitable, Callable, Iterable

import numpy as np
from typing_extensions import Self

from zarr.abc.store import ByteGetter, ByteSetter
from zarr.core.array_spec import ArraySpec
from zarr.core.chunk_grids import ChunkGrid
from zarr.core.common import JSON
from zarr.core.indexing import SelectorTuple

Expand Down
4 changes: 2 additions & 2 deletions src/zarr/abc/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
if TYPE_CHECKING:
from typing_extensions import Self

from dataclasses import dataclass, fields
from zarr.core.common import JSON

from zarr.core.common import JSON
from dataclasses import dataclass, fields

__all__ = ["Metadata"]

Expand Down
13 changes: 8 additions & 5 deletions src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

import asyncio
import warnings
from collections.abc import Iterable
from typing import Any, Literal, Union, cast
from typing import TYPE_CHECKING, Any, Literal, Union, cast

import numpy as np
import numpy.typing as npt

from zarr.abc.codec import Codec
from zarr.core.array import Array, AsyncArray
from zarr.core.buffer import NDArrayLike
from zarr.core.chunk_key_encodings import ChunkKeyEncoding
from zarr.core.common import JSON, AccessModeLiteral, ChunkCoords, MemoryOrder, ZarrFormat
from zarr.core.group import AsyncGroup
from zarr.core.metadata import ArrayV2Metadata, ArrayV3Metadata
Expand All @@ -20,6 +16,13 @@
make_store_path,
)

if TYPE_CHECKING:
from collections.abc import Iterable

from zarr.abc.codec import Codec
from zarr.core.buffer import NDArrayLike
from zarr.core.chunk_key_encodings import ChunkKeyEncoding

__all__ = [
"consolidate_metadata",
"copy",
Expand Down
10 changes: 6 additions & 4 deletions src/zarr/api/synchronous.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from __future__ import annotations

from typing import Any
from typing import TYPE_CHECKING, Any

import zarr.api.asynchronous as async_api
from zarr.core.array import Array, AsyncArray
from zarr.core.buffer import NDArrayLike
from zarr.core.common import JSON, AccessModeLiteral, ChunkCoords, ZarrFormat
from zarr.core.group import Group
from zarr.core.sync import sync
from zarr.store import StoreLike

if TYPE_CHECKING:
from zarr.core.buffer import NDArrayLike
from zarr.core.common import JSON, AccessModeLiteral, ChunkCoords, ZarrFormat
from zarr.store import StoreLike

__all__ = [
"consolidate_metadata",
Expand Down
5 changes: 4 additions & 1 deletion src/zarr/codecs/_v2.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

import numcodecs
from numcodecs.compat import ensure_bytes, ensure_ndarray

from zarr.abc.codec import ArrayArrayCodec, ArrayBytesCodec
from zarr.core.array_spec import ArraySpec
from zarr.core.buffer import Buffer, NDBuffer, default_buffer_prototype
from zarr.core.common import JSON, to_thread
from zarr.registry import get_ndbuffer_class

if TYPE_CHECKING:
from zarr.core.array_spec import ArraySpec


@dataclass(frozen=True)
class V2Compressor(ArrayBytesCodec):
Expand Down
5 changes: 3 additions & 2 deletions src/zarr/codecs/blosc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
from numcodecs.blosc import Blosc

from zarr.abc.codec import BytesBytesCodec
from zarr.core.array_spec import ArraySpec
from zarr.core.buffer import Buffer
from zarr.core.buffer.cpu import as_numpy_array_wrapper
from zarr.core.common import JSON, parse_enum, parse_named_configuration, to_thread
from zarr.registry import register_codec

if TYPE_CHECKING:
from typing_extensions import Self

from zarr.core.array_spec import ArraySpec
from zarr.core.buffer import Buffer


class BloscShuffle(Enum):
noshuffle = "noshuffle"
Expand Down
3 changes: 2 additions & 1 deletion src/zarr/codecs/bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import numpy as np

from zarr.abc.codec import ArrayBytesCodec
from zarr.core.array_spec import ArraySpec
from zarr.core.buffer import Buffer, NDArrayLike, NDBuffer
from zarr.core.common import JSON, parse_enum, parse_named_configuration
from zarr.registry import register_codec

if TYPE_CHECKING:
from typing_extensions import Self

from zarr.core.array_spec import ArraySpec


class Endian(Enum):
big = "big"
Expand Down
5 changes: 3 additions & 2 deletions src/zarr/codecs/crc32c_.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
from crc32c import crc32c

from zarr.abc.codec import BytesBytesCodec
from zarr.core.array_spec import ArraySpec
from zarr.core.buffer import Buffer
from zarr.core.common import JSON, parse_named_configuration
from zarr.registry import register_codec

if TYPE_CHECKING:
from typing_extensions import Self

from zarr.core.array_spec import ArraySpec
from zarr.core.buffer import Buffer


@dataclass(frozen=True)
class Crc32cCodec(BytesBytesCodec):
Expand Down
5 changes: 3 additions & 2 deletions src/zarr/codecs/gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
from numcodecs.gzip import GZip

from zarr.abc.codec import BytesBytesCodec
from zarr.core.array_spec import ArraySpec
from zarr.core.buffer import Buffer
from zarr.core.buffer.cpu import as_numpy_array_wrapper
from zarr.core.common import JSON, parse_named_configuration, to_thread
from zarr.registry import register_codec

if TYPE_CHECKING:
from typing_extensions import Self

from zarr.core.array_spec import ArraySpec
from zarr.core.buffer import Buffer


def parse_gzip_level(data: JSON) -> int:
if not isinstance(data, (int)):
Expand Down
9 changes: 4 additions & 5 deletions src/zarr/codecs/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from typing import TYPE_CHECKING, Any, TypeVar
from warnings import warn

import numpy as np

from zarr.abc.codec import (
ArrayArrayCodec,
ArrayBytesCodec,
Expand All @@ -17,18 +15,19 @@
Codec,
CodecPipeline,
)
from zarr.abc.store import ByteGetter, ByteSetter
from zarr.core.buffer import Buffer, BufferPrototype, NDBuffer
from zarr.core.chunk_grids import ChunkGrid
from zarr.core.common import JSON, ChunkCoords, concurrent_map, parse_named_configuration
from zarr.core.config import config
from zarr.core.indexing import SelectorTuple, is_scalar, is_total_slice
from zarr.registry import get_codec_class, register_pipeline

if TYPE_CHECKING:
import numpy as np
from typing_extensions import Self

from zarr.abc.store import ByteGetter, ByteSetter
from zarr.core.array_spec import ArraySpec
from zarr.core.buffer import Buffer, BufferPrototype, NDBuffer
from zarr.core.chunk_grids import ChunkGrid

T = TypeVar("T")
U = TypeVar("U")
Expand Down
5 changes: 3 additions & 2 deletions src/zarr/codecs/transpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

from zarr.abc.codec import ArrayArrayCodec
from zarr.core.array_spec import ArraySpec
from zarr.core.buffer import NDBuffer
from zarr.core.chunk_grids import ChunkGrid
from zarr.core.common import JSON, ChunkCoordsLike, parse_named_configuration
from zarr.registry import register_codec

Expand All @@ -18,6 +16,9 @@

from typing_extensions import Self

from zarr.core.buffer import NDBuffer
from zarr.core.chunk_grids import ChunkGrid


def parse_transpose_order(data: JSON | Iterable[int]) -> tuple[int, ...]:
if not isinstance(data, Iterable):
Expand Down
5 changes: 3 additions & 2 deletions src/zarr/codecs/zstd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
from numcodecs.zstd import Zstd

from zarr.abc.codec import BytesBytesCodec
from zarr.core.array_spec import ArraySpec
from zarr.core.buffer import Buffer
from zarr.core.buffer.cpu import as_numpy_array_wrapper
from zarr.core.common import JSON, parse_named_configuration, to_thread
from zarr.registry import register_codec

if TYPE_CHECKING:
from typing_extensions import Self

from zarr.core.array_spec import ArraySpec
from zarr.core.buffer import Buffer


def parse_zstd_level(data: JSON) -> int:
if isinstance(data, int):
Expand Down
9 changes: 6 additions & 3 deletions src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import json
from asyncio import gather
from collections.abc import Iterable
from dataclasses import dataclass, field, replace
from typing import Any, Literal, cast
from typing import TYPE_CHECKING, Any, Literal, cast

import numpy as np
import numpy.typing as npt

from zarr.abc.codec import Codec, CodecPipeline
from zarr.abc.store import set_or_delete
from zarr.codecs import BytesCodec
from zarr.codecs._v2 import V2Compressor, V2Filters
Expand Down Expand Up @@ -63,6 +61,11 @@
ensure_no_existing_node,
)

if TYPE_CHECKING:
from collections.abc import Iterable

from zarr.abc.codec import Codec, CodecPipeline


def parse_array_metadata(data: Any) -> ArrayV2Metadata | ArrayV3Metadata:
if isinstance(data, ArrayV2Metadata | ArrayV3Metadata):
Expand Down
4 changes: 2 additions & 2 deletions src/zarr/core/array_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Literal

import numpy as np

from zarr.core.common import parse_dtype, parse_fill_value, parse_order, parse_shapelike

if TYPE_CHECKING:
import numpy as np

from zarr.core.buffer import BufferPrototype
from zarr.core.common import ChunkCoords

Expand Down
6 changes: 3 additions & 3 deletions src/zarr/core/buffer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import sys
from abc import ABC, abstractmethod
from collections.abc import Iterable, Sequence
from typing import (
TYPE_CHECKING,
Any,
Expand All @@ -17,17 +16,18 @@
import numpy as np
import numpy.typing as npt

from zarr.core.common import ChunkCoords
from zarr.registry import (
get_buffer_class,
get_ndbuffer_class,
)

if TYPE_CHECKING:
from collections.abc import Iterable, Sequence

from typing_extensions import Self

from zarr.codecs.bytes import Endian
from zarr.core.common import BytesLike
from zarr.core.common import BytesLike, ChunkCoords


@runtime_checkable
Expand Down
5 changes: 3 additions & 2 deletions src/zarr/core/buffer/cpu.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from collections.abc import Callable, Iterable
from typing import (
TYPE_CHECKING,
Any,
Expand All @@ -11,15 +10,17 @@
import numpy.typing as npt

from zarr.core.buffer import core
from zarr.core.buffer.core import ArrayLike, NDArrayLike
from zarr.registry import (
register_buffer,
register_ndbuffer,
)

if TYPE_CHECKING:
from collections.abc import Callable, Iterable

from typing_extensions import Self

from zarr.core.buffer.core import ArrayLike, NDArrayLike
from zarr.core.common import BytesLike


Expand Down
3 changes: 2 additions & 1 deletion src/zarr/core/buffer/gpu.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import warnings
from collections.abc import Iterable
from typing import (
TYPE_CHECKING,
Any,
Expand All @@ -16,6 +15,8 @@
from zarr.core.buffer.core import ArrayLike, BufferPrototype, NDArrayLike

if TYPE_CHECKING:
from collections.abc import Iterable

from typing_extensions import Self

from zarr.core.common import BytesLike
Expand Down
3 changes: 2 additions & 1 deletion src/zarr/core/chunk_grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import math
import operator
from abc import abstractmethod
from collections.abc import Iterator
from dataclasses import dataclass
from functools import reduce
from typing import TYPE_CHECKING
Expand All @@ -22,6 +21,8 @@
from zarr.core.indexing import ceildiv

if TYPE_CHECKING:
from collections.abc import Iterator

from typing_extensions import Self


Expand Down
Loading

0 comments on commit c08b008

Please sign in to comment.