Skip to content

Commit 79f1880

Browse files
committed
Fix typing errors in test_consolidated
1 parent 6193fd9 commit 79f1880

File tree

4 files changed

+58
-40
lines changed

4 files changed

+58
-40
lines changed

src/zarr/core/group.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def to_dict(self) -> dict[str, JSON]:
146146
}
147147

148148
@classmethod
149-
def from_dict(cls, data: dict[str, JSON]) -> ConsolidatedMetadata:
149+
def from_dict(cls, data: Mapping[str, JSON]) -> ConsolidatedMetadata:
150150
data = dict(data)
151151

152152
kind = data.get("kind")

tests/test_metadata/test_consolidated.py

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
open,
1818
open_consolidated,
1919
)
20+
from zarr.api.synchronous import Group
2021
from zarr.core.buffer import cpu, default_buffer_prototype
2122
from zarr.core.group import ConsolidatedMetadata, GroupMetadata
2223
from zarr.core.metadata import ArrayV3Metadata
@@ -25,11 +26,11 @@
2526

2627
if TYPE_CHECKING:
2728
from zarr.abc.store import Store
28-
from zarr.core.common import ZarrFormat
29+
from zarr.core.common import JSON, ZarrFormat
2930

3031

3132
@pytest.fixture
32-
async def memory_store_with_hierarchy(memory_store: Store) -> None:
33+
async def memory_store_with_hierarchy(memory_store: Store) -> Store:
3334
g = await group(store=memory_store, attributes={"foo": "bar"})
3435
dtype = "uint8"
3536
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:
4950

5051

5152
class TestConsolidated:
52-
async def test_open_consolidated_false_raises(self):
53+
async def test_open_consolidated_false_raises(self) -> None:
5354
store = zarr.storage.MemoryStore()
5455
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]
5657

57-
def test_open_consolidated_false_raises_sync(self):
58+
def test_open_consolidated_false_raises_sync(self) -> None:
5859
store = zarr.storage.MemoryStore()
5960
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]
6162

6263
async def test_consolidated(self, memory_store_with_hierarchy: Store) -> None:
6364
# TODO: Figure out desired keys in
@@ -69,7 +70,7 @@ async def test_consolidated(self, memory_store_with_hierarchy: Store) -> None:
6970
await consolidate_metadata(memory_store_with_hierarchy)
7071
group2 = await AsyncGroup.open(memory_store_with_hierarchy)
7172

