Skip to content

Commit

Permalink
fix mypy errors around numpy functions not being strictly type hinted (
Browse files Browse the repository at this point in the history
  • Loading branch information
TomNicholas authored Oct 11, 2024
1 parent fadeeba commit 2d66e88
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
21 changes: 14 additions & 7 deletions virtualizarr/manifests/array_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Callable, Iterable
from typing import TYPE_CHECKING, Any, Callable, Iterable, cast

import numpy as np

Expand Down Expand Up @@ -217,9 +217,12 @@ def stack(
new_shape.insert(axis, length_along_new_stacked_axis)

# do stacking of entries in manifest
stacked_paths = np.stack(
[arr.manifest._paths for arr in arrays],
axis=axis,
stacked_paths = cast( # `np.stack` apparently is type hinted as if the output could have Any dtype
np.ndarray[Any, np.dtypes.StringDType],
np.stack(
[arr.manifest._paths for arr in arrays],
axis=axis,
),
)
stacked_offsets = np.stack(
[arr.manifest._offsets for arr in arrays],
Expand Down Expand Up @@ -296,10 +299,14 @@ def broadcast_to(x: "ManifestArray", /, shape: tuple[int, ...]) -> "ManifestArra
)

# do broadcasting of entries in manifest
broadcasted_paths = np.broadcast_to(
x.manifest._paths,
shape=new_chunk_grid_shape,
broadcasted_paths = cast( # `np.broadcast_to` apparently is type hinted as if the output could have Any dtype
np.ndarray[Any, np.dtypes.StringDType],
np.broadcast_to(
x.manifest._paths,
shape=new_chunk_grid_shape,
),
)

broadcasted_offsets = np.broadcast_to(
x.manifest._offsets,
shape=new_chunk_grid_shape,
Expand Down
9 changes: 6 additions & 3 deletions virtualizarr/manifests/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ChunkManifest:
so it's not possible to have a ChunkManifest object that does not represent a valid grid of chunks.
"""

_paths: np.ndarray[Any, np.dtypes.StringDType] # type: ignore[name-defined]
_paths: np.ndarray[Any, np.dtypes.StringDType]
_offsets: np.ndarray[Any, np.dtype[np.uint64]]
_lengths: np.ndarray[Any, np.dtype[np.uint64]]

Expand Down Expand Up @@ -113,7 +113,10 @@ def __init__(self, entries: dict) -> None:
shape = get_chunk_grid_shape(entries.keys())

# Initializing to empty implies that entries with path='' are treated as missing chunks
paths = np.empty(shape=shape, dtype=np.dtypes.StringDType()) # type: ignore[attr-defined]
paths = cast( # `np.empty` apparently is type hinted as if the output could have Any dtype
np.ndarray[Any, np.dtypes.StringDType],
np.empty(shape=shape, dtype=np.dtypes.StringDType()),
)
offsets = np.empty(shape=shape, dtype=np.dtype("uint64"))
lengths = np.empty(shape=shape, dtype=np.dtype("uint64"))

Expand Down Expand Up @@ -141,7 +144,7 @@ def __init__(self, entries: dict) -> None:
@classmethod
def from_arrays(
cls,
paths: np.ndarray[Any, np.dtype[np.dtypes.StringDType]], # type: ignore[name-defined]
paths: np.ndarray[Any, np.dtypes.StringDType],
offsets: np.ndarray[Any, np.dtype[np.uint64]],
lengths: np.ndarray[Any, np.dtype[np.uint64]],
) -> "ChunkManifest":
Expand Down

0 comments on commit 2d66e88

Please sign in to comment.