Skip to content
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

[v3] Disallow generic Any typing #1794

Merged
merged 3 commits into from
Apr 22, 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
16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ strict_concatenate = true

check_untyped_defs = true
disallow_untyped_decorators = true
disallow_any_generics = true

[[tool.mypy.overrides]]
module = [
Expand All @@ -176,6 +177,21 @@ module = [
]
check_untyped_defs = false

[[tool.mypy.overrides]]
module = [
"zarr.v3.abc.codec",
"zarr.v3.codecs.bytes",
"zarr.v3.codecs.pipeline",
"zarr.v3.codecs.sharding",
"zarr.v3.codecs.transpose",
"zarr.v3.array_v2",
"zarr.v3.array",
"zarr.v3.sync",
"zarr.convenience",
"zarr.meta",
]
disallow_any_generics = false


[tool.pytest.ini_options]
doctest_optionflags = [
Expand Down
4 changes: 2 additions & 2 deletions src/zarr/_storage/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
DEFAULT_ZARR_VERSION = 2


class BaseStore(MutableMapping):
class BaseStore(MutableMapping[str, Any]):
"""Abstract base class for store implementations.

This is a thin wrapper over MutableMapping that provides methods to check
Expand Down Expand Up @@ -165,7 +165,7 @@ def rmdir(self, path: str = "") -> None:


# allow MutableMapping for backwards compatibility
StoreLike = Union[BaseStore, MutableMapping]
StoreLike = Union[BaseStore, MutableMapping[str, Any]]


def _path_to_prefix(path: Optional[str]) -> str:
Expand Down
2 changes: 1 addition & 1 deletion src/zarr/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from zarr.util import json_dumps


class Attributes(MutableMapping):
class Attributes(MutableMapping[str, Any]):
"""Class providing access to user attributes on an array or group. Should not be
instantiated directly, will be available via the `.attrs` property of an array or
group.
Expand Down
4 changes: 2 additions & 2 deletions src/zarr/convenience.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
)
from zarr.util import TreeViewer, buffer_size, normalize_storage_path

from typing import Union
from typing import Any, Union

StoreLike = Union[BaseStore, MutableMapping, str, None]
StoreLike = Union[BaseStore, MutableMapping[str, Any], str, None]

_builtin_open = open # builtin open is later shadowed by a local open function

Expand Down
3 changes: 2 additions & 1 deletion src/zarr/hierarchy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections.abc import MutableMapping
from itertools import islice
from typing import Any

import numpy as np

Expand Down Expand Up @@ -48,7 +49,7 @@
)