72-
array_metadata = {
73+
array_metadata: dict[str, JSON] = {
7374
"attributes": {},
7475
"chunk_key_encoding": {
7576
"configuration": {"separator": "/"},
@@ -186,13 +187,11 @@ async def test_consolidated(self, memory_store_with_hierarchy: Store) -> None:
186187
group4 = await open_consolidated(store=memory_store_with_hierarchy)
187188
assert group4.metadata == expected
188189

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"]
196195
assert result_raw["kind"] == "inline"
197196
assert sorted(result_raw["metadata"]) == [
198197
"air",
@@ -206,7 +205,7 @@ async def test_consolidated(self, memory_store_with_hierarchy: Store) -> None:
206205
"time",
207206
]
208207

209-
def test_consolidated_sync(self, memory_store):
208+
def test_consolidated_sync(self, memory_store: zarr.storage.MemoryStore) -> None:
210209
g = zarr.api.synchronous.group(store=memory_store, attributes={"foo": "bar"})
211210
dtype = "uint8"
212211
g.create_array(name="air", shape=(1, 2, 3), dtype=dtype)
@@ -215,9 +214,9 @@ def test_consolidated_sync(self, memory_store):
215214
g.create_array(name="time", shape=(3,), dtype=dtype)
216215

217216
zarr.api.synchronous.consolidate_metadata(memory_store)
218-
group2 = zarr.api.synchronous.Group.open(memory_store)
217+
group2 = Group.open(memory_store)
219218

220-
array_metadata = {
219+
array_metadata: dict[str, JSON] = {
221220
"attributes": {},
222221
"chunk_key_encoding": {
223222
"configuration": {"separator": "/"},
@@ -306,8 +305,8 @@ async def test_non_root_node(self, memory_store_with_hierarchy: Store) -> None:
306305
assert "air" not in child.metadata.consolidated_metadata.metadata
307306
assert "grandchild" in child.metadata.consolidated_metadata.metadata
308307

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}
311310

312311
# missing kind
313312
with pytest.raises(ValueError, match="kind='None'"):
@@ -329,16 +328,16 @@ def test_consolidated_metadata_from_dict(self):
329328
data["metadata"] = {}
330329
ConsolidatedMetadata.from_dict(data)
331330

332-
def test_flatten(self):
333-
array_metadata = {
331+
def test_flatten(self) -> None:
332+
array_metadata: dict[str, JSON] = {
334333
"attributes": {},
335334
"chunk_key_encoding": {
336335
"configuration": {"separator": "/"},
337336
"name": "default",
338337
},
339338
"codecs": ({"configuration": {"endian": "little"}, "name": "bytes"},),
340339
"data_type": "float64",
341-
"fill_value": np.float64(0.0),
340+
"fill_value": 0,
342341
"node_type": "array",
343342
# "shape": (1, 2, 3),
344343
"zarr_format": 3,
@@ -407,6 +406,17 @@ def test_flatten(self):
407406
},
408407
)
409408
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+
)
410420
expected = {
411421
"air": metadata.metadata["air"],
412422
"lat": metadata.metadata["lat"],
@@ -426,7 +436,7 @@ def test_flatten(self):
426436
}
427437
assert result == expected
428438

429-
def test_invalid_metadata_raises(self):
439+
def test_invalid_metadata_raises(self) -> None:
430440
payload = {
431441
"kind": "inline",
432442
"must_understand": False,
@@ -436,9 +446,9 @@ def test_invalid_metadata_raises(self):
436446
}
437447

438448
with pytest.raises(TypeError, match="key='foo', type='list'"):
439-
ConsolidatedMetadata.from_dict(payload)
449+
ConsolidatedMetadata.from_dict(payload) # type: ignore[arg-type]
440450

441-
def test_to_dict_empty(self):
451+
def test_to_dict_empty(self) -> None:
442452
meta = ConsolidatedMetadata(
443453
metadata={
444454
"empty": GroupMetadata(
@@ -467,7 +477,7 @@ def test_to_dict_empty(self):
467477
assert result == expected
468478

469479
@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:
471481
store = zarr.storage.MemoryStore()
472482
await AsyncGroup.from_store(store, zarr_format=zarr_format)
473483
with pytest.raises(ValueError):
@@ -485,12 +495,15 @@ async def v2_consolidated_metadata_empty_dataset(
485495
b'{"metadata":{".zgroup":{"zarr_format":2}},"zarr_consolidated_format":1}'
486496
)
487497
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,
489502
)
490503

491504
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:
494507
"""
495508
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
496509
"""
@@ -500,7 +513,7 @@ async def test_consolidated_metadata_backwards_compatibility(
500513
result = await zarr.api.asynchronous.open_consolidated(store, zarr_format=2)
501514
assert result.metadata == v2_consolidated_metadata_empty_dataset.metadata
502515

503-
async def test_consolidated_metadata_v2(self):
516+
async def test_consolidated_metadata_v2(self) -> None:
504517
store = zarr.storage.MemoryStore()
505518
g = await AsyncGroup.from_store(store, attributes={"key": "root"}, zarr_format=2)
506519
dtype = "uint8"
@@ -622,7 +635,7 @@ async def test_use_consolidated_for_children_members(
622635
@pytest.mark.parametrize("fill_value", [np.nan, np.inf, -np.inf])
623636
async def test_consolidated_metadata_encodes_special_chars(
624637
memory_store: Store, zarr_format: ZarrFormat, fill_value: float
625-
):
638+
) -> None:
626639
root = await group(store=memory_store, zarr_format=zarr_format)
627640
_child = await root.create_group("child", attributes={"test": fill_value})
628641
_time = await root.create_array("time", shape=(12,), dtype=np.float64, fill_value=fill_value)

tests/test_metadata/test_v2.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import zarr.api.asynchronous
1010
import zarr.storage
11+
from zarr.core.array import AsyncArray
1112
from zarr.core.buffer import cpu
1213
from zarr.core.buffer.core import default_buffer_prototype
1314
from zarr.core.group import ConsolidatedMetadata, GroupMetadata
@@ -18,6 +19,8 @@
1819
from typing import Any
1920

2021
from zarr.abc.codec import Codec
22+
from zarr.core.common import JSON
23+
2124

2225
import numcodecs
2326

@@ -104,7 +107,7 @@ class TestConsolidated:
104107
async def v2_consolidated_metadata(
105108
self, memory_store: zarr.storage.MemoryStore
106109
) -> zarr.storage.MemoryStore:
107-
zmetadata = {
110+
zmetadata: dict[str, JSON] = {
108111
"metadata": {
109112
".zattrs": {
110113
"Conventions": "COARDS",
@@ -274,6 +277,7 @@ async def test_getitem_consolidated(self, v2_consolidated_metadata):
274277
store = v2_consolidated_metadata
275278
group = await zarr.api.asynchronous.open_consolidated(store=store, zarr_format=2)
276279
air = await group.getitem("air")
280+
assert isinstance(air, AsyncArray[ArrayV2Metadata])
277281
assert air.metadata.shape == (730,)
278282

279283

@@ -335,6 +339,7 @@ def test_structured_dtype_fill_value_serialization(tmp_path, fill_value):
335339

336340
zarr.consolidate_metadata(root_group.store, zarr_format=2)
337341
root_group = zarr.open_group(group_path, mode="r")
342+
assert isinstance(root_group.metadata.consolidated_metadata, ConsolidatedMetadata)
338343
assert (
339344
root_group.metadata.consolidated_metadata.to_dict()["metadata"]["structured_dtype"][
340345
"fill_value"

tests/test_metadata/test_v3.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def test_default_fill_value(dtype_str: str) -> None:
142142
(0j, "complex64"),
143143
],
144144
)
145-
def test_parse_fill_value_valid(fill_value: Any, dtype_str: str) -> None:
145+
def test_parse_fill_value_valid(fill_value: bool | float, dtype_str: str) -> None:
146146
"""
147147
Test that parse_fill_value(fill_value, dtype) casts fill_value to the given dtype.
148148
"""
@@ -156,18 +156,18 @@ def test_parse_fill_value_valid(fill_value: Any, dtype_str: str) -> None:
156156

157157
@pytest.mark.parametrize("fill_value", ["not a valid value"])
158158
@pytest.mark.parametrize("dtype_str", [*int_dtypes, *float_dtypes, *complex_dtypes])
159-
def test_parse_fill_value_invalid_value(fill_value: Any, dtype_str: str) -> None:
159+
def test_parse_fill_value_invalid_value(fill_value: str, dtype_str: str) -> None:
160160
"""
161161
Test that parse_fill_value(fill_value, dtype) raises ValueError for invalid values.
162162
This test excludes bool because the bool constructor takes anything.
163163
"""
164164
with pytest.raises(ValueError):
165-
parse_fill_value(fill_value, dtype_str)
165+
parse_fill_value(fill_value, dtype_str) # type: ignore[arg-type]
166166

167167

168168
@pytest.mark.parametrize("fill_value", [[1.0, 0.0], [0, 1], complex(1, 1), np.complex64(0)])
169169
@pytest.mark.parametrize("dtype_str", [*complex_dtypes])
170-
def test_parse_fill_value_complex(fill_value: Any, dtype_str: str) -> None:
170+
def test_parse_fill_value_complex(fill_value: list[int] | complex, dtype_str: str) -> None:
171171
"""
172172
Test that parse_fill_value(fill_value, dtype) correctly handles complex values represented
173173
as length-2 sequences
@@ -193,18 +193,18 @@ def test_parse_fill_value_complex_invalid(fill_value: Any, dtype_str: str) -> No
193193
f"length {len(fill_value)}."
194194
)
195195
with pytest.raises(ValueError, match=re.escape(match)):
196-
parse_fill_value(fill_value=fill_value, dtype=dtype_str)
196+
parse_fill_value(fill_value=fill_value, dtype=dtype_str) # type; ignore[arg-type]
197197

198198

199199
@pytest.mark.parametrize("fill_value", [{"foo": 10}])
200200
@pytest.mark.parametrize("dtype_str", [*int_dtypes, *float_dtypes, *complex_dtypes])
201-
def test_parse_fill_value_invalid_type(fill_value: Any, dtype_str: str) -> None:
201+
def test_parse_fill_value_invalid_type(fill_value: dict[str, int], dtype_str: str) -> None:
202202
"""
203203
Test that parse_fill_value(fill_value, dtype) raises TypeError for invalid non-sequential types.
204204
This test excludes bool because the bool constructor takes anything.
205205
"""
206206
with pytest.raises(ValueError, match=r"fill value .* is not valid for dtype .*"):
207-
parse_fill_value(fill_value, dtype_str)
207+
parse_fill_value(fill_value, dtype_str) # type: ignore[arg-type]
208208

209209

210210
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)