1
1
import tempfile
2
+ from collections .abc import Callable , Generator
2
3
from pathlib import Path
4
+ from typing import Any , Literal
3
5
4
6
import pytest
5
7
from _pytest .compat import LEGACY_PATH
21
23
@pytest .fixture (
22
24
params = ["none" , "temp_dir_str" , "temp_dir_path" , "store_path" , "memory_store" , "dict" ]
23
25
)
24
- def store_like (request ):
26
+ def store_like (
27
+ request : pytest .FixtureRequest ,
28
+ ) -> Generator [None | str | Path | StorePath | MemoryStore | dict [Any , Any ], None , None ]:
25
29
if request .param == "none" :
26
30
yield None
27
31
elif request .param == "temp_dir_str" :
@@ -42,7 +46,7 @@ def store_like(request):
42
46
@pytest .mark .parametrize ("write_group" , [True , False ])
43
47
@pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
44
48
async def test_contains_group (
45
- local_store , path : str , write_group : bool , zarr_format : ZarrFormat
49
+ local_store : LocalStore , path : str , write_group : bool , zarr_format : ZarrFormat
46
50
) -> None :
47
51
"""
48
52
Test that the contains_group method correctly reports the existence of a group.
@@ -58,7 +62,7 @@ async def test_contains_group(
58
62
@pytest .mark .parametrize ("write_array" , [True , False ])
59
63
@pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
60
64
async def test_contains_array (
61
- local_store , path : str , write_array : bool , zarr_format : ZarrFormat
65
+ local_store : LocalStore , path : str , write_array : bool , zarr_format : ZarrFormat
62
66
) -> None :
63
67
"""
64
68
Test that the contains array method correctly reports the existence of an array.
@@ -71,13 +75,15 @@ async def test_contains_array(
71
75
72
76
73
77
@pytest .mark .parametrize ("func" , [contains_array , contains_group ])
74
- async def test_contains_invalid_format_raises (local_store , func : callable ) -> None :
78
+ async def test_contains_invalid_format_raises (
79
+ local_store : LocalStore , func : Callable [[Any ], Any ]
80
+ ) -> None :
75
81
"""
76
82
Test contains_group and contains_array raise errors for invalid zarr_formats
77
83
"""
78
84
store_path = StorePath (local_store )
79
85
with pytest .raises (ValueError ):
80
- assert await func (store_path , zarr_format = "3.0" )
86
+ assert await func (store_path , zarr_format = "3.0" ) # type: ignore[call-arg]
81
87
82
88
83
89
@pytest .mark .parametrize ("path" , [None , "" , "bar" ])
@@ -113,40 +119,48 @@ async def test_make_store_path_local(
113
119
@pytest .mark .parametrize ("path" , [None , "" , "bar" ])
114
120
@pytest .mark .parametrize ("mode" , ["r" , "w" ])
115
121
async def test_make_store_path_store_path (
116
- tmpdir : LEGACY_PATH , path : str , mode : AccessModeLiteral
122
+ tmp_path : Path , path : str , mode : AccessModeLiteral
117
123
) -> None :
118
124
"""
119
125
Test invoking make_store_path when the input is another store_path. In particular we want to ensure
120
126
that a new path is handled correctly.
121
127
"""
122
128
ro = mode == "r"
123
- store_like = await StorePath .open (LocalStore (str (tmpdir ), read_only = ro ), path = "root" , mode = mode )
129
+ store_like = await StorePath .open (
130
+ LocalStore (str (tmp_path ), read_only = ro ), path = "root" , mode = mode
131
+ )
124
132
store_path = await make_store_path (store_like , path = path , mode = mode )
125
133
assert isinstance (store_path .store , LocalStore )
126
- assert Path (store_path .store .root ) == Path ( tmpdir )
134
+ assert Path (store_path .store .root ) == tmp_path
127
135
path_normalized = normalize_path (path )
128
136
assert store_path .path == (store_like / path_normalized ).path
129
137
assert store_path .read_only == ro
130
138
131
139
132
140
@pytest .mark .parametrize ("modes" , [(True , "w" ), (False , "x" )])
133
- async def test_store_path_invalid_mode_raises (tmpdir : LEGACY_PATH , modes : tuple ) -> None :
141
+ async def test_store_path_invalid_mode_raises (
142
+ tmp_path : Path , modes : tuple [bool , Literal ["w" , "x" ]]
143
+ ) -> None :
134
144
"""
135
145
Test that ValueErrors are raise for invalid mode.
136
146
"""
137
147
with pytest .raises (ValueError ):
138
- await StorePath .open (LocalStore (str (tmpdir ), read_only = modes [0 ]), path = None , mode = modes [1 ])
148
+ await StorePath .open (
149
+ LocalStore (str (tmp_path ), read_only = modes [0 ]),
150
+ path = "" ,
151
+ mode = modes [1 ], # type:ignore[arg-type]
152
+ )
139
153
140
154
141
155
async def test_make_store_path_invalid () -> None :
142
156
"""
143
157
Test that invalid types raise TypeError
144
158
"""
145
159
with pytest .raises (TypeError ):
146
- await make_store_path (1 ) # type: ignore[arg-type]
160
+ await make_store_path (1 )
147
161
148
162
149
- async def test_make_store_path_fsspec (monkeypatch ) -> None :
163
+ async def test_make_store_path_fsspec () -> None :
150
164
pytest .importorskip ("fsspec" )
151
165
pytest .importorskip ("requests" )
152
166
pytest .importorskip ("aiohttp" )
@@ -161,7 +175,7 @@ async def test_make_store_path_storage_options_raises(store_like: StoreLike) ->
161
175
162
176
async def test_unsupported () -> None :
163
177
with pytest .raises (TypeError , match = "Unsupported type for store_like: 'int'" ):
164
- await make_store_path (1 ) # type: ignore[arg-type]
178
+ await make_store_path (1 )
165
179
166
180
167
181
@pytest .mark .parametrize (
@@ -184,12 +198,12 @@ def test_normalize_path_upath() -> None:
184
198
assert normalize_path (upath .UPath ("foo/bar" )) == "foo/bar"
185
199
186
200
187
- def test_normalize_path_none ():
201
+ def test_normalize_path_none () -> None :
188
202
assert normalize_path (None ) == ""
189
203
190
204
191
205
@pytest .mark .parametrize ("path" , ["." , ".." ])
192
- def test_normalize_path_invalid (path : str ):
206
+ def test_normalize_path_invalid (path : str ) -> None :
193
207
with pytest .raises (ValueError ):
194
208
normalize_path (path )
195
209
@@ -230,7 +244,7 @@ def test_invalid(paths: tuple[str, str]) -> None:
230
244
_normalize_paths (paths )
231
245
232
246
233
- def test_normalize_path_keys ():
247
+ def test_normalize_path_keys () -> None :
234
248
"""
235
249
Test that ``_normalize_path_keys`` just applies the normalize_path function to each key of its
236
250
input
@@ -272,10 +286,10 @@ def test_different_open_mode(tmp_path: LEGACY_PATH) -> None:
272
286
273
287
# Test with a store that doesn't implement .with_read_only()
274
288
zarr_path = tmp_path / "foo.zarr"
275
- store = ZipStore (zarr_path , mode = "w" )
276
- zarr .create ((100 ,), store = store , zarr_format = 2 , path = "a" )
289
+ zip_store = ZipStore (zarr_path , mode = "w" )
290
+ zarr .create ((100 ,), store = zip_store , zarr_format = 2 , path = "a" )
277
291
with pytest .raises (
278
292
ValueError ,
279
293
match = "Store is not read-only but mode is 'r'. Unable to create a read-only copy of the store. Please use a read-only store or a storage class that implements .with_read_only()." ,
280
294
):
281
- zarr .open_array (store = store , path = "a" , zarr_format = 2 , mode = "r" )
295
+ zarr .open_array (store = zip_store , path = "a" , zarr_format = 2 , mode = "r" )
0 commit comments