class Group(MutableMapping):
class Group(MutableMapping[str, Any]):
"""Instantiate a group from an initialized store.

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

Path = Union[str, bytes, None]
# allow MutableMapping for backwards compatibility
StoreLike = Union[BaseStore, MutableMapping]
StoreLike = Union[BaseStore, MutableMapping[str, Any]]


def contains_array(store: StoreLike, path: Path = None) -> bool:
Expand Down Expand Up @@ -202,7 +202,7 @@ def listdir(store: BaseStore, path: Path = None):

def _getsize(store: BaseStore, path: Path = None) -> int:
# compute from size of values
if path and path in store:
if isinstance(path, str) and path in store:
v = store[path]
size = buffer_size(v)
else:
Expand Down Expand Up @@ -584,7 +584,7 @@ def _init_group_metadata(
store[key] = encode_group_metadata(meta)


def _dict_store_keys(d: Dict, prefix="", cls=dict):
def _dict_store_keys(d: dict[str, Any], prefix="", cls=dict):
for k in d.keys():
v = d[k]
if isinstance(v, cls):
Expand Down
9 changes: 5 additions & 4 deletions src/zarr/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)

import numpy as np
import numpy.typing as npt
from asciitree import BoxStyle, LeftAligned
from asciitree.traversal import Traversal
from numcodecs.compat import (
Expand All @@ -36,7 +37,7 @@
ValueType = TypeVar("ValueType")


def flatten(arg: Iterable) -> Iterable:
def flatten(arg: Iterable[Any]) -> Iterable[Any]:
for element in arg:
if isinstance(element, Iterable) and not isinstance(element, (str, bytes)):
yield from flatten(element)
Expand Down Expand Up @@ -179,7 +180,7 @@ def normalize_chunks(chunks: Any, shape: Tuple[int, ...], typesize: int) -> Tupl
return chunks


def normalize_dtype(dtype: Union[str, np.dtype], object_codec) -> Tuple[np.dtype, Any]:
def normalize_dtype(dtype: Union[str, npt.DTypeLike], object_codec) -> Tuple[np.dtype[Any], Any]:
# convenience API for object arrays
if inspect.isclass(dtype):
dtype = dtype.__name__
Expand Down Expand Up @@ -291,7 +292,7 @@ def normalize_dimension_separator(sep: Optional[str]) -> Optional[str]:
raise ValueError("dimension_separator must be either '.' or '/', found: %r" % sep)


def normalize_fill_value(fill_value, dtype: np.dtype):
def normalize_fill_value(fill_value, dtype: np.dtype[Any]):
if fill_value is None or dtype.hasobject:
# no fill value
pass
Expand Down Expand Up @@ -668,7 +669,7 @@ def read_full(self):


def retry_call(
callabl: Callable,
callabl: Callable[..., Any],
args=None,
kwargs=None,
exceptions: Tuple[Any, ...] = (),
Expand Down
10 changes: 5 additions & 5 deletions src/zarr/v3/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Union, Tuple, Iterable, Dict, List, TypeVar, overload
from typing import TYPE_CHECKING, Union, Tuple, Iterable, Dict, List, TypeVar, overload, Any
import asyncio
import contextvars
from dataclasses import dataclass
Expand Down Expand Up @@ -28,7 +28,7 @@ def product(tup: ChunkCoords) -> int:
return functools.reduce(lambda x, y: x * y, tup, 1)


T = TypeVar("T", bound=Tuple)
T = TypeVar("T", bound=Tuple[Any, ...])
V = TypeVar("V")


Expand Down Expand Up @@ -76,7 +76,7 @@ def parse_enum(data: JSON, cls: Type[E]) -> E:
@dataclass(frozen=True)
class ArraySpec:
shape: ChunkCoords
dtype: np.dtype
dtype: np.dtype[Any]
fill_value: Any

def __init__(self, shape, dtype, fill_value):
Expand All @@ -102,7 +102,7 @@ def parse_name(data: JSON, expected: Optional[str] = None) -> str:
raise TypeError(f"Expected a string, got an instance of {type(data)}.")


def parse_configuration(data: JSON) -> dict:
def parse_configuration(data: JSON) -> JSON:
if not isinstance(data, dict):
raise TypeError(f"Expected dict, got {type(data)}")
return data
Expand Down Expand Up @@ -153,7 +153,7 @@ def parse_shapelike(data: Any) -> Tuple[int, ...]:
return data_tuple


def parse_dtype(data: Any) -> np.dtype:
def parse_dtype(data: Any) -> np.dtype[Any]:
# todo: real validation
return np.dtype(data)

Expand Down
11 changes: 6 additions & 5 deletions src/zarr/v3/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import dataclass, field
import json
import numpy as np
import numpy.typing as npt

from zarr.v3.chunk_grids import ChunkGrid, RegularChunkGrid
from zarr.v3.chunk_key_encodings import ChunkKeyEncoding, parse_separator
Expand Down Expand Up @@ -90,7 +91,7 @@ def to_numpy_shortname(self) -> str:
return data_type_to_numpy[self]

@classmethod
def from_dtype(cls, dtype: np.dtype) -> DataType:
def from_dtype(cls, dtype: np.dtype[Any]) -> DataType:
dtype_to_data_type = {
"|b1": "bool",
"bool": "bool",
Expand All @@ -111,7 +112,7 @@ def from_dtype(cls, dtype: np.dtype) -> DataType:
@dataclass(frozen=True)
class ArrayMetadata(Metadata):
shape: ChunkCoords
data_type: np.dtype
data_type: np.dtype[Any]
chunk_grid: ChunkGrid
chunk_key_encoding: ChunkKeyEncoding
fill_value: Any
Expand Down Expand Up @@ -176,7 +177,7 @@ def _validate_metadata(self) -> None:
self.codecs.validate(self)

@property
def dtype(self) -> np.dtype:
def dtype(self) -> np.dtype[Any]:
return self.data_type

@property
Expand Down Expand Up @@ -238,7 +239,7 @@ def to_dict(self) -> Dict[str, Any]:
class ArrayV2Metadata(Metadata):
shape: ChunkCoords
chunks: ChunkCoords
dtype: np.dtype
dtype: np.dtype[Any]
fill_value: Union[None, int, float] = 0
order: Literal["C", "F"] = "C"
filters: Optional[List[Dict[str, Any]]] = None
Expand All @@ -251,7 +252,7 @@ def __init__(
self,
*,
shape: ChunkCoords,
dtype: np.dtype,
dtype: npt.DTypeLike,
chunks: ChunkCoords,
fill_value: Any,
order: Literal["C", "F"],
Expand Down
Loading