Skip to content

Commit 6998375

Browse files
committed
tests for invalid arguments in creation
1 parent 788f9cc commit 6998375

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/zarr/core/array.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@
135135
logger = getLogger(__name__)
136136

137137

138-
def parse_array_metadata(data: ArrayMetadata | dict[str, JSON]) -> ArrayMetadata:
138+
def parse_array_metadata(
139+
data: ArrayMetadata | ArrayMetadataDict | dict[str, JSON],
140+
) -> ArrayMetadata:
139141
if isinstance(data, ArrayMetadata):
140142
return data
141143
elif isinstance(data, dict):

tests/test_array.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,9 +1137,9 @@ async def test_v3_chunk_encoding(
11371137
@pytest.mark.parametrize("name", ["v2", "default", "invalid"])
11381138
@pytest.mark.parametrize("separator", [".", "/"])
11391139
async def test_chunk_key_encoding(
1140-
name: str, separator: str, zarr_format: ZarrFormat, store: MemoryStore
1140+
name: str, separator: Literal[".", "/"], zarr_format: ZarrFormat, store: MemoryStore
11411141
) -> None:
1142-
chunk_key_encoding = ChunkKeyEncodingParams(name=name, separator=separator) # type: ignore[typeddict-item"]
1142+
chunk_key_encoding = ChunkKeyEncodingParams(name=name, separator=separator) # type: ignore[typeddict-item]
11431143
error_msg = ""
11441144
if name == "invalid":
11451145
error_msg = "Unknown chunk key encoding."
@@ -1175,13 +1175,48 @@ async def test_chunk_key_encoding(
11751175
({"dimension_names": ["test"]}, "Zarr format 2 arrays do not support dimension names."),
11761176
],
11771177
)
1178-
async def test_invalid_v2_arguments(
1178+
async def test_create_array_invalid_v2_arguments(
11791179
kwargs: dict[str, Any], error_msg: str, store: MemoryStore
11801180
) -> None:
11811181
with pytest.raises(ValueError, match=re.escape(error_msg)):
1182-
await create_array(
1182+
await zarr.api.asynchronous.create_array(
11831183
store=store, dtype="uint8", shape=(10,), chunks=(1,), zarr_format=2, **kwargs
11841184
)
1185+
@staticmethod
1186+
@pytest.mark.parametrize(
1187+
("kwargs", "error_msg"),
1188+
[
1189+
({"dimension_names": ["test"]}, "dimension_names cannot be used for arrays with zarr_format 2."),
1190+
({"chunk_key_encoding": {"name": "default", "separator": "/"}}, "chunk_key_encoding cannot be used for arrays with zarr_format 2. Use dimension_separator instead."),
1191+
({"codecs": "bytes"}, "codecs cannot be used for arrays with zarr_format 2. Use filters and compressor instead."),
1192+
],
1193+
)
1194+
async def test_create_invalid_v2_arguments(
1195+
kwargs: dict[str, Any], error_msg: str, store: MemoryStore
1196+
) -> None:
1197+
with pytest.raises(ValueError, match=re.escape(error_msg)):
1198+
await zarr.api.asynchronous.create(
1199+
store=store, dtype="uint8", shape=(10,), chunks=(1,), zarr_format=2, **kwargs
1200+
)
1201+
1202+
@staticmethod
1203+
@pytest.mark.parametrize(
1204+
("kwargs", "error_msg"),
1205+
[
1206+
({"chunk_shape": (1,), "chunks": (2,)}, "Only one of chunk_shape or chunks can be provided."),
1207+
({"dimension_separator": "/"}, "dimension_separator cannot be used for arrays with zarr_format 3. Use chunk_key_encoding instead."),
1208+
({"filters": []}, "filters cannot be used for arrays with zarr_format 3. Use array-to-array codecs instead"),
1209+
({"compressor": "blosc"}, "compressor cannot be used for arrays with zarr_format 3. Use bytes-to-bytes codecs instead"),
1210+
],
1211+
)
1212+
async def test_invalid_v3_arguments(
1213+
kwargs: dict[str, Any], error_msg: str, store: MemoryStore
1214+
) -> None:
1215+
kwargs.setdefault("chunks", (1,))
1216+
with pytest.raises(ValueError, match=re.escape(error_msg)):
1217+
zarr.create(
1218+
store=store, dtype="uint8", shape=(10,), zarr_format=3, **kwargs
1219+
)
11851220

11861221
@staticmethod
11871222
@pytest.mark.parametrize("dtype", ["uint8", "float32", "str"])

0 commit comments

Comments
 (0)