|
1 | 1 | """Public type definitions and constants"""
|
2 | 2 |
|
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections.abc import Iterable, Mapping, Sequence |
| 6 | +from os import PathLike |
| 7 | +from typing import TYPE_CHECKING, Any, Literal, TypeAlias, TypeVar |
| 8 | + |
| 9 | +# Import core types from zarr.core.types |
3 | 10 | from zarr.core.types import (
|
| 11 | + JSON, |
| 12 | + AccessModeLiteral, |
4 | 13 | ArrayMetadataJSON_V2,
|
5 | 14 | ArrayMetadataJSON_V3,
|
| 15 | + BytesLike, |
| 16 | + ChunkCoords, |
| 17 | + ChunkCoordsLike, |
| 18 | + DimensionNames, |
6 | 19 | GroupMetadataJSON_V2,
|
7 | 20 | GroupMetadataJSON_V3,
|
| 21 | + MemoryOrder, |
| 22 | + NodeType, |
| 23 | + ShapeLike, |
8 | 24 | ZarrFormat,
|
9 | 25 | )
|
10 | 26 |
|
| 27 | +# Additional types needed for comprehensive documentation linking |
| 28 | +# These extend the minimal set from zarr.core.types |
| 29 | + |
| 30 | +# Import numpy types for proper typing |
| 31 | +if TYPE_CHECKING: |
| 32 | + import numpy as np |
| 33 | + import numpy.typing as npt |
| 34 | + |
| 35 | +# Core classes - forward references for main API classes |
| 36 | +if TYPE_CHECKING: |
| 37 | + from zarr.core.array import Array as _Array |
| 38 | + from zarr.core.array import AsyncArray as _AsyncArray |
| 39 | + from zarr.core.common import DefaultFillValue as _DefaultFillValue |
| 40 | + from zarr.core.dtype import ZDType as _ZDType |
| 41 | + from zarr.core.group import AsyncGroup as _AsyncGroup |
| 42 | + from zarr.core.group import Group as _Group |
| 43 | + |
| 44 | + Array: TypeAlias = _Array[Any] |
| 45 | + AsyncArray: TypeAlias = _AsyncArray[Any] |
| 46 | + Group: TypeAlias = _Group |
| 47 | + AsyncGroup: TypeAlias = _AsyncGroup |
| 48 | + DefaultFillValue: TypeAlias = _DefaultFillValue |
| 49 | + ZDType: TypeAlias = _ZDType |
| 50 | +else: |
| 51 | + # Import at runtime for autoapi to resolve |
| 52 | + try: |
| 53 | + from zarr.core.array import Array, AsyncArray |
| 54 | + from zarr.core.common import DefaultFillValue |
| 55 | + from zarr.core.dtype import ZDType |
| 56 | + from zarr.core.group import AsyncGroup, Group |
| 57 | + except ImportError: |
| 58 | + # Fallback to TypeVar placeholders |
| 59 | + Array = TypeVar("Array") |
| 60 | + AsyncArray = TypeVar("AsyncArray") |
| 61 | + Group = TypeVar("Group") |
| 62 | + AsyncGroup = TypeVar("AsyncGroup") |
| 63 | + DefaultFillValue = TypeVar("DefaultFillValue") |
| 64 | + ZDType = TypeVar("ZDType") |
| 65 | + |
| 66 | +# Store types with forward references |
| 67 | +if TYPE_CHECKING: |
| 68 | + from zarr.abc.store import Store as _Store |
| 69 | + from zarr.storage import StorePath as _StorePath |
| 70 | + |
| 71 | + Store: TypeAlias = _Store |
| 72 | + StorePath: TypeAlias = _StorePath |
| 73 | +else: |
| 74 | + # Import at runtime for autoapi to resolve |
| 75 | + Store = TypeVar("Store") |
| 76 | + StorePath = TypeVar("StorePath") |
| 77 | + |
| 78 | +StoreLike: TypeAlias = Store | StorePath | PathLike[str] | str | dict[str, Any] | Mapping[str, Any] |
| 79 | +"""Store-like object. Can be a Store, StorePath, Path, str, or dict.""" |
| 80 | + |
| 81 | +# Array types with proper numpy typing |
| 82 | +if TYPE_CHECKING: |
| 83 | + NDArrayLike: TypeAlias = npt.NDArray[Any] | np.generic |
| 84 | +else: |
| 85 | + NDArrayLike = TypeVar("NDArrayLike") |
| 86 | + |
| 87 | +NDArrayLikeOrScalar: TypeAlias = NDArrayLike | Any |
| 88 | +ArrayLike: TypeAlias = Sequence[Any] | NDArrayLike |
| 89 | + |
| 90 | +# ZDType - special handling for strong typing |
| 91 | +if TYPE_CHECKING: |
| 92 | + from zarr.core.dtype import ZDType as _ZDTypeRuntime |
| 93 | + |
| 94 | + ZDTypeLike: TypeAlias = _ZDTypeRuntime | Any |
| 95 | +else: |
| 96 | + ZDTypeLike = TypeVar("ZDTypeLike") |
| 97 | + |
| 98 | +# Chunk key encoding types |
| 99 | +if TYPE_CHECKING: |
| 100 | + from zarr.core.chunk_key_encodings import ChunkKeyEncoding as _ChunkKeyEncoding |
| 101 | + from zarr.core.chunk_key_encodings import ChunkKeyEncodingParams as _ChunkKeyEncodingParams |
| 102 | + |
| 103 | + ChunkKeyEncodingLike: TypeAlias = _ChunkKeyEncodingParams | _ChunkKeyEncoding |
| 104 | + ChunkKeyEncoding: TypeAlias = _ChunkKeyEncoding |
| 105 | + ChunkKeyEncodingParams: TypeAlias = _ChunkKeyEncodingParams |
| 106 | +else: |
| 107 | + ChunkKeyEncodingLike = TypeVar("ChunkKeyEncodingLike") |
| 108 | + ChunkKeyEncoding = TypeVar("ChunkKeyEncoding") |
| 109 | + ChunkKeyEncodingParams = TypeVar("ChunkKeyEncodingParams") |
| 110 | + |
| 111 | +# Array config types |
| 112 | +if TYPE_CHECKING: |
| 113 | + from zarr.core.array_spec import ArrayConfig as _ArrayConfig |
| 114 | + from zarr.core.array_spec import ArrayConfigLike as _ArrayConfigLike |
| 115 | + |
| 116 | + ArrayConfig: TypeAlias = _ArrayConfig |
| 117 | + ArrayConfigLike: TypeAlias = _ArrayConfigLike |
| 118 | +else: |
| 119 | + try: |
| 120 | + from zarr.core.array_spec import ArrayConfig, ArrayConfigLike |
| 121 | + except ImportError: |
| 122 | + ArrayConfig = TypeVar("ArrayConfig") |
| 123 | + ArrayConfigLike = TypeVar("ArrayConfigLike") |
| 124 | + |
| 125 | +# Codec class types |
| 126 | +if TYPE_CHECKING: |
| 127 | + from zarr.abc.codec import ArrayArrayCodec as _ArrayArrayCodec |
| 128 | + from zarr.abc.codec import ArrayBytesCodec as _ArrayBytesCodec |
| 129 | + from zarr.abc.codec import BaseCodec as _BaseCodec |
| 130 | + from zarr.abc.codec import BytesBytesCodec as _BytesBytesCodec |
| 131 | + from zarr.abc.codec import CodecPipeline as _CodecPipeline |
| 132 | + |
| 133 | + BaseCodec: TypeAlias = _BaseCodec[Any, Any] |
| 134 | + ArrayArrayCodec: TypeAlias = _ArrayArrayCodec[Any, Any] |
| 135 | + ArrayBytesCodec: TypeAlias = _ArrayBytesCodec[Any] |
| 136 | + BytesBytesCodec: TypeAlias = _BytesBytesCodec |
| 137 | + CodecPipeline: TypeAlias = _CodecPipeline[Any] |
| 138 | + Codec: TypeAlias = _BaseCodec[Any, Any] |
| 139 | +else: |
| 140 | + try: |
| 141 | + from zarr.abc.codec import ( |
| 142 | + ArrayArrayCodec, |
| 143 | + ArrayBytesCodec, |
| 144 | + BaseCodec, |
| 145 | + BytesBytesCodec, |
| 146 | + CodecPipeline, |
| 147 | + ) |
| 148 | + |
| 149 | + Codec = BaseCodec |
| 150 | + except ImportError: |
| 151 | + BaseCodec = TypeVar("BaseCodec") |
| 152 | + ArrayArrayCodec = TypeVar("ArrayArrayCodec") |
| 153 | + ArrayBytesCodec = TypeVar("ArrayBytesCodec") |
| 154 | + BytesBytesCodec = TypeVar("BytesBytesCodec") |
| 155 | + CodecPipeline = TypeVar("CodecPipeline") |
| 156 | + Codec = BaseCodec |
| 157 | + |
| 158 | +# Additional commonly used types |
| 159 | +ArrayMetadata: TypeAlias = Any |
| 160 | +ArrayMetadataDict: TypeAlias = dict[str, JSON] |
| 161 | +ArrayV2MetadataDict: TypeAlias = dict[str, JSON] |
| 162 | +ArrayV3MetadataDict: TypeAlias = dict[str, JSON] |
| 163 | + |
| 164 | +# Additional metadata types with forward references |
| 165 | +if TYPE_CHECKING: |
| 166 | + from zarr.core.array_spec import ArrayV2Metadata as _ArrayV2Metadata |
| 167 | + from zarr.core.array_spec import ArrayV3Metadata as _ArrayV3Metadata |
| 168 | + from zarr.core.group import ConsolidatedMetadata as _ConsolidatedMetadata |
| 169 | + from zarr.core.group import GroupMetadata as _GroupMetadata |
| 170 | + |
| 171 | + ArrayV2Metadata: TypeAlias = _ArrayV2Metadata |
| 172 | + ArrayV3Metadata: TypeAlias = _ArrayV3Metadata |
| 173 | + GroupMetadata: TypeAlias = _GroupMetadata |
| 174 | + ConsolidatedMetadata: TypeAlias = _ConsolidatedMetadata |
| 175 | +else: |
| 176 | + GroupMetadata = TypeVar("GroupMetadata") |
| 177 | + ConsolidatedMetadata = TypeVar("ConsolidatedMetadata") |
| 178 | + |
| 179 | +# Codec types |
| 180 | +if TYPE_CHECKING: |
| 181 | + from zarr.abc.codec import BytesBytesCodec as _BytesBytesCodec |
| 182 | + |
| 183 | + CompressorLike: TypeAlias = dict[str, JSON] | _BytesBytesCodec | Any | Literal["auto"] | None |
| 184 | +else: |
| 185 | + CompressorLike: TypeAlias = dict[str, JSON] | Any | Literal["auto"] | None |
| 186 | + |
| 187 | +CompressorsLike: TypeAlias = Iterable[CompressorLike] |
| 188 | +FiltersLike: TypeAlias = Iterable[Any] |
| 189 | +SerializerLike: TypeAlias = Any |
| 190 | +ShardsLike: TypeAlias = tuple[int, ...] | None |
| 191 | +CompressorLikev2: TypeAlias = dict[str, JSON] | Any | None |
| 192 | + |
| 193 | +# Store protocols |
| 194 | +if TYPE_CHECKING: |
| 195 | + from zarr.abc.store import ByteGetter as _ByteGetter |
| 196 | + from zarr.abc.store import ByteSetter as _ByteSetter |
| 197 | + |
| 198 | + ByteGetter: TypeAlias = _ByteGetter |
| 199 | + ByteSetter: TypeAlias = _ByteSetter |
| 200 | +else: |
| 201 | + ByteGetter = TypeVar("ByteGetter") |
| 202 | + ByteSetter = TypeVar("ByteSetter") |
| 203 | + |
| 204 | +# Import enums at runtime - these don't cause circular imports |
| 205 | +try: |
| 206 | + from zarr.codecs.blosc import BloscCname, BloscShuffle |
| 207 | + from zarr.codecs.bytes import Endian |
| 208 | + from zarr.codecs.sharding import ShardingCodecIndexLocation |
| 209 | + from zarr.core.indexing import Order |
| 210 | +except ImportError: |
| 211 | + # Fallback if imports fail |
| 212 | + BloscCname = Any |
| 213 | + BloscShuffle = Any |
| 214 | + Endian = Any |
| 215 | + ShardingCodecIndexLocation = Any |
| 216 | + Order = Any |
| 217 | + |
11 | 218 | __all__ = [
|
| 219 | + # Basic type aliases from zarr.core.types |
| 220 | + "JSON", |
| 221 | + "AccessModeLiteral", |
| 222 | + # Extended types for comprehensive documentation linking |
| 223 | + # Core classes |
| 224 | + "Array", |
| 225 | + "ArrayArrayCodec", |
| 226 | + "ArrayBytesCodec", |
| 227 | + # Array config |
| 228 | + "ArrayConfig", |
| 229 | + "ArrayConfigLike", |
| 230 | + # Array types |
| 231 | + "ArrayLike", |
| 232 | + # Metadata types |
| 233 | + "ArrayMetadata", |
| 234 | + "ArrayMetadataDict", |
| 235 | + # Core metadata types from zarr.core.types |
12 | 236 | "ArrayMetadataJSON_V2",
|
13 | 237 | "ArrayMetadataJSON_V3",
|
| 238 | + "ArrayV2Metadata", |
| 239 | + "ArrayV2MetadataDict", |
| 240 | + "ArrayV3Metadata", |
| 241 | + "ArrayV3MetadataDict", |
| 242 | + "AsyncArray", |
| 243 | + "AsyncGroup", |
| 244 | + # Codec types |
| 245 | + "BaseCodec", |
| 246 | + # Enums |
| 247 | + "BloscCname", |
| 248 | + "BloscShuffle", |
| 249 | + "ByteGetter", |
| 250 | + "ByteSetter", |
| 251 | + "BytesBytesCodec", |
| 252 | + "BytesLike", |
| 253 | + "ChunkCoords", |
| 254 | + "ChunkCoordsLike", |
| 255 | + # Chunk key encoding |
| 256 | + "ChunkKeyEncoding", |
| 257 | + "ChunkKeyEncodingLike", |
| 258 | + "ChunkKeyEncodingParams", |
| 259 | + "Codec", |
| 260 | + "CodecPipeline", |
| 261 | + "CompressorLike", |
| 262 | + "CompressorLikev2", |
| 263 | + "CompressorsLike", |
| 264 | + "ConsolidatedMetadata", |
| 265 | + "DefaultFillValue", |
| 266 | + "DimensionNames", |
| 267 | + "Endian", |
| 268 | + "FiltersLike", |
| 269 | + "Group", |
| 270 | + "GroupMetadata", |
14 | 271 | "GroupMetadataJSON_V2",
|
15 | 272 | "GroupMetadataJSON_V3",
|
| 273 | + "MemoryOrder", |
| 274 | + "NDArrayLike", |
| 275 | + "NDArrayLikeOrScalar", |
| 276 | + "NodeType", |
| 277 | + "Order", |
| 278 | + "SerializerLike", |
| 279 | + "ShapeLike", |
| 280 | + "ShardingCodecIndexLocation", |
| 281 | + "ShardsLike", |
| 282 | + # Store types |
| 283 | + "Store", |
| 284 | + "StoreLike", |
| 285 | + "StorePath", |
| 286 | + "ZDType", |
| 287 | + "ZDTypeLike", |
16 | 288 | "ZarrFormat",
|
17 | 289 | ]
|
| 290 | + |
| 291 | +# Post-import patching for types that have circular import issues |
| 292 | +if not TYPE_CHECKING: |
| 293 | + try: |
| 294 | + from zarr.storage._common import StorePath as _StorePath_Runtime |
| 295 | + |
| 296 | + StorePath = _StorePath_Runtime |
| 297 | + |
| 298 | + from zarr.abc.store import Store as _Store_Runtime |
| 299 | + |
| 300 | + Store = _Store_Runtime |
| 301 | + |
| 302 | + # Update StoreLike with the real types |
| 303 | + StoreLike = Store | StorePath | PathLike[str] | str | dict[str, Any] | Mapping[str, Any] |
| 304 | + except ImportError: |
| 305 | + pass # Keep the TypeVar fallbacks |
0 commit comments