Skip to content

Configure Ruff to apply flake8-bugbear/isort/pyupgrade #1890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bench/compress_normal.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import sys
import timeit

import line_profiler
import numpy as np

import line_profiler
import zarr
from zarr import blosc

Expand Down
9 changes: 7 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,17 @@ extend-exclude = [
"build",
"dist",
"venv",
"docs"
"docs",
"src/zarr/v2/",
"tests/v2/",
]

[tool.ruff.lint]
extend-select = [
"RUF"
"B", # flake8-bugbear
"I", # isort
"UP", # pyupgrade
"RUF",
]
ignore = [
"RUF003",
Expand Down
2 changes: 1 addition & 1 deletion src/zarr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import zarr.codecs # noqa: F401
from zarr._version import version as __version__
from zarr.array import Array, AsyncArray
from zarr.config import config # noqa: F401
from zarr.group import AsyncGroup, Group
Expand All @@ -9,7 +10,6 @@
make_store_path,
)
from zarr.sync import sync as _sync
from zarr._version import version as __version__

# in case setuptools scm screw up and find version to be 0.0.0
assert not __version__.startswith("0.0.0")
Expand Down
5 changes: 3 additions & 2 deletions src/zarr/abc/codec.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from __future__ import annotations

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

from zarr.abc.metadata import Metadata
from zarr.abc.store import ByteGetter, ByteSetter
from zarr.buffer import Buffer, NDBuffer


if TYPE_CHECKING:
from typing_extensions import Self

from zarr.common import ArraySpec, SliceSelection
from zarr.metadata import ArrayMetadata

Expand Down
9 changes: 5 additions & 4 deletions src/zarr/abc/metadata.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Sequence

from collections.abc import Sequence
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Dict
from typing_extensions import Self

from dataclasses import fields, dataclass
from dataclasses import dataclass, fields

from zarr.common import JSON

Expand Down Expand Up @@ -36,7 +37,7 @@ def to_dict(self) -> JSON:
return out_dict

@classmethod
def from_dict(cls, data: Dict[str, JSON]) -> Self:
def from_dict(cls, data: dict[str, JSON]) -> Self:
"""
Create an instance of the model from a dictionary
"""
Expand Down
24 changes: 10 additions & 14 deletions src/zarr/abc/store.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from abc import abstractmethod, ABC
from abc import ABC, abstractmethod
from collections.abc import AsyncGenerator
from typing import List, Protocol, Tuple, Optional, runtime_checkable
from typing import Protocol, runtime_checkable

from zarr.common import BytesLike
from zarr.buffer import Buffer
from zarr.common import BytesLike


class Store(ABC):
@abstractmethod
async def get(
self, key: str, byte_range: Optional[Tuple[int, Optional[int]]] = None
) -> Optional[Buffer]:
self, key: str, byte_range: tuple[int, int | None] | None = None
) -> Buffer | None:
"""Retrieve the value associated with a given key.

Parameters
Expand All @@ -26,8 +26,8 @@ async def get(

@abstractmethod
async def get_partial_values(
self, key_ranges: List[Tuple[str, Tuple[int, int]]]
) -> List[Optional[Buffer]]:
self, key_ranges: list[tuple[str, tuple[int, int]]]
) -> list[Buffer | None]:
"""Retrieve possibly partial values from given key_ranges.

Parameters
Expand Down Expand Up @@ -150,18 +150,14 @@ def list_dir(self, prefix: str) -> AsyncGenerator[str, None]:

@runtime_checkable
class ByteGetter(Protocol):
async def get(
self, byte_range: Optional[Tuple[int, Optional[int]]] = None
) -> Optional[Buffer]: ...
async def get(self, byte_range: tuple[int, int | None] | None = None) -> Buffer | None: ...


@runtime_checkable
class ByteSetter(Protocol):
async def get(
self, byte_range: Optional[Tuple[int, Optional[int]]] = None
) -> Optional[Buffer]: ...
async def get(self, byte_range: tuple[int, int | None] | None = None) -> Buffer | None: ...

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

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

Expand Down
20 changes: 8 additions & 12 deletions src/zarr/array.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
from __future__ import annotations

import json

# Notes on what I've changed here:
# 1. Split Array into AsyncArray and Array
# 3. Added .size and .attrs methods
# 4. Temporarily disabled the creation of ArrayV2
# 5. Added from_dict to AsyncArray

# Questions to consider:
# 1. Was splitting the array into two classes really necessary?


from asyncio import gather
from collections.abc import Iterable
from dataclasses import dataclass, replace

import json
from typing import Any, Iterable, Literal
from typing import Any, Literal

import numpy as np
import numpy.typing as npt

from zarr.abc.codec import Codec
from zarr.abc.store import set_or_delete


from zarr.attributes import Attributes
from zarr.buffer import Factory, NDArrayLike, NDBuffer
from zarr.chunk_grids import RegularChunkGrid
from zarr.chunk_key_encodings import ChunkKeyEncoding, DefaultChunkKeyEncoding, V2ChunkKeyEncoding
from zarr.codecs import BytesCodec
from zarr.common import (
JSON,
Expand All @@ -36,11 +35,8 @@
concurrent_map,
)
from zarr.config import config

from zarr.indexing import BasicIndexer
from zarr.chunk_grids import RegularChunkGrid
from zarr.chunk_key_encodings import ChunkKeyEncoding, DefaultChunkKeyEncoding, V2ChunkKeyEncoding
from zarr.metadata import ArrayMetadata, ArrayV3Metadata, ArrayV2Metadata, parse_indexing_order
from zarr.metadata import ArrayMetadata, ArrayV2Metadata, ArrayV3Metadata, parse_indexing_order
from zarr.store import StoreLike, StorePath, make_store_path
from zarr.sync import sync

