11from __future__ import annotations
22
3+ from collections .abc import Sequence
34from typing import TYPE_CHECKING , Final , TypeAlias
45
56from zarr .core .dtype .common import (
9495 "ZDType" ,
9596 "data_type_registry" ,
9697 "parse_data_type" ,
98+ "parse_dtype" ,
9799]
98100
99101data_type_registry = DataTypeRegistry ()
@@ -188,22 +190,26 @@ def parse_data_type(
188190 zarr_format : ZarrFormat ,
189191) -> ZDType [TBaseDType , TBaseScalar ]:
190192 """
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``.
192197
193198 Parameters
194199 ----------
195200 dtype_spec : ZDTypeLike
196- The input to be interpreted as a ZDType instance. This could be a native data type
197- (e.g., a NumPy data type), a Python object that can be converted into a native data type,
198- a ZDType instance (in which case the input is returned unchanged), or a JSON object
199- representation of a data type.
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.
200204 zarr_format : ZarrFormat
201- The zarr format version.
205+ The Zarr format version. This parameter is required because this function will attempt to
206+ parse the JSON representation of a data type, and the JSON representation of data types
207+ varies between Zarr 2 and Zarr 3.
202208
203209 Returns
204210 -------
205211 ZDType[TBaseDType, TBaseScalar]
206- The ZDType instance corresponding to the input.
212+ The ZDType corresponding to the input.
207213
208214 Examples
209215 --------
@@ -216,15 +222,57 @@ def parse_data_type(
216222 >>> parse_data_type({"name": "numpy.datetime64", "configuration": {"unit": "s", "scale_factor": 10}}, zarr_format=3)
217223 DateTime64(endianness='little', scale_factor=10, unit='s')
218224 """
225+ return parse_dtype (dtype_spec , zarr_format = zarr_format )
226+
227+
228+ def parse_dtype (
229+ dtype_spec : ZDTypeLike ,
230+ * ,
231+ zarr_format : ZarrFormat ,
232+ ) -> ZDType [TBaseDType , TBaseScalar ]:
233+ """
234+ Convert the input as a ZDType.
235+
236+ Parameters
237+ ----------
238+ dtype_spec : ZDTypeLike
239+ The input to be converted to a ZDType. This could be a ZDType, which will be returned
240+ directly, or a JSON representation of a ZDType, or a numpy dtype, or a python object that
241+ can be converted into a native dtype.
242+ zarr_format : ZarrFormat
243+ The Zarr format version. This parameter is required because this function will attempt to
244+ parse the JSON representation of a data type, and the JSON representation of data types
245+ varies between Zarr 2 and Zarr 3.
246+
247+ Returns
248+ -------
249+ ZDType[TBaseDType, TBaseScalar]
250+ The ZDType corresponding to the input.
251+
252+ Examples
253+ --------
254+ >>> from zarr.dtype import parse_dtype
255+ >>> import numpy as np
256+ >>> parse_dtype("int32", zarr_format=2)
257+ Int32(endianness='little')
258+ >>> parse_dtype(np.dtype('S10'), zarr_format=2)
259+ NullTerminatedBytes(length=10)
260+ >>> parse_dtype({"name": "numpy.datetime64", "configuration": {"unit": "s", "scale_factor": 10}}, zarr_format=3)
261+ DateTime64(endianness='little', scale_factor=10, unit='s')
262+ """
219263 if isinstance (dtype_spec , ZDType ):
220264 return dtype_spec
221- # dict and zarr_format 3 means that we have a JSON object representation of the dtype
222- if zarr_format == 3 and isinstance (dtype_spec , Mapping ):
223- return get_data_type_from_json (dtype_spec , zarr_format = 3 )
265+ # First attempt to interpret the input as JSON
266+ if isinstance (dtype_spec , Mapping | str | Sequence ):
267+ try :
268+ return get_data_type_from_json (dtype_spec , zarr_format = zarr_format ) # type: ignore[arg-type]
269+ except ValueError :
270+ # no data type matched this JSON-like input
271+ pass
224272 if dtype_spec in VLEN_UTF8_ALIAS :
225273 # If the dtype request is one of the aliases for variable-length UTF-8 strings,
226274 # return that dtype.
227275 return VariableLengthUTF8 () # type: ignore[return-value]
228276 # otherwise, we have either a numpy dtype string, or a zarr v3 dtype string, and in either case
229- # we can create a numpy dtype from it, and do the dtype inference from that
277+ # we can create a native dtype from it, and do the dtype inference from that
230278 return get_data_type_from_native_dtype (dtype_spec ) # type: ignore[arg-type]
0 commit comments