Skip to content

Commit 1b7273b

Browse files
committed
remove default parameters for parametric dtypes; add mixin classes for numpy dtypes; define zdtypelike
1 parent def5eb2 commit 1b7273b

File tree

5 files changed

+76
-69
lines changed

5 files changed

+76
-69
lines changed

src/zarr/api/synchronous.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
ShapeLike,
3838
ZarrFormat,
3939
)
40+
from zarr.core.dtype import ZDTypeLike
4041
from zarr.storage import StoreLike
4142

4243
__all__ = [
@@ -748,7 +749,7 @@ def create_array(
748749
*,
749750
name: str | None = None,
750751
shape: ShapeLike | None = None,
751-
dtype: npt.DTypeLike | None = None,
752+
dtype: ZDTypeLike | None = None,
752753
data: np.ndarray[Any, np.dtype[Any]] | None = None,
753754
chunks: ChunkCoords | Literal["auto"] = "auto",
754755
shards: ShardsLike | None = None,
@@ -778,7 +779,7 @@ def create_array(
778779
at the root of the store.
779780
shape : ChunkCoords, optional
780781
Shape of the array. Can be ``None`` if ``data`` is provided.
781-
dtype : npt.DTypeLike, optional
782+
dtype : ZDTypeLike, optional
782783
Data type of the array. Can be ``None`` if ``data`` is provided.
783784
data : np.ndarray, optional
784785
Array-like data to use for initializing the array. If this parameter is provided, the

src/zarr/core/array.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import numcodecs
2424
import numcodecs.abc
2525
import numpy as np
26-
import numpy.typing as npt
2726
from typing_extensions import deprecated
2827

2928
from zarr._compat import _deprecate_positional_args
@@ -67,6 +66,7 @@
6766
from zarr.core.config import config as zarr_config
6867
from zarr.core.dtype import (
6968
ZDType,
69+
ZDTypeLike,
7070
parse_data_type,
7171
)
7272
from zarr.core.indexing import (
@@ -122,6 +122,8 @@
122122
from collections.abc import Iterator, Sequence
123123
from typing import Self
124124

125+
import numpy.typing as npt
126+
125127
from zarr.abc.codec import CodecPipeline
126128
from zarr.codecs.sharding import ShardingCodecIndexLocation
127129
from zarr.core.dtype.wrapper import _BaseDType, _BaseScalar
@@ -295,7 +297,7 @@ async def create(
295297
*,
296298
# v2 and v3
297299
shape: ShapeLike,
298-
dtype: npt.DTypeLike,
300+
dtype: ZDTypeLike,
299301
zarr_format: Literal[2],
300302
fill_value: Any | None = None,
301303
attributes: dict[str, JSON] | None = None,
@@ -319,7 +321,7 @@ async def create(
319321
*,
320322
# v2 and v3
321323
shape: ShapeLike,
322-
dtype: npt.DTypeLike,
324+
dtype: ZDTypeLike,
323325
zarr_format: Literal[3],
324326
fill_value: Any | None = None,
325327
attributes: dict[str, JSON] | None = None,
@@ -347,7 +349,7 @@ async def create(
347349
*,
348350
# v2 and v3
349351
shape: ShapeLike,
350-
dtype: npt.DTypeLike,
352+
dtype: ZDTypeLike,
351353
zarr_format: Literal[3] = 3,
352354
fill_value: Any | None = None,
353355
attributes: dict[str, JSON] | None = None,
@@ -375,7 +377,7 @@ async def create(
375377
*,
376378
# v2 and v3
377379
shape: ShapeLike,
378-
dtype: npt.DTypeLike,
380+
dtype: ZDTypeLike,
379381
zarr_format: ZarrFormat,
380382
fill_value: Any | None = None,
381383
attributes: dict[str, JSON] | None = None,
@@ -410,7 +412,7 @@ async def create(
410412
*,
411413
# v2 and v3
412414
shape: ShapeLike,
413-
dtype: npt.DTypeLike,
415+
dtype: ZDTypeLike,
414416
zarr_format: ZarrFormat = 3,
415417
fill_value: Any | None = None,
416418
attributes: dict[str, JSON] | None = None,
@@ -446,7 +448,7 @@ async def create(
446448
The store where the array will be created.
447449
shape : ShapeLike
448450
The shape of the array.
449-
dtype : npt.DTypeLike
451+
dtype : ZDTypeLike
450452
The data type of the array.
451453
zarr_format : ZarrFormat, optional
452454
The Zarr format version (default is 3).
@@ -551,7 +553,7 @@ async def _create(
551553
*,
552554
# v2 and v3
553555
shape: ShapeLike,
554-
dtype: npt.DTypeLike | ZDType[_BaseDType, _BaseScalar],
556+
dtype: ZDTypeLike | ZDType[_BaseDType, _BaseScalar],
555557
zarr_format: ZarrFormat = 3,
556558
fill_value: Any | None = None,
557559
attributes: dict[str, JSON] | None = None,
@@ -1746,7 +1748,7 @@ def create(
17461748
*,
17471749
# v2 and v3
17481750
shape: ChunkCoords,
1749-
dtype: npt.DTypeLike,
1751+
dtype: ZDTypeLike,
17501752
zarr_format: ZarrFormat = 3,
17511753
fill_value: Any | None = None,
17521754
attributes: dict[str, JSON] | None = None,
@@ -1781,7 +1783,7 @@ def create(
17811783
The array store that has already been initialized.
17821784
shape : ChunkCoords
17831785
The shape of the array.
1784-
dtype : npt.DTypeLike
1786+
dtype : ZDTypeLike
17851787
The data type of the array.
17861788
chunk_shape : ChunkCoords, optional
17871789
The shape of the Array's chunks.
@@ -1875,7 +1877,7 @@ def _create(
18751877
*,
18761878
# v2 and v3
18771879
shape: ChunkCoords,
1878-
dtype: npt.DTypeLike,
1880+
dtype: ZDTypeLike,
18791881
zarr_format: ZarrFormat = 3,
18801882
fill_value: Any | None = None,
18811883
attributes: dict[str, JSON] | None = None,
@@ -3817,7 +3819,7 @@ async def init_array(
38173819
*,
38183820
store_path: StorePath,
38193821
shape: ShapeLike,
3820-
dtype: npt.DTypeLike,
3822+
dtype: ZDTypeLike,
38213823
chunks: ChunkCoords | Literal["auto"] = "auto",
38223824
shards: ShardsLike | None = None,
38233825
filters: FiltersLike = "auto",
@@ -3840,7 +3842,7 @@ async def init_array(
38403842
StorePath instance. The path attribute is the name of the array to initialize.
38413843
shape : ChunkCoords
38423844
Shape of the array.
3843-
dtype : npt.DTypeLike
3845+
dtype : ZDTypeLike
38443846
Data type of the array.
38453847
chunks : ChunkCoords, optional
38463848
Chunk shape of the array.
@@ -4028,7 +4030,7 @@ async def create_array(
40284030
*,
40294031
name: str | None = None,
40304032
shape: ShapeLike | None = None,
4031-
dtype: npt.DTypeLike | None = None,
4033+
dtype: ZDTypeLike | None = None,
40324034
data: np.ndarray[Any, np.dtype[Any]] | None = None,
40334035
chunks: ChunkCoords | Literal["auto"] = "auto",
40344036
shards: ShardsLike | None = None,
@@ -4057,7 +4059,7 @@ async def create_array(
40574059
at the root of the store.
40584060
shape : ChunkCoords, optional
40594061
Shape of the array. Can be ``None`` if ``data`` is provided.
4060-
dtype : npt.DTypeLike | None
4062+
dtype : ZDTypeLike | None
40614063
Data type of the array. Can be ``None`` if ``data`` is provided.
40624064
data : Array-like data to use for initializing the array. If this parameter is provided, the
40634065
``shape`` and ``dtype`` parameters must be identical to ``data.shape`` and ``data.dtype``,
@@ -4401,8 +4403,8 @@ def _parse_data_params(
44014403
*,
44024404
data: np.ndarray[Any, np.dtype[Any]] | None,
44034405
shape: ShapeLike | None,
4404-
dtype: npt.DTypeLike | None,
4405-
) -> tuple[np.ndarray[Any, np.dtype[Any]] | None, ShapeLike, npt.DTypeLike]:
4406+
dtype: ZDTypeLike | None,
4407+
) -> tuple[np.ndarray[Any, np.dtype[Any]] | None, ShapeLike, ZDTypeLike]:
44064408
"""
44074409
Ensure an array-like ``data`` parameter is consistent with the ``dtype`` and ``shape``
44084410
parameters.

src/zarr/core/dtype/__init__.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, Any, get_args
4-
5-
import numpy as np
6-
7-
from zarr.core.dtype._numpy import _NUMPY_SUPPORTS_VLEN_STRING
8-
from zarr.core.dtype.wrapper import _BaseDType, _BaseScalar
3+
from typing import TYPE_CHECKING, Any, TypeAlias, get_args
94

105
if TYPE_CHECKING:
11-
import numpy.typing as npt
12-
13-
from zarr.core.common import JSON, ZarrFormat
6+
from zarr.core.common import ZarrFormat
147

8+
import numpy as np
9+
import numpy.typing as npt
1510

11+
from zarr.core.common import JSON
1612
from zarr.core.dtype._numpy import (
13+
_NUMPY_SUPPORTS_VLEN_STRING,
1714
Bool,
1815
Complex64,
1916
Complex128,
@@ -36,7 +33,7 @@
3633
VariableLengthString,
3734
)
3835
from zarr.core.dtype.registry import DataTypeRegistry
39-
from zarr.core.dtype.wrapper import ZDType
36+
from zarr.core.dtype.wrapper import ZDType, _BaseDType, _BaseScalar
4037

4138
__all__ = [
4239
"Complex64",
@@ -80,6 +77,8 @@
8077
| DateTime64
8178
)
8279

80+
ZDTypeLike: TypeAlias = npt.DTypeLike | ZDType[Any, Any] | dict[str, JSON]
81+
8382
for dtype in get_args(DTYPE):
8483
data_type_registry.register(dtype._zarr_v3_name, dtype)
8584

@@ -112,9 +111,10 @@ def get_data_type_from_json(
112111
return data_type_registry.match_json(dtype, zarr_format=zarr_format)
113112

114113

115-
def parse_data_type(
116-
dtype: npt.DTypeLike | ZDType[Any, Any] | dict[str, JSON], zarr_format: ZarrFormat
117-
) -> ZDType[Any, Any]:
114+
def parse_data_type(dtype: ZDTypeLike, zarr_format: ZarrFormat) -> ZDType[Any, Any]:
115+
"""
116+
Interpret the input as a ZDType instance.
117+
"""
118118
if isinstance(dtype, ZDType):
119119
return dtype
120120
elif isinstance(dtype, dict):

0 commit comments

Comments
 (0)