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

Make typing strict #1879

Merged
merged 6 commits into from
Jun 1, 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: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ repos:
- types-setuptools
- pytest
- numpy
- numcodecs
- zstandard
23 changes: 8 additions & 15 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,7 @@ python_version = "3.10"
ignore_missing_imports = true
namespace_packages = false

warn_unused_configs = true
warn_redundant_casts = true
warn_unused_ignores = true
strict_equality = true
strict_concatenate = true

check_untyped_defs = true
disallow_untyped_decorators = true
disallow_any_generics = true

disallow_incomplete_defs = true
disallow_untyped_calls = true

disallow_untyped_defs = true
no_implicit_reexport = true
strict = true


[[tool.mypy.overrides]]
Expand Down Expand Up @@ -238,6 +224,13 @@ module = [
disallow_untyped_defs = false


[[tool.mypy.overrides]]
module = [
"zarr.metadata",
"zarr.store.remote"
]
warn_return_any = false

[tool.pytest.ini_options]
minversion = "7"
testpaths = ["tests"]
Expand Down
2 changes: 1 addition & 1 deletion src/zarr/codecs/sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def is_all_empty(self) -> bool:
return bool(np.array_equiv(self.offsets_and_lengths, MAX_UINT_64))

def get_full_chunk_map(self) -> npt.NDArray[np.bool_]:
return self.offsets_and_lengths[..., 0] != MAX_UINT_64
return np.not_equal(self.offsets_and_lengths[..., 0], MAX_UINT_64)

def get_chunk_slice(self, chunk_coords: ChunkCoords) -> tuple[int, int] | None:
localized_chunk = self._localize_chunk(chunk_coords)
Expand Down
4 changes: 2 additions & 2 deletions src/zarr/codecs/zstd.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ def to_dict(self) -> dict[str, JSON]:

def _compress(self, data: npt.NDArray[Any]) -> bytes:
ctx = ZstdCompressor(level=self.level, write_checksum=self.checksum)
return ctx.compress(data)
return ctx.compress(data.tobytes())

def _decompress(self, data: npt.NDArray[Any]) -> bytes:
ctx = ZstdDecompressor()
return ctx.decompress(data)
return ctx.decompress(data.tobytes())

async def _decode_single(
self,
Expand Down
12 changes: 10 additions & 2 deletions src/zarr/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
from collections.abc import Iterable
from dataclasses import dataclass
from enum import Enum
from typing import TYPE_CHECKING, Any, Literal, ParamSpec, TypeVar, overload
from typing import (
TYPE_CHECKING,
Any,
Literal,
ParamSpec,
TypeVar,
cast,
overload,
)

if TYPE_CHECKING:
from collections.abc import Awaitable, Callable, Iterator
Expand Down Expand Up @@ -178,5 +186,5 @@ def parse_fill_value(data: Any) -> Any:

def parse_order(data: Any) -> Literal["C", "F"]:
if data in ("C", "F"):
return data
return cast(Literal["C", "F"], data)
raise ValueError(f"Expected one of ('C', 'F'), got {data} instead.")
4 changes: 2 additions & 2 deletions src/zarr/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Any, Literal
from typing import Any, Literal, cast

from donfig import Config

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

def parse_indexing_order(data: Any) -> Literal["C", "F"]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noting that this function also exists in zarr.common... No need to fix here but we don't need both.

if data in ("C", "F"):
return data
return cast(Literal["C", "F"], data)
msg = f"Expected one of ('C', 'F'), got {data} instead."
raise ValueError(msg)
4 changes: 2 additions & 2 deletions src/zarr/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
from collections.abc import Iterator
from dataclasses import asdict, dataclass, field, replace
from typing import TYPE_CHECKING, overload
from typing import TYPE_CHECKING, Literal, cast, overload

import numpy.typing as npt

Expand Down Expand Up @@ -37,7 +37,7 @@

def parse_zarr_format(data: Any) -> ZarrFormat:
if data in (2, 3):
return data
return cast(Literal[2, 3], data)
msg = msg = f"Invalid zarr_format. Expected one 2 or 3. Got {data}."
raise ValueError(msg)

Expand Down
2 changes: 1 addition & 1 deletion src/zarr/v2/n5.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ def compressor_config_to_zarr(compressor_config: Dict[str, Any]) -> Optional[Dic
return zarr_config


class N5ChunkWrapper(Codec):
class N5ChunkWrapper(Codec): # type: ignore[misc]
codec_id = "n5_wrapper"

def __init__(self, dtype, chunk_shape, compressor_config=None, compressor=None):
Expand Down
2 changes: 1 addition & 1 deletion src/zarr/v2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def get_type(self):
return type(self.obj).__name__


class TreeTraversal(Traversal):
class TreeTraversal(Traversal): # type: ignore[misc]
def get_children(self, node):
return node.get_children()

Expand Down
Loading