17
17
open ,
18
18
open_consolidated ,
19
19
)
20
+ from zarr .api .synchronous import Group
20
21
from zarr .core .buffer import cpu , default_buffer_prototype
21
22
from zarr .core .group import ConsolidatedMetadata , GroupMetadata
22
23
from zarr .core .metadata import ArrayV3Metadata
25
26
26
27
if TYPE_CHECKING :
27
28
from zarr .abc .store import Store
28
- from zarr .core .common import ZarrFormat
29
+ from zarr .core .common import JSON , ZarrFormat
29
30
30
31
31
32
@pytest .fixture
32
- async def memory_store_with_hierarchy (memory_store : Store ) -> None :
33
+ async def memory_store_with_hierarchy (memory_store : Store ) -> Store :
33
34
g = await group (store = memory_store , attributes = {"foo" : "bar" })
34
35
dtype = "uint8"
35
36
await g .create_array (name = "air" , shape = (1 , 2 , 3 ), dtype = dtype )
@@ -49,15 +50,15 @@ async def memory_store_with_hierarchy(memory_store: Store) -> None:
49
50
50
51
51
52
class TestConsolidated :
52
- async def test_open_consolidated_false_raises (self ):
53
+ async def test_open_consolidated_false_raises (self ) -> None :
53
54
store = zarr .storage .MemoryStore ()
54
55
with pytest .raises (TypeError , match = "use_consolidated" ):
55
- await zarr .api .asynchronous .open_consolidated (store , use_consolidated = False )
56
+ await zarr .api .asynchronous .open_consolidated (store , use_consolidated = False ) # type: ignore[arg-type]
56
57
57
- def test_open_consolidated_false_raises_sync (self ):
58
+ def test_open_consolidated_false_raises_sync (self ) -> None :
58
59
store = zarr .storage .MemoryStore ()
59
60
with pytest .raises (TypeError , match = "use_consolidated" ):
60
- zarr .open_consolidated (store , use_consolidated = False )
61
+ zarr .open_consolidated (store , use_consolidated = False ) # type: ignore[arg-type]
61
62
62
63
async def test_consolidated (self , memory_store_with_hierarchy : Store ) -> None :
63
64
# TODO: Figure out desired keys in
@@ -69,7 +70,7 @@ async def test_consolidated(self, memory_store_with_hierarchy: Store) -> None:
69
70
await consolidate_metadata (memory_store_with_hierarchy )
70
71
group2 = await AsyncGroup .open (memory_store_with_hierarchy )
71
72
72
- array_metadata = {
73
+ array_metadata : dict [ str , JSON ] = {
73
74
"attributes" : {},
74
75
"chunk_key_encoding" : {
75
76
"configuration" : {"separator" : "/" },
@@ -186,13 +187,11 @@ async def test_consolidated(self, memory_store_with_hierarchy: Store) -> None:
186
187
group4 = await open_consolidated (store = memory_store_with_hierarchy )
187
188
assert group4 .metadata == expected
188
189
189
- result_raw = json .loads (
190
- (
191
- await memory_store_with_hierarchy .get (
192
- "zarr.json" , prototype = default_buffer_prototype ()
193
- )
194
- ).to_bytes ()
195
- )["consolidated_metadata" ]
190
+ val = await memory_store_with_hierarchy .get (
191
+ "zarr.json" , prototype = default_buffer_prototype ()
192
+ )
193
+ assert val is not None
194
+ result_raw = json .loads ((val ).to_bytes ())["consolidated_metadata" ]
196
195
assert result_raw ["kind" ] == "inline"
197
196
assert sorted (result_raw ["metadata" ]) == [
198
197
"air" ,
@@ -206,7 +205,7 @@ async def test_consolidated(self, memory_store_with_hierarchy: Store) -> None:
206
205
"time" ,
207
206
]
208
207
209
- def test_consolidated_sync (self , memory_store ) :
208
+ def test_consolidated_sync (self , memory_store : zarr . storage . MemoryStore ) -> None :
210
209
g = zarr .api .synchronous .group (store = memory_store , attributes = {"foo" : "bar" })
211
210
dtype = "uint8"
212
211
g .create_array (name = "air" , shape = (1 , 2 , 3 ), dtype = dtype )
@@ -215,9 +214,9 @@ def test_consolidated_sync(self, memory_store):
215
214
g .create_array (name = "time" , shape = (3 ,), dtype = dtype )
216
215
217
216
zarr .api .synchronous .consolidate_metadata (memory_store )
218
- group2 = zarr . api . synchronous . Group .open (memory_store )
217
+ group2 = Group .open (memory_store )
219
218
220
- array_metadata = {
219
+ array_metadata : dict [ str , JSON ] = {
221
220
"attributes" : {},
222
221
"chunk_key_encoding" : {
223
222
"configuration" : {"separator" : "/" },
@@ -306,8 +305,8 @@ async def test_non_root_node(self, memory_store_with_hierarchy: Store) -> None:
306
305
assert "air" not in child .metadata .consolidated_metadata .metadata
307
306
assert "grandchild" in child .metadata .consolidated_metadata .metadata
308
307
309
- def test_consolidated_metadata_from_dict (self ):
310
- data = {"must_understand" : False }
308
+ def test_consolidated_metadata_from_dict (self ) -> None :
309
+ data : dict [ str , JSON ] = {"must_understand" : False }
311
310
312
311
# missing kind
313
312
with pytest .raises (ValueError , match = "kind='None'" ):
@@ -329,16 +328,16 @@ def test_consolidated_metadata_from_dict(self):
329
328
data ["metadata" ] = {}
330
329
ConsolidatedMetadata .from_dict (data )
331
330
332
- def test_flatten (self ):
333
- array_metadata = {
331
+ def test_flatten (self ) -> None :
332
+ array_metadata : dict [ str , JSON ] = {
334
333
"attributes" : {},
335
334
"chunk_key_encoding" : {
336
335
"configuration" : {"separator" : "/" },
337
336
"name" : "default" ,
338
337
},
339
338
"codecs" : ({"configuration" : {"endian" : "little" }, "name" : "bytes" },),
340
339
"data_type" : "float64" ,
341
- "fill_value" : np . float64 ( 0.0 ) ,
340
+ "fill_value" : 0 ,
342
341
"node_type" : "array" ,
343
342
# "shape": (1, 2, 3),
344
343
"zarr_format" : 3 ,
@@ -407,6 +406,17 @@ def test_flatten(self):
407
406
},
408
407
)
409
408
result = metadata .flattened_metadata
409
+ assert isinstance (metadata .metadata ["child" ], GroupMetadata )
410
+ assert isinstance (metadata .metadata ["child" ].consolidated_metadata , ConsolidatedMetadata )
411
+ assert isinstance (
412
+ metadata .metadata ["child" ].consolidated_metadata .metadata ["grandchild" ], GroupMetadata
413
+ )
414
+ assert isinstance (
415
+ metadata .metadata ["child" ]
416
+ .consolidated_metadata .metadata ["grandchild" ]
417
+ .consolidated_metadata ,
418
+ ConsolidatedMetadata ,
419
+ )
410
420
expected = {
411
421
"air" : metadata .metadata ["air" ],
412
422
"lat" : metadata .metadata ["lat" ],
@@ -426,7 +436,7 @@ def test_flatten(self):
426
436
}
427
437
assert result == expected
428
438
429
- def test_invalid_metadata_raises (self ):
439
+ def test_invalid_metadata_raises (self ) -> None :
430
440
payload = {
431
441
"kind" : "inline" ,
432
442
"must_understand" : False ,
@@ -436,9 +446,9 @@ def test_invalid_metadata_raises(self):
436
446
}
437
447
438
448
with pytest .raises (TypeError , match = "key='foo', type='list'" ):
439
- ConsolidatedMetadata .from_dict (payload )
449
+ ConsolidatedMetadata .from_dict (payload ) # type: ignore[arg-type]
440
450
441
- def test_to_dict_empty (self ):
451
+ def test_to_dict_empty (self ) -> None :
442
452
meta = ConsolidatedMetadata (
443
453
metadata = {
444
454
"empty" : GroupMetadata (
@@ -467,7 +477,7 @@ def test_to_dict_empty(self):
467
477
assert result == expected
468
478
469
479
@pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
470
- async def test_open_consolidated_raises_async (self , zarr_format : ZarrFormat ):
480
+ async def test_open_consolidated_raises_async (self , zarr_format : ZarrFormat ) -> None :
471
481
store = zarr .storage .MemoryStore ()
472
482
await AsyncGroup .from_store (store , zarr_format = zarr_format )
473
483
with pytest .raises (ValueError ):
@@ -485,12 +495,15 @@ async def v2_consolidated_metadata_empty_dataset(
485
495
b'{"metadata":{".zgroup":{"zarr_format":2}},"zarr_consolidated_format":1}'
486
496
)
487
497
return AsyncGroup ._from_bytes_v2 (
488
- None , zgroup_bytes , zattrs_bytes = None , consolidated_metadata_bytes = zmetadata_bytes
498
+ None , # type: ignore[arg-type]
499
+ zgroup_bytes ,
500
+ zattrs_bytes = None ,
501
+ consolidated_metadata_bytes = zmetadata_bytes ,
489
502
)
490
503
491
504
async def test_consolidated_metadata_backwards_compatibility (
492
- self , v2_consolidated_metadata_empty_dataset
493
- ):
505
+ self , v2_consolidated_metadata_empty_dataset : AsyncGroup
506
+ ) -> None :
494
507
"""
495
508
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
496
509
"""
@@ -500,7 +513,7 @@ async def test_consolidated_metadata_backwards_compatibility(
500
513
result = await zarr .api .asynchronous .open_consolidated (store , zarr_format = 2 )
501
514
assert result .metadata == v2_consolidated_metadata_empty_dataset .metadata
502
515
503
- async def test_consolidated_metadata_v2 (self ):
516
+ async def test_consolidated_metadata_v2 (self ) -> None :
504
517
store = zarr .storage .MemoryStore ()
505
518
g = await AsyncGroup .from_store (store , attributes = {"key" : "root" }, zarr_format = 2 )
506
519
dtype = "uint8"
@@ -622,7 +635,7 @@ async def test_use_consolidated_for_children_members(
622
635
@pytest .mark .parametrize ("fill_value" , [np .nan , np .inf , - np .inf ])
623
636
async def test_consolidated_metadata_encodes_special_chars (
624
637
memory_store : Store , zarr_format : ZarrFormat , fill_value : float
625
- ):
638
+ ) -> None :
626
639
root = await group (store = memory_store , zarr_format = zarr_format )
627
640
_child = await root .create_group ("child" , attributes = {"test" : fill_value })
628
641
_time = await root .create_array ("time" , shape = (12 ,), dtype = np .float64 , fill_value = fill_value )
0 commit comments