|
41 | 41 | from collections.abc import Callable, Generator |
42 | 42 | from pathlib import Path |
43 | 43 |
|
44 | | - from zarr.core.common import ChunksLike |
| 44 | + from zarr.core.common import ChunksLike, ZarrFormat |
45 | 45 |
|
46 | 46 |
|
47 | 47 | @pytest.fixture(autouse=True) |
@@ -1246,46 +1246,80 @@ def test_rectilinear_chunks_gates( |
1246 | 1246 | zarr.create(store=MemoryStore(), shape=shape, chunks=chunks, zarr_format=2, dtype="uint8") |
1247 | 1247 |
|
1248 | 1248 |
|
| 1249 | +_LEGACY_SHAPE = (2**12, 2**12) |
| 1250 | + |
| 1251 | + |
| 1252 | +@pytest.mark.parametrize("zarr_format", [2, 3]) |
1249 | 1253 | @pytest.mark.parametrize( |
1250 | 1254 | ("chunks", "expected"), |
1251 | 1255 | [ |
1252 | | - (None, None), |
1253 | | - (0, None), |
1254 | | - ((), None), |
1255 | | - ([], None), |
1256 | | - (False, None), |
1257 | | - (np.int64(0), None), |
1258 | | - (np.False_, None), |
1259 | | - (np.array(0), None), |
1260 | | - (np.array(False), None), |
1261 | | - (np.array([0]), None), |
1262 | | - (np.array([]), None), |
| 1256 | + (None, guess_chunks(_LEGACY_SHAPE, 4).chunk_shape), |
| 1257 | + (False, _LEGACY_SHAPE), |
1263 | 1258 | (np.array(7), (7, 7)), |
1264 | 1259 | (np.array([5, 3]), (5, 3)), |
1265 | 1260 | ], |
1266 | 1261 | ids=repr, |
1267 | 1262 | ) |
1268 | | -def test_legacy_create_v2_chunks( |
1269 | | - chunks: ChunksLike | None, expected: tuple[int, ...] | None |
| 1263 | +def test_legacy_create_chunks( |
| 1264 | + chunks: ChunksLike | None, expected: tuple[int, ...], zarr_format: ZarrFormat |
1270 | 1265 | ) -> None: |
1271 | | - """The legacy `zarr.create` Zarr format 2 path reads a falsy `chunks` as not given |
1272 | | - and chunks automatically (`expected` is `None`). It never tests the truth value of a |
1273 | | - numpy array with more than one element, which has none: that array is the chunk |
1274 | | - shape.""" |
1275 | | - shape = (2**12, 2**12) |
1276 | | - arr = zarr.create(store=MemoryStore(), shape=shape, chunks=chunks, dtype="int32", zarr_format=2) |
1277 | | - assert arr.chunks == (expected or guess_chunks(shape, 4).chunk_shape) |
| 1266 | + """The legacy `zarr.create` reads `chunks` the same way for both Zarr formats: only |
| 1267 | + `None` means not given and chunks automatically; `False` is one chunk, and a numpy |
| 1268 | + array is the chunk shape.""" |
| 1269 | + arr = zarr.create( |
| 1270 | + store=MemoryStore(), |
| 1271 | + shape=_LEGACY_SHAPE, |
| 1272 | + chunks=chunks, |
| 1273 | + dtype="int32", |
| 1274 | + zarr_format=zarr_format, |
| 1275 | + ) |
| 1276 | + assert arr.chunks == expected |
1278 | 1277 | assert all(type(c) is int for c in arr.chunks) |
1279 | 1278 |
|
1280 | 1279 |
|
1281 | | -def test_legacy_create_v2_chunks_all_zero_array_raises() -> None: |
1282 | | - """A numpy `chunks` array with more than one element is given even when every element |
1283 | | - is 0, so the legacy Zarr format 2 path rejects its 0 sizes instead of chunking |
1284 | | - automatically.""" |
1285 | | - chunks = np.array([0, 0]) |
| 1280 | +@pytest.mark.parametrize("zarr_format", [2, 3]) |
| 1281 | +@pytest.mark.parametrize("chunks", [0, np.int64(0), np.array(0), np.array([0, 0])], ids=repr) |
| 1282 | +def test_legacy_create_chunks_rejects_zero(chunks: ChunksLike, zarr_format: ZarrFormat) -> None: |
| 1283 | + """A chunk size of 0 is rejected, not read as "not given".""" |
1286 | 1284 | with pytest.raises(ValueError, match="Chunk size must be positive or -1, got 0"): |
1287 | 1285 | zarr.create( |
1288 | | - store=MemoryStore(), shape=(2**12, 2**12), chunks=chunks, dtype="int32", zarr_format=2 |
| 1286 | + store=MemoryStore(), |
| 1287 | + shape=_LEGACY_SHAPE, |
| 1288 | + chunks=chunks, |
| 1289 | + dtype="int32", |
| 1290 | + zarr_format=zarr_format, |
| 1291 | + ) |
| 1292 | + |
| 1293 | + |
| 1294 | +@pytest.mark.parametrize("zarr_format", [2, 3]) |
| 1295 | +@pytest.mark.parametrize("chunks", [(), [], np.array([]), np.array([0])], ids=repr) |
| 1296 | +def test_legacy_create_chunks_rejects_wrong_ndim( |
| 1297 | + chunks: ChunksLike, zarr_format: ZarrFormat |
| 1298 | +) -> None: |
| 1299 | + """An empty or too-short chunk shape is rejected, not read as "not given".""" |
| 1300 | + with pytest.raises(ValueError, match="dimensions but shape has 2 dimensions"): |
| 1301 | + zarr.create( |
| 1302 | + store=MemoryStore(), |
| 1303 | + shape=_LEGACY_SHAPE, |
| 1304 | + chunks=chunks, |
| 1305 | + dtype="int32", |
| 1306 | + zarr_format=zarr_format, |
| 1307 | + ) |
| 1308 | + |
| 1309 | + |
| 1310 | +@pytest.mark.parametrize("zarr_format", [2, 3]) |
| 1311 | +@pytest.mark.parametrize("chunks", [np.False_, np.array(False)], ids=repr) |
| 1312 | +def test_legacy_create_chunks_rejects_numpy_bool( |
| 1313 | + chunks: ChunksLike, zarr_format: ZarrFormat |
| 1314 | +) -> None: |
| 1315 | + """A numpy boolean is not a chunk size, and is not read as "not given".""" |
| 1316 | + with pytest.raises(TypeError, match="Chunk specification must be an integer"): |
| 1317 | + zarr.create( |
| 1318 | + store=MemoryStore(), |
| 1319 | + shape=_LEGACY_SHAPE, |
| 1320 | + chunks=chunks, |
| 1321 | + dtype="int32", |
| 1322 | + zarr_format=zarr_format, |
1289 | 1323 | ) |
1290 | 1324 |
|
1291 | 1325 |
|
|
0 commit comments