11import lzma
22from pathlib import Path
3+ from typing import Literal , cast
34
45import numcodecs
56import numcodecs .abc
7+ import numpy as np
68import pytest
79from numcodecs .zarr3 import LZMA , Delta
810
911import zarr
1012from tests .test_cli .conftest import create_nested_zarr
1113from zarr .abc .codec import Codec
12- from zarr .abc .store import Store
1314from zarr .codecs .blosc import BloscCodec
1415from zarr .codecs .bytes import BytesCodec
1516from zarr .codecs .gzip import GzipCodec
1617from zarr .codecs .transpose import TransposeCodec
1718from zarr .codecs .zstd import ZstdCodec
19+ from zarr .core .array import Array
1820from zarr .core .chunk_grids import RegularChunkGrid
1921from zarr .core .chunk_key_encodings import V2ChunkKeyEncoding
20- from zarr .core .common import ZarrFormat
21- from zarr .core .dtype .npy .int import BaseInt , UInt8 , UInt16
22+ from zarr .core .common import JSON , ZarrFormat
23+ from zarr .core .dtype .npy .int import UInt8 , UInt16
24+ from zarr .core .group import Group
2225from zarr .storage ._local import LocalStore
2326
2427typer_testing = pytest .importorskip (
3134runner = typer_testing .CliRunner ()
3235
3336
34- def test_migrate_array (local_store : Store ) -> None :
37+ def test_migrate_array (local_store : LocalStore ) -> None :
3538 shape = (10 , 10 )
3639 chunks = (10 , 10 )
3740 dtype = "uint16"
3841 compressors = numcodecs .Blosc (cname = "zstd" , clevel = 3 , shuffle = 1 )
3942 fill_value = 2
40- attributes = {"baz" : 42 , "qux" : [1 , 4 , 7 , 12 ]}
43+ attributes = cast ( dict [ str , JSON ], {"baz" : 42 , "qux" : [1 , 4 , 7 , 12 ]})
4144
4245 zarr .create_array (
4346 store = local_store ,
@@ -72,7 +75,7 @@ def test_migrate_array(local_store: Store) -> None:
7275 assert metadata .storage_transformers == ()
7376
7477
75- def test_migrate_group (local_store : Store ) -> None :
78+ def test_migrate_group (local_store : LocalStore ) -> None :
7679 attributes = {"baz" : 42 , "qux" : [1 , 4 , 7 , 12 ]}
7780 zarr .create_group (store = local_store , zarr_format = 2 , attributes = attributes )
7881
@@ -90,7 +93,7 @@ def test_migrate_group(local_store: Store) -> None:
9093
9194@pytest .mark .parametrize ("separator" , ["." , "/" ])
9295def test_migrate_nested_groups_and_arrays_in_place (
93- local_store : Store , separator : str , expected_v3_metadata : list [Path ]
96+ local_store : LocalStore , separator : str , expected_v3_metadata : list [Path ]
9497) -> None :
9598 """Test that zarr.json are made at the correct points in a hierarchy of groups and arrays
9699 (including when there are additional dirs due to using a / separator)"""
@@ -108,7 +111,7 @@ def test_migrate_nested_groups_and_arrays_in_place(
108111 # Check converted zarr can be opened + metadata accessed at all levels
109112 zarr_array = zarr .open (local_store .root , zarr_format = 3 )
110113 for path in paths :
111- zarr_v3 = zarr_array [path ]
114+ zarr_v3 = cast ( Array | Group , zarr_array [path ])
112115 metadata = zarr_v3 .metadata
113116 assert metadata .zarr_format == 3
114117 assert metadata .attributes == attributes
@@ -151,7 +154,7 @@ async def test_migrate_nested_groups_and_arrays_separate_location(
151154
152155
153156def test_remove_v2_metadata_option_in_place (
154- local_store : Store , expected_paths_v3_metadata : list [Path ]
157+ local_store : LocalStore , expected_paths_v3_metadata : list [Path ]
155158) -> None :
156159 create_nested_zarr (local_store )
157160
@@ -198,7 +201,7 @@ async def test_remove_v2_metadata_option_separate_location(
198201
199202
200203def test_overwrite_option_in_place (
201- local_store : Store , expected_paths_v2_v3_metadata : list [Path ]
204+ local_store : LocalStore , expected_paths_v2_v3_metadata : list [Path ]
202205) -> None :
203206 create_nested_zarr (local_store )
204207
@@ -253,7 +256,7 @@ async def test_overwrite_option_separate_location(
253256
254257@pytest .mark .parametrize ("separator" , ["." , "/" ])
255258def test_migrate_sub_group (
256- local_store : Store , separator : str , expected_v3_metadata : list [Path ]
259+ local_store : LocalStore , separator : str , expected_v3_metadata : list [Path ]
257260) -> None :
258261 """Test that only arrays/groups within group_1 are converted (+ no other files in store)"""
259262
@@ -305,7 +308,7 @@ def test_migrate_sub_group(
305308 ids = ["blosc" , "zstd" , "gzip" , "numcodecs-compressor" ],
306309)
307310def test_migrate_compressor (
308- local_store : Store , compressor_v2 : numcodecs .abc .Codec , compressor_v3 : Codec
311+ local_store : LocalStore , compressor_v2 : numcodecs .abc .Codec , compressor_v3 : Codec
309312) -> None :
310313 zarr_array = zarr .create_array (
311314 store = local_store ,
@@ -322,17 +325,17 @@ def test_migrate_compressor(
322325 assert result .exit_code == 0
323326 assert (local_store .root / "zarr.json" ).exists ()
324327
325- zarr_array = zarr .open (local_store .root , zarr_format = 3 )
328+ zarr_array = zarr .open_array (local_store .root , zarr_format = 3 )
326329 metadata = zarr_array .metadata
327330 assert metadata .zarr_format == 3
328331 assert metadata .codecs == (
329332 BytesCodec (endian = "little" ),
330333 compressor_v3 ,
331334 )
332- assert (zarr_array [:] == 1 ). all ( )
335+ assert np . all (zarr_array [:] == 1 )
333336
334337
335- def test_migrate_filter (local_store : Store ) -> None :
338+ def test_migrate_filter (local_store : LocalStore ) -> None :
336339 filter_v2 = numcodecs .Delta (dtype = "<u2" , astype = "<u2" )
337340 filter_v3 = Delta (dtype = "<u2" , astype = "<u2" )
338341
@@ -351,7 +354,7 @@ def test_migrate_filter(local_store: Store) -> None:
351354 assert result .exit_code == 0
352355 assert (local_store .root / "zarr.json" ).exists ()
353356
354- zarr_array = zarr .open (local_store .root , zarr_format = 3 )
357+ zarr_array = zarr .open_array (local_store .root , zarr_format = 3 )
355358 metadata = zarr_array .metadata
356359 assert metadata .zarr_format == 3
357360 assert metadata .codecs == (
@@ -368,7 +371,7 @@ def test_migrate_filter(local_store: Store) -> None:
368371 ],
369372)
370373def test_migrate_C_vs_F_order (
371- local_store : Store , order : str , expected_codecs : tuple [Codec ]
374+ local_store : LocalStore , order : Literal [ "C" , "F" ] , expected_codecs : tuple [Codec ]
372375) -> None :
373376 zarr_array = zarr .create_array (
374377 store = local_store ,
@@ -386,11 +389,11 @@ def test_migrate_C_vs_F_order(
386389 assert result .exit_code == 0
387390 assert (local_store .root / "zarr.json" ).exists ()
388391
389- zarr_array = zarr .open (local_store .root , zarr_format = 3 )
392+ zarr_array = zarr .open_array (local_store .root , zarr_format = 3 )
390393 metadata = zarr_array .metadata
391394 assert metadata .zarr_format == 3
392395 assert metadata .codecs == expected_codecs
393- assert (zarr_array [:] == 1 ). all ( )
396+ assert np . all (zarr_array [:] == 1 )
394397
395398
396399@pytest .mark .parametrize (
@@ -402,7 +405,10 @@ def test_migrate_C_vs_F_order(
402405 ids = ["single_byte" , "multi_byte" ],
403406)
404407def test_migrate_endian (
405- local_store : Store , dtype : str , expected_data_type : BaseInt , expected_codecs : tuple [Codec ]
408+ local_store : LocalStore ,
409+ dtype : str ,
410+ expected_data_type : UInt8 | UInt16 ,
411+ expected_codecs : tuple [Codec ],
406412) -> None :
407413 zarr_array = zarr .create_array (
408414 store = local_store ,
@@ -419,16 +425,16 @@ def test_migrate_endian(
419425 assert result .exit_code == 0
420426 assert (local_store .root / "zarr.json" ).exists ()
421427
422- zarr_array = zarr .open (local_store .root , zarr_format = 3 )
428+ zarr_array = zarr .open_array (local_store .root , zarr_format = 3 )
423429 metadata = zarr_array .metadata
424430 assert metadata .zarr_format == 3
425431 assert metadata .data_type == expected_data_type
426432 assert metadata .codecs == expected_codecs
427- assert (zarr_array [:] == 1 ). all ( )
433+ assert np . all (zarr_array [:] == 1 )
428434
429435
430436@pytest .mark .parametrize ("node_type" , ["array" , "group" ])
431- def test_migrate_v3 (local_store : Store , node_type : str ) -> None :
437+ def test_migrate_v3 (local_store : LocalStore , node_type : str ) -> None :
432438 """Attempting to convert a v3 array/group should always fail"""
433439
434440 if node_type == "array" :
@@ -444,7 +450,7 @@ def test_migrate_v3(local_store: Store, node_type: str) -> None:
444450 assert str (result .exception ) == "Only arrays / groups with zarr v2 metadata can be converted"
445451
446452
447- def test_migrate_unknown_codec (local_store : Store ) -> None :
453+ def test_migrate_unknown_codec (local_store : LocalStore ) -> None :
448454 """Attempting to convert a codec without a v3 equivalent should always fail"""
449455
450456 zarr .create_array (
@@ -465,7 +471,7 @@ def test_migrate_unknown_codec(local_store: Store) -> None:
465471 )
466472
467473
468- def test_migrate_incorrect_filter (local_store : Store ) -> None :
474+ def test_migrate_incorrect_filter (local_store : LocalStore ) -> None :
469475 """Attempting to convert a filter (which is the wrong type of codec) should always fail"""
470476
471477 zarr .create_array (
@@ -486,7 +492,7 @@ def test_migrate_incorrect_filter(local_store: Store) -> None:
486492 )
487493
488494
489- def test_migrate_incorrect_compressor (local_store : Store ) -> None :
495+ def test_migrate_incorrect_compressor (local_store : LocalStore ) -> None :
490496 """Attempting to convert a compressor (which is the wrong type of codec) should always fail"""
491497
492498 zarr .create_array (
@@ -509,7 +515,9 @@ def test_migrate_incorrect_compressor(local_store: Store) -> None:
509515
510516
511517@pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
512- def test_remove_metadata_fails_without_force (local_store : Store , zarr_format : ZarrFormat ) -> None :
518+ def test_remove_metadata_fails_without_force (
519+ local_store : LocalStore , zarr_format : ZarrFormat
520+ ) -> None :
513521 """Test removing metadata (when no alternate metadata is present) fails without --force."""
514522
515523 create_nested_zarr (local_store , zarr_format = zarr_format )
@@ -522,7 +530,7 @@ def test_remove_metadata_fails_without_force(local_store: Store, zarr_format: Za
522530
523531@pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
524532def test_remove_metadata_succeeds_with_force (
525- local_store : Store , zarr_format : ZarrFormat , expected_paths_no_metadata : list [Path ]
533+ local_store : LocalStore , zarr_format : ZarrFormat , expected_paths_no_metadata : list [Path ]
526534) -> None :
527535 """Test removing metadata (when no alternate metadata is present) succeeds with --force."""
528536
@@ -539,7 +547,7 @@ def test_remove_metadata_succeeds_with_force(
539547
540548
541549def test_remove_metadata_sub_group (
542- local_store : Store , expected_paths_no_metadata : list [Path ]
550+ local_store : LocalStore , expected_paths_no_metadata : list [Path ]
543551) -> None :
544552 """Test only v2 metadata within group_1 is removed and rest remains un-changed."""
545553
@@ -567,7 +575,7 @@ def test_remove_metadata_sub_group(
567575 [("v2" , "expected_paths_v3_metadata" ), ("v3" , "expected_paths_v2_metadata" )],
568576)
569577def test_remove_metadata_after_conversion (
570- local_store : Store ,
578+ local_store : LocalStore ,
571579 request : pytest .FixtureRequest ,
572580 zarr_format : str ,
573581 expected_output_paths : str ,
@@ -591,7 +599,7 @@ def test_remove_metadata_after_conversion(
591599
592600@pytest .mark .parametrize ("cli_command" , ["migrate" , "remove-metadata" ])
593601def test_dry_run (
594- local_store : Store , cli_command : str , expected_paths_v2_metadata : list [Path ]
602+ local_store : LocalStore , cli_command : str , expected_paths_v2_metadata : list [Path ]
595603) -> None :
596604 """Test that all files are un-changed after a dry run"""
597605
0 commit comments