11from __future__ import annotations
22
33import json
4- from typing import TYPE_CHECKING
4+ from typing import TYPE_CHECKING , Any
55
66import numpy as np
77import pytest
1010import zarr .api .asynchronous
1111import zarr .api .synchronous
1212import zarr .storage
13+ from zarr import AsyncGroup
1314from zarr .api .asynchronous import (
14- AsyncGroup ,
1515 consolidate_metadata ,
1616 group ,
1717 open ,
2727
2828if TYPE_CHECKING :
2929 from zarr .abc .store import Store
30- from zarr .core .common import ZarrFormat
30+ from zarr .core .common import JSON , ZarrFormat
3131
3232
3333@pytest .fixture
34- async def memory_store_with_hierarchy (memory_store : Store ) -> None :
34+ async def memory_store_with_hierarchy (memory_store : Store ) -> Store :
3535 g = await group (store = memory_store , attributes = {"foo" : "bar" })
3636 dtype = "uint8"
3737 await g .create_array (name = "air" , shape = (1 , 2 , 3 ), dtype = dtype )
@@ -51,15 +51,15 @@ async def memory_store_with_hierarchy(memory_store: Store) -> None:
5151
5252
5353class TestConsolidated :
54- async def test_open_consolidated_false_raises (self ):
54+ async def test_open_consolidated_false_raises (self ) -> None :
5555 store = zarr .storage .MemoryStore ()
5656 with pytest .raises (TypeError , match = "use_consolidated" ):
57- await zarr .api .asynchronous .open_consolidated (store , use_consolidated = False )
57+ await zarr .api .asynchronous .open_consolidated (store , use_consolidated = False ) # type: ignore[arg-type]
5858
59- def test_open_consolidated_false_raises_sync (self ):
59+ def test_open_consolidated_false_raises_sync (self ) -> None :
6060 store = zarr .storage .MemoryStore ()
6161 with pytest .raises (TypeError , match = "use_consolidated" ):
62- zarr .open_consolidated (store , use_consolidated = False )
62+ zarr .open_consolidated (store , use_consolidated = False ) # type: ignore[arg-type]
6363
6464 async def test_consolidated (self , memory_store_with_hierarchy : Store ) -> None :
6565 # TODO: Figure out desired keys in
@@ -75,7 +75,7 @@ async def test_consolidated(self, memory_store_with_hierarchy: Store) -> None:
7575 await consolidate_metadata (memory_store_with_hierarchy )
7676 group2 = await AsyncGroup .open (memory_store_with_hierarchy )
7777
78- array_metadata = {
78+ array_metadata : dict [ str , JSON ] = {
7979 "attributes" : {},
8080 "chunk_key_encoding" : {
8181 "configuration" : {"separator" : "/" },
@@ -192,13 +192,12 @@ async def test_consolidated(self, memory_store_with_hierarchy: Store) -> None:
192192 group4 = await open_consolidated (store = memory_store_with_hierarchy )
193193 assert group4 .metadata == expected
194194
195- result_raw = json .loads (
196- (
197- await memory_store_with_hierarchy .get (
198- "zarr.json" , prototype = default_buffer_prototype ()
199- )
200- ).to_bytes ()
201- )["consolidated_metadata" ]
195+ buf = await memory_store_with_hierarchy .get (
196+ "zarr.json" , prototype = default_buffer_prototype ()
197+ )
198+ assert buf is not None
199+
200+ result_raw = json .loads (buf .to_bytes ())["consolidated_metadata" ]
202201 assert result_raw ["kind" ] == "inline"
203202 assert sorted (result_raw ["metadata" ]) == [
204203 "air" ,
@@ -212,7 +211,7 @@ async def test_consolidated(self, memory_store_with_hierarchy: Store) -> None:
212211 "time" ,
213212 ]
214213
215- def test_consolidated_sync (self , memory_store ) :
214+ def test_consolidated_sync (self , memory_store : Store ) -> None :
216215 g = zarr .api .synchronous .group (store = memory_store , attributes = {"foo" : "bar" })
217216 dtype = "uint8"
218217 g .create_array (name = "air" , shape = (1 , 2 , 3 ), dtype = dtype )
@@ -225,9 +224,9 @@ def test_consolidated_sync(self, memory_store):
225224 match = "Consolidated metadata is currently not part in the Zarr format 3 specification." ,
226225 ):
227226 zarr .api .synchronous .consolidate_metadata (memory_store )
228- group2 = zarr .api . synchronous . Group .open (memory_store )
227+ group2 = zarr .Group .open (memory_store )
229228
230- array_metadata = {
229+ array_metadata : dict [ str , JSON ] = {
231230 "attributes" : {},
232231 "chunk_key_encoding" : {
233232 "configuration" : {"separator" : "/" },
@@ -320,8 +319,8 @@ async def test_non_root_node(self, memory_store_with_hierarchy: Store) -> None:
320319 assert "air" not in child .metadata .consolidated_metadata .metadata
321320 assert "grandchild" in child .metadata .consolidated_metadata .metadata
322321
323- def test_consolidated_metadata_from_dict (self ):
324- data = {"must_understand" : False }
322+ def test_consolidated_metadata_from_dict (self ) -> None :
323+ data : dict [ str , JSON ] = {"must_understand" : False }
325324
326325 # missing kind
327326 with pytest .raises (ValueError , match = "kind='None'" ):
@@ -343,8 +342,8 @@ def test_consolidated_metadata_from_dict(self):
343342 data ["metadata" ] = {}
344343 ConsolidatedMetadata .from_dict (data )
345344
346- def test_flatten (self ):
347- array_metadata = {
345+ def test_flatten (self ) -> None :
346+ array_metadata : dict [ str , Any ] = {
348347 "attributes" : {},
349348 "chunk_key_encoding" : {
350349 "configuration" : {"separator" : "/" },
@@ -421,27 +420,28 @@ def test_flatten(self):
421420 },
422421 )
423422 result = metadata .flattened_metadata
423+
424424 expected = {
425425 "air" : metadata .metadata ["air" ],
426426 "lat" : metadata .metadata ["lat" ],
427427 "child" : GroupMetadata (
428428 attributes = {"key" : "child" }, consolidated_metadata = ConsolidatedMetadata (metadata = {})
429429 ),
430- "child/array" : metadata .metadata ["child" ].consolidated_metadata .metadata ["array" ],
430+ "child/array" : metadata .metadata ["child" ].consolidated_metadata .metadata ["array" ], # type: ignore[union-attr]
431431 "child/grandchild" : GroupMetadata (
432432 attributes = {"key" : "grandchild" },
433433 consolidated_metadata = ConsolidatedMetadata (metadata = {}),
434434 ),
435435 "child/grandchild/array" : (
436436 metadata .metadata ["child" ]
437- .consolidated_metadata .metadata ["grandchild" ]
437+ .consolidated_metadata .metadata ["grandchild" ] # type: ignore[union-attr]
438438 .consolidated_metadata .metadata ["array" ]
439439 ),
440440 }
441441 assert result == expected
442442
443- def test_invalid_metadata_raises (self ):
444- payload = {
443+ def test_invalid_metadata_raises (self ) -> None :
444+ payload : dict [ str , JSON ] = {
445445 "kind" : "inline" ,
446446 "must_understand" : False ,
447447 "metadata" : {
@@ -452,7 +452,7 @@ def test_invalid_metadata_raises(self):
452452 with pytest .raises (TypeError , match = "key='foo', type='list'" ):
453453 ConsolidatedMetadata .from_dict (payload )
454454
455- def test_to_dict_empty (self ):
455+ def test_to_dict_empty (self ) -> None :
456456 meta = ConsolidatedMetadata (
457457 metadata = {
458458 "empty" : GroupMetadata (
@@ -507,6 +507,7 @@ async def test_to_dict_order(
507507 await zarr .api .asynchronous .consolidate_metadata (memory_store )
508508 g2 = await zarr .api .asynchronous .open_group (store = memory_store )
509509
510+ assert g2 .metadata .consolidated_metadata is not None
510511 assert list (g2 .metadata .consolidated_metadata .metadata ) == ["a" , "b" , "c" ]
511512 assert list (g2 .metadata .consolidated_metadata .flattened_metadata ) == [
512513 "a" ,
@@ -517,7 +518,7 @@ async def test_to_dict_order(
517518 ]
518519
519520 @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
520- async def test_open_consolidated_raises_async (self , zarr_format : ZarrFormat ):
521+ async def test_open_consolidated_raises_async (self , zarr_format : ZarrFormat ) -> None :
521522 store = zarr .storage .MemoryStore ()
522523 await AsyncGroup .from_store (store , zarr_format = zarr_format )
523524 with pytest .raises (ValueError ):
@@ -535,12 +536,15 @@ async def v2_consolidated_metadata_empty_dataset(
535536 b'{"metadata":{".zgroup":{"zarr_format":2}},"zarr_consolidated_format":1}'
536537 )
537538 return AsyncGroup ._from_bytes_v2 (
538- None , zgroup_bytes , zattrs_bytes = None , consolidated_metadata_bytes = zmetadata_bytes
539+ StorePath (memory_store , path = "" ),
540+ zgroup_bytes ,
541+ zattrs_bytes = None ,
542+ consolidated_metadata_bytes = zmetadata_bytes ,
539543 )
540544
541545 async def test_consolidated_metadata_backwards_compatibility (
542- self , v2_consolidated_metadata_empty_dataset
543- ):
546+ self , v2_consolidated_metadata_empty_dataset : AsyncGroup
547+ ) -> None :
544548 """
545549 Test that consolidated metadata handles a missing .zattrs key. This is necessary for backwards compatibility with zarr-python 2.x. See https://github.com/zarr-developers/zarr-python/issues/2694
546550 """
@@ -550,7 +554,7 @@ async def test_consolidated_metadata_backwards_compatibility(
550554 result = await zarr .api .asynchronous .open_consolidated (store , zarr_format = 2 )
551555 assert result .metadata == v2_consolidated_metadata_empty_dataset .metadata
552556
553- async def test_consolidated_metadata_v2 (self ):
557+ async def test_consolidated_metadata_v2 (self ) -> None :
554558 store = zarr .storage .MemoryStore ()
555559 g = await AsyncGroup .from_store (store , attributes = {"key" : "root" }, zarr_format = 2 )
556560 dtype = parse_dtype ("uint8" , zarr_format = 2 )
@@ -638,7 +642,9 @@ async def test_use_consolidated_false(
638642 assert good .metadata .consolidated_metadata
639643 assert sorted (good .metadata .consolidated_metadata .metadata ) == ["a" , "b" ]
640644
641- async def test_stale_child_metadata_ignored (self , memory_store : zarr .storage .MemoryStore ):
645+ async def test_stale_child_metadata_ignored (
646+ self , memory_store : zarr .storage .MemoryStore
647+ ) -> None :
642648 # https://github.com/zarr-developers/zarr-python/issues/2921
643649 # When consolidating metadata, we should ignore any (possibly stale) metadata
644650 # from previous consolidations, *including at child nodes*.
@@ -660,7 +666,7 @@ async def test_stale_child_metadata_ignored(self, memory_store: zarr.storage.Mem
660666
661667 async def test_use_consolidated_for_children_members (
662668 self , memory_store : zarr .storage .MemoryStore
663- ):
669+ ) -> None :
664670 # A test that has *unconsolidated* metadata at the root group, but discovers
665671 # a child group with consolidated metadata.
666672
@@ -690,7 +696,7 @@ async def test_use_consolidated_for_children_members(
690696@pytest .mark .parametrize ("fill_value" , [np .nan , np .inf , - np .inf ])
691697async def test_consolidated_metadata_encodes_special_chars (
692698 memory_store : Store , zarr_format : ZarrFormat , fill_value : float
693- ):
699+ ) -> None :
694700 root = await group (store = memory_store , zarr_format = zarr_format )
695701 _time = await root .create_array ("time" , shape = (12 ,), dtype = np .float64 , fill_value = fill_value )
696702 if zarr_format == 3 :
@@ -728,7 +734,7 @@ def supports_consolidated_metadata(self) -> bool:
728734 return False
729735
730736
731- async def test_consolidate_metadata_raises_for_self_consolidating_stores ():
737+ async def test_consolidate_metadata_raises_for_self_consolidating_stores () -> None :
732738 """Verify calling consolidate_metadata on a non supporting stores raises an error."""
733739
734740 memory_store = NonConsolidatedStore ()
@@ -739,7 +745,7 @@ async def test_consolidate_metadata_raises_for_self_consolidating_stores():
739745 await zarr .api .asynchronous .consolidate_metadata (memory_store )
740746
741747
742- async def test_open_group_in_non_consolidating_stores ():
748+ async def test_open_group_in_non_consolidating_stores () -> None :
743749 memory_store = NonConsolidatedStore ()
744750 root = await zarr .api .asynchronous .create_group (store = memory_store )
745751 await root .create_group ("a/b" )
0 commit comments