Skip to content

Commit 5642e03

Browse files
committed
Consistent typing
1 parent 3327cc1 commit 5642e03

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

src/zarr/api/synchronous.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from zarr.errors import ZarrDeprecationWarning
1414

1515
if TYPE_CHECKING:
16-
from collections.abc import Iterable, Sequence
16+
from collections.abc import Iterable
1717

1818
import numpy as np
1919
import numpy.typing as npt
@@ -33,6 +33,7 @@
3333
from zarr.core.common import (
3434
JSON,
3535
AccessModeLiteral,
36+
ChunksLike,
3637
DimensionNamesLike,
3738
MemoryOrder,
3839
ShapeLike,
@@ -822,7 +823,7 @@ def create_array(
822823
shape: ShapeLike | None = None,
823824
dtype: ZDTypeLike | None = None,
824825
data: np.ndarray[Any, np.dtype[Any]] | None = None,
825-
chunks: tuple[int, ...] | Sequence[Sequence[int]] | Literal["auto"] = "auto",
826+
chunks: ChunksLike | Literal["auto"] = "auto",
826827
shards: ShardsLike | None = None,
827828
filters: FiltersLike = "auto",
828829
compressors: CompressorsLike = "auto",
@@ -997,7 +998,7 @@ def from_array(
997998
data: AnyArray | npt.ArrayLike,
998999
write_data: bool = True,
9991000
name: str | None = None,
1000-
chunks: Literal["auto", "keep"] | tuple[int, ...] | Sequence[Sequence[int]] = "keep",
1001+
chunks: ChunksLike | Literal["auto", "keep"] = "keep",
10011002
shards: ShardsLike | None | Literal["keep"] = "keep",
10021003
filters: FiltersLike | Literal["keep"] = "keep",
10031004
compressors: CompressorsLike | Literal["keep"] = "keep",

src/zarr/core/array.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
ZARR_JSON,
5656
ZARRAY_JSON,
5757
ZATTRS_JSON,
58+
ChunksLike,
5859
DimensionNamesLike,
5960
MemoryOrder,
6061
ShapeLike,
@@ -2828,7 +2829,7 @@ def __array__(
28282829
raise ValueError(msg)
28292830

28302831
arr = self[...]
2831-
arr_np: NDArrayLike = np.array(arr, dtype=dtype)
2832+
arr_np = np.array(arr, dtype=dtype)
28322833

28332834
if dtype is not None:
28342835
arr_np = arr_np.astype(dtype)
@@ -4411,7 +4412,7 @@ async def from_array(
44114412
data: AnyArray | npt.ArrayLike,
44124413
write_data: bool = True,
44134414
name: str | None = None,
4414-
chunks: Literal["auto", "keep"] | tuple[int, ...] | Sequence[Sequence[int]] = "keep",
4415+
chunks: ChunksLike | Literal["auto", "keep"] = "keep",
44154416
shards: ShardsLike | None | Literal["keep"] = "keep",
44164417
filters: FiltersLike | Literal["keep"] = "keep",
44174418
compressors: CompressorsLike | Literal["keep"] = "keep",
@@ -4684,7 +4685,7 @@ async def init_array(
46844685
store_path: StorePath,
46854686
shape: ShapeLike,
46864687
dtype: ZDTypeLike,
4687-
chunks: tuple[int, ...] | Sequence[Sequence[int]] | Literal["auto"] = "auto",
4688+
chunks: ChunksLike | Literal["auto"] = "auto",
46884689
shards: ShardsLike | None = None,
46894690
filters: FiltersLike = "auto",
46904691
compressors: CompressorsLike = "auto",
@@ -4821,6 +4822,9 @@ async def init_array(
48214822
# Use first chunk size per dim as placeholder for _auto_partition
48224823
chunks_flat: tuple[int, ...] | Literal["auto"] = tuple(dim_edges[0] for dim_edges in chunks)
48234824
else:
4825+
# Normalize scalar int to per-dimension tuple (e.g. chunks=100000 for a 1D array)
4826+
if isinstance(chunks, int):
4827+
chunks = tuple(chunks for _ in shape_parsed)
48244828
chunks_flat = cast("tuple[int, ...] | Literal['auto']", chunks)
48254829

48264830
# Handle rectilinear shards: shards=[[60, 40, 20], [50, 50]]
@@ -4945,7 +4949,7 @@ async def create_array(
49454949
shape: ShapeLike | None = None,
49464950
dtype: ZDTypeLike | None = None,
49474951
data: np.ndarray[Any, np.dtype[Any]] | None = None,
4948-
chunks: tuple[int, ...] | Sequence[Sequence[int]] | Literal["auto"] = "auto",
4952+
chunks: ChunksLike | Literal["auto"] = "auto",
49494953
shards: ShardsLike | None = None,
49504954
filters: FiltersLike = "auto",
49514955
compressors: CompressorsLike = "auto",
@@ -5134,7 +5138,7 @@ async def create_array(
51345138

51355139
def _parse_keep_array_attr(
51365140
data: AnyArray | npt.ArrayLike,
5137-
chunks: Literal["auto", "keep"] | tuple[int, ...] | Sequence[Sequence[int]],
5141+
chunks: ChunksLike | Literal["auto", "keep"],
51385142
shards: ShardsLike | None | Literal["keep"],
51395143
filters: FiltersLike | Literal["keep"],
51405144
compressors: CompressorsLike | Literal["keep"],
@@ -5145,7 +5149,7 @@ def _parse_keep_array_attr(
51455149
chunk_key_encoding: ChunkKeyEncodingLike | None,
51465150
dimension_names: DimensionNamesLike,
51475151
) -> tuple[
5148-
tuple[int, ...] | Sequence[Sequence[int]] | Literal["auto"],
5152+
ChunksLike | Literal["auto"],
51495153
ShardsLike | None,
51505154
FiltersLike,
51515155
CompressorsLike,
@@ -5215,8 +5219,10 @@ def _parse_keep_array_attr(
52155219
compressors = "auto"
52165220
if serializer == "keep":
52175221
serializer = "auto"
5222+
# After resolving "keep" above, chunks is never "keep" at this point.
5223+
chunks_out: ChunksLike | Literal["auto"] = chunks # type: ignore[assignment]
52185224
return (
5219-
chunks,
5225+
chunks_out,
52205226
shards,
52215227
filters,
52225228
compressors,

src/zarr/core/group.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
ZATTRS_JSON,
4141
ZGROUP_JSON,
4242
ZMETADATA_V2_JSON,
43+
ChunksLike,
4344
DimensionNamesLike,
4445
NodeType,
4546
ShapeLike,
@@ -71,7 +72,6 @@
7172
Iterable,
7273
Iterator,
7374
Mapping,
74-
Sequence,
7575
)
7676
from typing import Any
7777

@@ -1021,7 +1021,7 @@ async def create_array(
10211021
shape: ShapeLike | None = None,
10221022
dtype: ZDTypeLike | None = None,
10231023
data: np.ndarray[Any, np.dtype[Any]] | None = None,
1024-
chunks: tuple[int, ...] | Sequence[Sequence[int]] | Literal["auto"] = "auto",
1024+
chunks: ChunksLike | Literal["auto"] = "auto",
10251025
shards: ShardsLike | None = None,
10261026
filters: FiltersLike = "auto",
10271027
compressors: CompressorsLike = "auto",
@@ -2474,7 +2474,7 @@ def create(
24742474
shape: ShapeLike | None = None,
24752475
dtype: ZDTypeLike | None = None,
24762476
data: np.ndarray[Any, np.dtype[Any]] | None = None,
2477-
chunks: tuple[int, ...] | Sequence[Sequence[int]] | Literal["auto"] = "auto",
2477+
chunks: ChunksLike | Literal["auto"] = "auto",
24782478
shards: ShardsLike | None = None,
24792479
filters: FiltersLike = "auto",
24802480
compressors: CompressorsLike = "auto",
@@ -2618,7 +2618,7 @@ def create_array(
26182618
shape: ShapeLike | None = None,
26192619
dtype: ZDTypeLike | None = None,
26202620
data: np.ndarray[Any, np.dtype[Any]] | None = None,
2621-
chunks: tuple[int, ...] | Sequence[Sequence[int]] | Literal["auto"] = "auto",
2621+
chunks: ChunksLike | Literal["auto"] = "auto",
26222622
shards: ShardsLike | None = None,
26232623
filters: FiltersLike = "auto",
26242624
compressors: CompressorsLike = "auto",
@@ -3016,7 +3016,7 @@ def array(
30163016
*,
30173017
shape: ShapeLike,
30183018
dtype: npt.DTypeLike,
3019-
chunks: tuple[int, ...] | Sequence[Sequence[int]] | Literal["auto"] = "auto",
3019+
chunks: ChunksLike | Literal["auto"] = "auto",
30203020
shards: tuple[int, ...] | Literal["auto"] | None = None,
30213021
filters: FiltersLike = "auto",
30223022
compressors: CompressorsLike = "auto",

0 commit comments

Comments
 (0)