Expand Down
6 changes: 3 additions & 3 deletions src/zarr/attributes.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from __future__ import annotations

from collections.abc import MutableMapping
from typing import TYPE_CHECKING, Iterator
from collections.abc import Iterator, MutableMapping
from typing import TYPE_CHECKING

from zarr.common import JSON

if TYPE_CHECKING:
from zarr.group import Group
from zarr.array import Array
from zarr.group import Group


class Attributes(MutableMapping[str, JSON]):
Expand Down
14 changes: 6 additions & 8 deletions src/zarr/buffer.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
from __future__ import annotations

import sys
from collections.abc import Callable, Iterable
from typing import (
TYPE_CHECKING,
Any,
Callable,
Iterable,
Literal,
Optional,
Protocol,
Tuple,
TypeAlias,
)

import numpy as np

if TYPE_CHECKING:
from typing_extensions import Self

from zarr.codecs.bytes import Endian
from zarr.common import BytesLike

Expand Down Expand Up @@ -44,7 +42,7 @@ def __call__(
shape: Iterable[int],
dtype: np.DTypeLike,
order: Literal["C", "F"],
fill_value: Optional[Any],
fill_value: Any | None,
) -> NDBuffer:
"""Factory function to create a new NDBuffer (or subclass)

Expand Down Expand Up @@ -227,7 +225,7 @@ def __add__(self, other: Buffer) -> Self:
return self.__class__(np.concatenate((self._data, other_array)))

def __eq__(self, other: Any) -> bool:
if isinstance(other, (bytes, bytearray)):
if isinstance(other, bytes | bytearray):
# Many of the tests compares `Buffer` with `bytes` so we
# convert the bytes to a Buffer and try again
return self == self.from_bytes(other)
Expand Down Expand Up @@ -275,7 +273,7 @@ def create(
shape: Iterable[int],
dtype: np.DTypeLike,
order: Literal["C", "F"] = "C",
fill_value: Optional[Any] = None,
fill_value: Any | None = None,
) -> Self:
"""Create a new buffer and its underlying ndarray-like object

Expand Down Expand Up @@ -380,7 +378,7 @@ def dtype(self) -> np.dtype[Any]:
return self._data.dtype

@property
def shape(self) -> Tuple[int, ...]:
def shape(self) -> tuple[int, ...]:
return self._data.shape

@property
Expand Down
14 changes: 8 additions & 6 deletions src/zarr/chunk_grids.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

import itertools
from typing import TYPE_CHECKING, Any, Dict, Iterator
from collections.abc import Iterator
from dataclasses import dataclass
from zarr.abc.metadata import Metadata
from typing import TYPE_CHECKING, Any

from zarr.abc.metadata import Metadata
from zarr.common import (
JSON,
ChunkCoords,
Expand All @@ -20,7 +22,7 @@
@dataclass(frozen=True)
class ChunkGrid(Metadata):
@classmethod
def from_dict(cls, data: Dict[str, JSON]) -> ChunkGrid:
def from_dict(cls, data: dict[str, JSON]) -> ChunkGrid:
if isinstance(data, ChunkGrid):
return data

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

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

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

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

def all_chunk_coords(self, array_shape: ChunkCoords) -> Iterator[ChunkCoords]:
return itertools.product(
*(range(0, _ceildiv(s, c)) for s, c in zip(array_shape, self.chunk_shape))
*(range(0, _ceildiv(s, c)) for s, c in zip(array_shape, self.chunk_shape, strict=False))
)
9 changes: 5 additions & 4 deletions src/zarr/chunk_key_encodings.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

from abc import abstractmethod
from typing import TYPE_CHECKING, Dict, Literal, cast
from dataclasses import dataclass
from zarr.abc.metadata import Metadata
from typing import TYPE_CHECKING, Literal, cast

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

@classmethod
def from_dict(cls, data: Dict[str, JSON]) -> ChunkKeyEncoding:
def from_dict(cls, data: dict[str, JSON]) -> ChunkKeyEncoding:
if isinstance(data, ChunkKeyEncoding):
return data

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

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

@abstractmethod
Expand Down
4 changes: 2 additions & 2 deletions src/zarr/codecs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import annotations

from zarr.codecs.blosc import BloscCodec, BloscCname, BloscShuffle # noqa: F401
from zarr.codecs.blosc import BloscCname, BloscCodec, BloscShuffle # noqa: F401
from zarr.codecs.bytes import BytesCodec, Endian # noqa: F401
from zarr.codecs.crc32c_ import Crc32cCodec # noqa: F401
from zarr.codecs.gzip import GzipCodec # noqa: F401
from zarr.codecs.pipeline import BatchedCodecPipeline # noqa: F401
from zarr.codecs.sharding import ShardingCodec, ShardingCodecIndexLocation # noqa: F401
from zarr.codecs.transpose import TransposeCodec # noqa: F401
from zarr.codecs.zstd import ZstdCodec # noqa: F401
from zarr.codecs.pipeline import BatchedCodecPipeline # noqa: F401
6 changes: 3 additions & 3 deletions src/zarr/codecs/_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from dataclasses import dataclass

import numcodecs
from numcodecs.compat import ensure_bytes, ensure_ndarray

from zarr.buffer import Buffer, NDBuffer
from zarr.codecs.mixins import ArrayArrayCodecBatchMixin, ArrayBytesCodecBatchMixin
from zarr.common import JSON, ArraySpec, to_thread

import numcodecs
from numcodecs.compat import ensure_bytes, ensure_ndarray


@dataclass(frozen=True)
class V2Compressor(ArrayBytesCodecBatchMixin):
Expand Down
Loading
Loading