|
39 | 39 | from zarr.core.chunk_grids import _auto_partition |
40 | 40 | from zarr.core.common import JSON, MemoryOrder, ZarrFormat |
41 | 41 | from zarr.core.dtype import get_data_type_from_native_dtype |
42 | | -from zarr.core.dtype._numpy import Float64, endianness_from_numpy_str |
| 42 | +from zarr.core.dtype._numpy import Float64, Int16, endianness_from_numpy_str |
43 | 43 | from zarr.core.dtype.common import Endianness |
44 | 44 | from zarr.core.dtype.wrapper import ZDType |
45 | 45 | from zarr.core.group import AsyncGroup |
46 | 46 | from zarr.core.indexing import BasicIndexer, ceildiv |
| 47 | +from zarr.core.metadata.v3 import ArrayV3Metadata |
47 | 48 | from zarr.core.sync import sync |
48 | 49 | from zarr.errors import ContainsArrayError, ContainsGroupError |
49 | 50 | from zarr.storage import LocalStore, MemoryStore, StorePath |
|
53 | 54 | if TYPE_CHECKING: |
54 | 55 | from zarr.core.array_spec import ArrayConfigLike |
55 | 56 | from zarr.core.metadata.v2 import ArrayV2Metadata |
56 | | - from zarr.core.metadata.v3 import ArrayV3Metadata |
57 | 57 |
|
58 | 58 |
|
59 | 59 | @pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"]) |
@@ -1388,16 +1388,41 @@ async def test_sharding_coordinate_selection() -> None: |
1388 | 1388 |
|
1389 | 1389 | @pytest.mark.parametrize("store", ["memory"], indirect=True) |
1390 | 1390 | @pytest.mark.parametrize("endianness", get_args(Endianness)) |
1391 | | -def test_endianness(store: Store, zarr_format: ZarrFormat, endianness: Endianness) -> None: |
| 1391 | +def test_default_endianness(store: Store, zarr_format: ZarrFormat, endianness: Endianness) -> None: |
1392 | 1392 | """ |
1393 | | - Test that that endianness is correctly set when creating an array. |
| 1393 | + Test that that endianness is correctly set when creating an array when not specifying a serializer |
| 1394 | + """ |
| 1395 | + dtype = Int16(endianness=endianness) |
| 1396 | + arr = zarr.create_array(store=store, shape=(1,), dtype=dtype, zarr_format=zarr_format) |
| 1397 | + assert endianness_from_numpy_str(arr[:].dtype.byteorder) == endianness |
| 1398 | + if zarr_format == 3: |
| 1399 | + assert isinstance(arr.metadata, ArrayV3Metadata) # mypy |
| 1400 | + assert str(arr.metadata.codecs[0].endian.value) == endianness # type: ignore[union-attr] |
| 1401 | + |
| 1402 | + |
| 1403 | +@pytest.mark.parametrize("store", ["memory"], indirect=True) |
| 1404 | +@pytest.mark.parametrize("endianness", get_args(Endianness)) |
| 1405 | +def test_explicit_endianness(store: Store, endianness: Endianness) -> None: |
| 1406 | + """ |
| 1407 | + Test that that a mismatch between the bytescodec endianness and the dtype endianness is an error |
1394 | 1408 | """ |
1395 | 1409 | if endianness == "little": |
1396 | | - np_dtype = "<i2" |
| 1410 | + dtype = Int16(endianness="big") |
1397 | 1411 | else: |
1398 | | - np_dtype = ">i2" |
| 1412 | + dtype = Int16(endianness="little") |
1399 | 1413 |
|
1400 | | - arr = zarr.create_array(store=store, shape=(1,), dtype=np_dtype, zarr_format=zarr_format) |
1401 | | - assert endianness_from_numpy_str(arr[:].dtype.byteorder) == endianness |
1402 | | - if zarr_format == 3: |
1403 | | - assert str(arr.metadata.codecs[0].endian.value) == endianness |
| 1414 | + serializer = BytesCodec(endian=endianness) |
| 1415 | + |
| 1416 | + msg = ( |
| 1417 | + f"The endianness of the requested serializer ({serializer}) does not match the endianness of the dtype ({dtype.endianness}). " |
| 1418 | + "The endianness of the serializer and the dtype must match." |
| 1419 | + ) |
| 1420 | + |
| 1421 | + with pytest.raises(ValueError, match=re.escape(msg)): |
| 1422 | + _ = zarr.create_array( |
| 1423 | + store=store, |
| 1424 | + shape=(1,), |
| 1425 | + dtype=dtype, |
| 1426 | + zarr_format=3, |
| 1427 | + serializer=serializer, |
| 1428 | + ) |
0 commit comments