|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from collections.abc import Sequence |
3 | 4 | from typing import TYPE_CHECKING, Final, TypeAlias |
4 | 5 |
|
5 | 6 | from zarr.core.dtype.common import ( |
|
94 | 95 | "ZDType", |
95 | 96 | "data_type_registry", |
96 | 97 | "parse_data_type", |
| 98 | + "parse_dtype", |
97 | 99 | ] |
98 | 100 |
|
99 | 101 | data_type_registry = DataTypeRegistry() |
@@ -188,13 +190,69 @@ def parse_data_type( |
188 | 190 | zarr_format: ZarrFormat, |
189 | 191 | ) -> ZDType[TBaseDType, TBaseScalar]: |
190 | 192 | """ |
191 | | - Interpret the input as a ZDType instance. |
| 193 | + Interpret the input as a ZDType. |
| 194 | +
|
| 195 | + This function wraps ``parse_dtype``. The only difference is the function name. This function may |
| 196 | + be deprecated in a future version of Zarr Python in favor of ``parse_dtype``. |
| 197 | +
|
| 198 | + Parameters |
| 199 | + ---------- |
| 200 | + dtype_spec : ZDTypeLike |
| 201 | + The input to be interpreted as a ZDType. This could be a ZDType, which will be returned |
| 202 | + directly, or a JSON representation of a ZDType, or a native dtype, or a python object that |
| 203 | + can be converted into a native dtype. |
| 204 | + zarr_format : ZarrFormat |
| 205 | + The Zarr format version. |
| 206 | +
|
| 207 | + Returns |
| 208 | + ------- |
| 209 | + ZDType[TBaseDType, TBaseScalar] |
| 210 | + The ZDType corresponding to the input. |
| 211 | +
|
| 212 | + Examples |
| 213 | + -------- |
| 214 | + >>> parse_dtype("int32", zarr_format=2) |
| 215 | + Int32(endianness="little") |
| 216 | + """ |
| 217 | + return parse_dtype(dtype_spec, zarr_format=zarr_format) |
| 218 | + |
| 219 | + |
| 220 | +def parse_dtype( |
| 221 | + dtype_spec: ZDTypeLike, |
| 222 | + *, |
| 223 | + zarr_format: ZarrFormat, |
| 224 | +) -> ZDType[TBaseDType, TBaseScalar]: |
| 225 | + """ |
| 226 | + Interpret the input as a ZDType. |
| 227 | +
|
| 228 | + Parameters |
| 229 | + ---------- |
| 230 | + dtype_spec : ZDTypeLike |
| 231 | + The input to be interpreted as a ZDType. This could be a ZDType, which will be returned |
| 232 | + directly, or a JSON representation of a ZDType, or a native dtype, or a python object that |
| 233 | + can be converted into a native dtype. |
| 234 | + zarr_format : ZarrFormat |
| 235 | + The Zarr format version. |
| 236 | +
|
| 237 | + Returns |
| 238 | + ------- |
| 239 | + ZDType[TBaseDType, TBaseScalar] |
| 240 | + The ZDType corresponding to the input. |
| 241 | +
|
| 242 | + Examples |
| 243 | + -------- |
| 244 | + >>> parse_dtype("int32", zarr_format=2) |
| 245 | + Int32(endianness="little") |
192 | 246 | """ |
193 | 247 | if isinstance(dtype_spec, ZDType): |
194 | 248 | return dtype_spec |
195 | | - # dict and zarr_format 3 means that we have a JSON object representation of the dtype |
196 | | - if zarr_format == 3 and isinstance(dtype_spec, Mapping): |
197 | | - return get_data_type_from_json(dtype_spec, zarr_format=3) |
| 249 | + # First attempt to interpret the input as JSON |
| 250 | + if isinstance(dtype_spec, Mapping | str | Sequence): |
| 251 | + try: |
| 252 | + return get_data_type_from_json(dtype_spec, zarr_format=3) # type: ignore[arg-type] |
| 253 | + except ValueError: |
| 254 | + # no data type matched this JSON-like input |
| 255 | + pass |
198 | 256 | if dtype_spec in VLEN_UTF8_ALIAS: |
199 | 257 | # If the dtype request is one of the aliases for variable-length UTF-8 strings, |
200 | 258 | # return that dtype. |
|
0 commit comments