@@ -221,7 +221,7 @@ def _read_strictly(doc: dict[str, JSON]) -> ArrayV2Metadata | ArrayV3Metadata:
221221
222222def test_stored_negative_chunk_size_rejected () -> None :
223223 """No known writer stored a negative chunk size: it is rejected, not upgraded."""
224- with pytest .raises (ValueError , match = "^Dimension 0: chunk edge length must be >= 1, got -1$ " ):
224+ with pytest .raises (ValueError , match = "^Expected all values to be non-negative " ):
225225 _read_strictly (_v2_doc ([4 ], [- 1 ]))
226226
227227
@@ -232,19 +232,12 @@ def test_stored_chunk_shape_ndim_mismatch_rejected() -> None:
232232 _read_strictly (_v3_doc ([4 , 4 ], [0 ]))
233233
234234
235- def test_stored_float_chunk_size_rejected () -> None :
236- """No known writer stored a float chunk size: it is rejected, not upgraded."""
235+ def test_stored_fractional_chunk_size_rejected () -> None :
236+ """A stored chunk size that is not an integral number is rejected, not upgraded."""
237237 with pytest .raises (
238- TypeError , match = r"^Dimension 0: chunk edge length must be an int , got 4\.0 $"
238+ TypeError , match = r"^Dimension 0: chunk edge length must be an integer , got 4\.5 $"
239239 ):
240- _read_strictly (_v3_doc ([4 ], [4.0 ]))
241-
242-
243- def test_stored_zero_inner_chunk_size_rejected () -> None :
244- """No known writer stored an inner chunk size of 0, and no span defines one: it is
245- rejected, not upgraded."""
246- with pytest .raises (ValueError , match = "^Dimension 0: chunk edge length must be >= 1, got 0$" ):
247- _read_strictly (_v3_doc ([4 ], [0 ], inner = [0 ]))
240+ _read_strictly (_v3_doc ([4 ], [4.5 ]))
248241
249242
250243def test_v2_constructor_rejects_chunks_of_wrong_length () -> None :
@@ -271,33 +264,52 @@ def _rectilinear_from_dict(chunk_shapes: list[Any]) -> RectilinearChunkGridMetad
271264 )
272265
273266
267+ def _second_edge (grid : RectilinearChunkGridMetadata ) -> int :
268+ edges = grid .chunk_shapes [0 ]
269+ assert isinstance (edges , tuple )
270+ return edges [1 ]
271+
272+
274273CHUNK_EDGE_SITES : dict [str , Callable [[Any ], object ]] = {
275- "regular" : lambda size : RegularChunkGridMetadata (chunk_shape = (size ,)),
276- "v2" : lambda size : _v2_metadata ((size ,)),
277- "rectilinear-bare" : lambda size : _rectilinear ((size ,)),
278- "rectilinear-edge" : lambda size : _rectilinear (((4 , size ),)),
279- "rectilinear-bare-json" : lambda size : _rectilinear_from_dict ([size ]),
280- "rectilinear-edge-json" : lambda size : _rectilinear_from_dict ([[4 , size ]]),
281- "rectilinear-rle-json" : lambda size : _rectilinear_from_dict ([[[size , 2 ]]]),
282- "sharding-inner" : lambda size : ShardingCodec (chunk_shape = (size ,)),
274+ "regular" : lambda size : RegularChunkGridMetadata (chunk_shape = (size ,)).chunk_shape [0 ],
275+ "rectilinear-bare" : lambda size : _rectilinear ((size ,)).chunk_shapes [0 ],
276+ "rectilinear-edge" : lambda size : _second_edge (_rectilinear (((4 , size ),))),
277+ "rectilinear-bare-json" : lambda size : _rectilinear_from_dict ([size ]).chunk_shapes [0 ],
278+ "rectilinear-edge-json" : lambda size : _second_edge (_rectilinear_from_dict ([[4 , size ]])),
279+ "rectilinear-rle-json" : lambda size : _second_edge (_rectilinear_from_dict ([[[size , 2 ]]])),
283280}
281+ """Each place chunk grid metadata reads a chunk edge length, returning the edge it read."""
284282
285283
286284@pytest .mark .parametrize ("site" , CHUNK_EDGE_SITES )
287- @pytest .mark .parametrize ("size" , [True , False , 4.0 , np .int64 (4 ), "4" ])
288- def test_metadata_rejects_non_int_chunk_edge (site : str , size : object ) -> None :
289- """Metadata built in code takes chunk edge lengths as `int`s only, everywhere."""
285+ @pytest .mark .parametrize (
286+ ("size" , "expected" ),
287+ [(4 , 4 ), (True , 1 ), (np .int64 (4 ), 4 ), (4.0 , 4 ), (np .float64 (4.0 ), 4 )],
288+ ids = ["int" , "bool" , "numpy-int" , "float" , "numpy-float" ],
289+ )
290+ def test_metadata_reads_integral_chunk_edge (site : str , size : object , expected : int ) -> None :
291+ """Chunk grid metadata reads any integral number as the `int` chunk edge length it
292+ equals."""
293+ edge = CHUNK_EDGE_SITES [site ](size )
294+ assert type (edge ) is int
295+ assert edge == expected
296+
297+
298+ @pytest .mark .parametrize ("site" , CHUNK_EDGE_SITES )
299+ @pytest .mark .parametrize ("size" , [4.5 , float ("inf" ), "4" , None ])
300+ def test_metadata_rejects_non_integral_chunk_edge (site : str , size : object ) -> None :
290301 with pytest .raises (
291- TypeError , match = re .escape (f"Dimension 0: chunk edge length must be an int, got { size !r} " )
302+ TypeError ,
303+ match = re .escape (f"Dimension 0: chunk edge length must be an integer, got { size !r} " ),
292304 ):
293305 CHUNK_EDGE_SITES [site ](size )
294306
295307
296308@pytest .mark .parametrize ("site" , CHUNK_EDGE_SITES )
297- @pytest .mark .parametrize ("size" , [0 , - 1 ])
309+ @pytest .mark .parametrize ("size" , [0 , False , - 1 ])
298310def test_metadata_rejects_chunk_edge_below_one (site : str , size : int ) -> None :
299- """Metadata built in code is strict: a chunk edge length below 1 is rejected,
300- without a warning."""
311+ """Chunk grid metadata built in code is strict: a chunk edge length below 1 is
312+ rejected, without a warning."""
301313 with warnings .catch_warnings ():
302314 warnings .simplefilter ("error" , ZarrUserWarning )
303315 with pytest .raises (
@@ -306,25 +318,71 @@ def test_metadata_rejects_chunk_edge_below_one(site: str, size: int) -> None:
306318 CHUNK_EDGE_SITES [site ](size )
307319
308320
309- CHUNK_SHAPE_SITES : dict [str , Callable [[Any ], object ]] = {
310- "regular" : lambda chunk_shape : RegularChunkGridMetadata (chunk_shape = chunk_shape ),
311- "v2" : _v2_metadata ,
312- "sharding-inner" : lambda chunk_shape : ShardingCodec (chunk_shape = chunk_shape ),
313- }
314-
315-
316- @pytest .mark .parametrize ("site" , CHUNK_SHAPE_SITES )
317- @pytest .mark .parametrize ("chunk_shape" , [4 , np .int64 (4 ), "10" , {"a" : 1 }, range (1 , 2 )])
318- def test_metadata_rejects_chunk_shape_not_list_or_tuple (site : str , chunk_shape : object ) -> None :
319- """A regular chunk shape is a list or tuple; anything else is rejected as a whole,
320- not iterated as if its elements were chunk edge lengths."""
321+ @pytest .mark .parametrize ("chunk_shape" , [4 , np .int64 (4 ), None ])
322+ def test_regular_chunk_grid_rejects_chunk_shape_not_iterable (chunk_shape : Any ) -> None :
321323 with pytest .raises (
322324 TypeError ,
323325 match = re .escape (
324- f"A chunk shape must be a list or tuple of chunk edge lengths, got { chunk_shape !r} "
326+ f"A chunk shape must be an iterable of chunk edge lengths, got { chunk_shape !r} "
325327 ),
326328 ):
327- CHUNK_SHAPE_SITES [site ](chunk_shape )
329+ RegularChunkGridMetadata (chunk_shape = chunk_shape )
330+
331+
332+ def _sharding_chunk_shape (chunks : Any ) -> tuple [tuple [int , ...], object ]:
333+ codec = ShardingCodec (chunk_shape = chunks )
334+ configuration = cast ("dict[str, JSON]" , codec .to_dict ()["configuration" ])
335+ return codec .chunk_shape , configuration ["chunk_shape" ]
336+
337+
338+ CHUNK_SHAPE_SITES : dict [str , Callable [[Any ], tuple [tuple [int , ...], object ]]] = {
339+ "v2" : lambda chunks : ((md := _v2_metadata (chunks )).chunks , md .to_dict ()["chunks" ]),
340+ "sharding-inner" : _sharding_chunk_shape ,
341+ }
342+ """`ArrayV2Metadata` and `ShardingCodec` read a chunk shape as an array shape, returning
343+ the chunk shape and the value `to_dict` writes for it."""
344+
345+
346+ @pytest .mark .parametrize ("site" , CHUNK_SHAPE_SITES )
347+ @pytest .mark .parametrize (
348+ ("chunks" , "expected" ),
349+ [
350+ ((4 ,), (4 ,)),
351+ ([4 ], (4 ,)),
352+ (4 , (4 ,)),
353+ (np .int64 (4 ), (4 ,)),
354+ ((np .int64 (4 ),), (4 ,)),
355+ (np .array ([4 ]), (4 ,)),
356+ ((True ,), (1 ,)),
357+ ((0 ,), (0 ,)),
358+ ((False ,), (0 ,)),
359+ (range (4 , 5 ), (4 ,)),
360+ ],
361+ )
362+ def test_chunk_shape_read_as_array_shape (
363+ site : str , chunks : object , expected : tuple [int , ...]
364+ ) -> None :
365+ """`ArrayV2Metadata` and `ShardingCodec` read their chunk shape as `parse_shapelike`
366+ reads an array shape: an integer or an iterable of non-negative integers, including
367+ NumPy integers and bools. A chunk size of 0 is written back as given; reading a
368+ stored 0 is `zarr.core.metadata.upgrades`' business."""
369+ parsed , written = CHUNK_SHAPE_SITES [site ](chunks )
370+ assert parsed == expected
371+ assert all (type (size ) is int for size in parsed )
372+ assert written == expected
373+
374+
375+ @pytest .mark .parametrize ("site" , CHUNK_SHAPE_SITES )
376+ def test_chunk_shape_read_as_array_shape_rejects_negative (site : str ) -> None :
377+ with pytest .raises (ValueError , match = "Expected all values to be non-negative" ):
378+ CHUNK_SHAPE_SITES [site ]((- 1 ,))
379+
380+
381+ @pytest .mark .parametrize ("site" , CHUNK_SHAPE_SITES )
382+ @pytest .mark .parametrize ("chunks" , [(4.0 ,), "4" , None ])
383+ def test_chunk_shape_read_as_array_shape_rejects_non_integer (site : str , chunks : object ) -> None :
384+ with pytest .raises (TypeError , match = "Expected an" ):
385+ CHUNK_SHAPE_SITES [site ](chunks )
328386
329387
330388def _rewrite_doc (path : Path , zarr_format : Literal [2 , 3 ], edit : Any ) -> None :
0 commit comments