Skip to content

Commit f4d31a2

Browse files
committed
test for v2-style error when creating an object array without an object codec
1 parent 173766d commit f4d31a2

2 files changed

Lines changed: 54 additions & 11 deletions

File tree

src/zarr/core/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4721,7 +4721,7 @@ def default_filters_v2(dtype: ZDType[Any, Any]) -> tuple[numcodecs.abc.Codec] |
47214721

47224722
return (VLenUTF8(),)
47234723
else:
4724-
msg = f"Data type {dtype} requires an unknown object codec: {dtype.object_codec_id}"
4724+
msg = f"Data type {dtype} requires an unknown object codec: {dtype.object_codec_id!r}"
47254725
raise ValueError(msg)
47264726
return None
47274727

@@ -4795,7 +4795,7 @@ def _parse_chunk_encoding_v2(
47954795
elif isinstance(dtype, VariableLengthBytes):
47964796
codec_name = "the numcodecs.VLenBytes codec"
47974797
else:
4798-
codec_name = "an unknown object codec"
4798+
codec_name = f"an unknown object codec with id {dtype.object_codec_id!r}"
47994799
msg = (
48004800
f"Data type {dtype} requires {codec_name}, "
48014801
"but no such codec was specified in the filters or compressor parameters for "

tests/test_array.py

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,22 @@
4141
from zarr.core.chunk_grids import _auto_partition
4242
from zarr.core.chunk_key_encodings import ChunkKeyEncodingParams
4343
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,
5149
Structured,
50+
TimeDelta64,
51+
UInt8,
52+
VariableLengthBytes,
53+
VariableLengthUTF8,
54+
ZDType,
55+
parse_data_type,
5256
)
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
5560
from zarr.core.group import AsyncGroup
5661
from zarr.core.indexing import BasicIndexer, ceildiv
5762
from zarr.core.metadata.v2 import ArrayV2Metadata
@@ -1850,3 +1855,41 @@ def test_array_repr(store: Store) -> None:
18501855
dtype = "uint8"
18511856
arr = zarr.create_array(store, shape=shape, dtype=dtype)
18521857
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

Comments
 (0)