|
41 | 41 | from zarr.core.chunk_grids import _auto_partition |
42 | 42 | from zarr.core.chunk_key_encodings import ChunkKeyEncodingParams |
43 | 43 | from zarr.core.common import JSON, MemoryOrder, ZarrFormat |
44 | | -from zarr.core.dtype import parse_data_type |
45 | | -from zarr.core.dtype.common import ENDIANNESS_STR, EndiannessStr |
46 | | -from zarr.core.dtype.npy.common import NUMPY_ENDIANNESS_STR, endianness_from_numpy_str |
47 | | -from zarr.core.dtype.npy.float import Float32, Float64 |
48 | | -from zarr.core.dtype.npy.int import Int16, UInt8 |
49 | | -from zarr.core.dtype.npy.string import VariableLengthUTF8 |
50 | | -from zarr.core.dtype.npy.structured import ( |
| 44 | +from zarr.core.dtype import ( |
| 45 | + DateTime64, |
| 46 | + Float32, |
| 47 | + Float64, |
| 48 | + Int16, |
51 | 49 | Structured, |
| 50 | + TimeDelta64, |
| 51 | + UInt8, |
| 52 | + VariableLengthBytes, |
| 53 | + VariableLengthUTF8, |
| 54 | + ZDType, |
| 55 | + parse_data_type, |
52 | 56 | ) |
53 | | -from zarr.core.dtype.npy.time import DateTime64, TimeDelta64 |
54 | | -from zarr.core.dtype.wrapper import ZDType |
| 57 | +from zarr.core.dtype.common import ENDIANNESS_STR, EndiannessStr |
| 58 | +from zarr.core.dtype.npy.common import NUMPY_ENDIANNESS_STR, endianness_from_numpy_str |
| 59 | +from zarr.core.dtype.npy.string import UTF8Base |
55 | 60 | from zarr.core.group import AsyncGroup |
56 | 61 | from zarr.core.indexing import BasicIndexer, ceildiv |
57 | 62 | from zarr.core.metadata.v2 import ArrayV2Metadata |
@@ -1850,3 +1855,41 @@ def test_array_repr(store: Store) -> None: |
1850 | 1855 | dtype = "uint8" |
1851 | 1856 | arr = zarr.create_array(store, shape=shape, dtype=dtype) |
1852 | 1857 | assert str(arr) == f"<Array {store} shape={shape} dtype={dtype}>" |
| 1858 | + |
| 1859 | + |
| 1860 | +class UnknownObjectDtype(UTF8Base[np.dtypes.ObjectDType]): |
| 1861 | + object_codec_id = "unknown" # type: ignore[assignment] |
| 1862 | + |
| 1863 | + def to_native_dtype(self) -> np.dtypes.ObjectDType: |
| 1864 | + """ |
| 1865 | + Create a NumPy object dtype from this VariableLengthUTF8 ZDType. |
| 1866 | +
|
| 1867 | + Returns |
| 1868 | + ------- |
| 1869 | + np.dtypes.ObjectDType |
| 1870 | + The NumPy object dtype. |
| 1871 | + """ |
| 1872 | + return np.dtype("o") |
| 1873 | + |
| 1874 | + |
| 1875 | +@pytest.mark.parametrize( |
| 1876 | + "dtype", [VariableLengthUTF8(), VariableLengthBytes(), UnknownObjectDtype()] |
| 1877 | +) |
| 1878 | +def test_chunk_encoding_no_object_codec_errors(dtype: ZDType[Any, Any]) -> None: |
| 1879 | + """ |
| 1880 | + Test that a valuerror is raised when checking the chunk encoding for a v2 array with a |
| 1881 | + data type that requires an object codec, but where no object codec is specified |
| 1882 | + """ |
| 1883 | + if isinstance(dtype, VariableLengthUTF8): |
| 1884 | + codec_name = "the numcodecs.VLenUTF8 codec" |
| 1885 | + elif isinstance(dtype, VariableLengthBytes): |
| 1886 | + codec_name = "the numcodecs.VLenBytes codec" |
| 1887 | + else: |
| 1888 | + codec_name = f"an unknown object codec with id {dtype.object_codec_id!r}" |
| 1889 | + msg = ( |
| 1890 | + f"Data type {dtype} requires {codec_name}, " |
| 1891 | + "but no such codec was specified in the filters or compressor parameters for " |
| 1892 | + "this array. " |
| 1893 | + ) |
| 1894 | + with pytest.raises(ValueError, match=re.escape(msg)): |
| 1895 | + _parse_chunk_encoding_v2(filters=None, compressor=None, dtype=dtype) |
0 commit comments