1
1
from __future__ import annotations
2
2
3
+ import re
3
4
from typing import TYPE_CHECKING
4
5
5
6
import zarr .codecs
@@ -72,13 +73,19 @@ def test_create(memory_store: Store) -> None:
72
73
73
74
# TODO: parametrize over everything this function takes
74
75
@pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
75
- def test_create_array (store : Store ) -> None :
76
+ def test_create_array (store : Store , zarr_format : ZarrFormat ) -> None :
76
77
attrs : dict [str , JSON ] = {"foo" : 100 } # explicit type annotation to avoid mypy error
77
78
shape = (10 , 10 )
78
79
path = "foo"
79
80
data_val = 1
80
81
array_w = create_array (
81
- store , name = path , shape = shape , attributes = attrs , chunks = shape , dtype = "uint8"
82
+ store ,
83
+ name = path ,
84
+ shape = shape ,
85
+ attributes = attrs ,
86
+ chunks = shape ,
87
+ dtype = "uint8" ,
88
+ zarr_format = zarr_format ,
82
89
)
83
90
array_w [:] = data_val
84
91
assert array_w .shape == shape
@@ -87,18 +94,27 @@ def test_create_array(store: Store) -> None:
87
94
88
95
89
96
@pytest .mark .parametrize ("write_empty_chunks" , [True , False ])
90
- def test_write_empty_chunks_warns (write_empty_chunks : bool ) -> None :
97
+ def test_write_empty_chunks_warns (write_empty_chunks : bool , zarr_format : ZarrFormat ) -> None :
91
98
"""
92
99
Test that using the `write_empty_chunks` kwarg on array access will raise a warning.
93
100
"""
94
101
match = "The `write_empty_chunks` keyword argument .*"
95
102
with pytest .warns (RuntimeWarning , match = match ):
96
103
_ = zarr .array (
97
- data = np .arange (10 ), shape = (10 ,), dtype = "uint8" , write_empty_chunks = write_empty_chunks
104
+ data = np .arange (10 ),
105
+ shape = (10 ,),
106
+ dtype = "uint8" ,
107
+ write_empty_chunks = write_empty_chunks ,
108
+ zarr_format = zarr_format ,
98
109
)
99
110
100
111
with pytest .warns (RuntimeWarning , match = match ):
101
- _ = zarr .create (shape = (10 ,), dtype = "uint8" , write_empty_chunks = write_empty_chunks )
112
+ _ = zarr .create (
113
+ shape = (10 ,),
114
+ dtype = "uint8" ,
115
+ write_empty_chunks = write_empty_chunks ,
116
+ zarr_format = zarr_format ,
117
+ )
102
118
103
119
104
120
@pytest .mark .parametrize ("path" , ["foo" , "/" , "/foo" , "///foo/bar" ])
@@ -115,18 +131,18 @@ def test_open_normalized_path(
115
131
assert node .path == normalize_path (path )
116
132
117
133
118
- async def test_open_array (memory_store : MemoryStore ) -> None :
134
+ async def test_open_array (memory_store : MemoryStore , zarr_format : ZarrFormat ) -> None :
119
135
store = memory_store
120
136
121
137
# open array, create if doesn't exist
122
- z = open (store = store , shape = 100 )
138
+ z = open (store = store , shape = 100 , zarr_format = zarr_format )
123
139
assert isinstance (z , Array )
124
140
assert z .shape == (100 ,)
125
141
126
142
# open array, overwrite
127
143
# store._store_dict = {}
128
144
store = MemoryStore ()
129
- z = open (store = store , shape = 200 )
145
+ z = open (store = store , shape = 200 , zarr_format = zarr_format )
130
146
assert isinstance (z , Array )
131
147
assert z .shape == (200 ,)
132
148
@@ -140,7 +156,16 @@ async def test_open_array(memory_store: MemoryStore) -> None:
140
156
141
157
# path not found
142
158
with pytest .raises (FileNotFoundError ):
143
- open (store = "doesnotexist" , mode = "r" )
159
+ open (store = "doesnotexist" , mode = "r" , zarr_format = zarr_format )
160
+
161
+
162
+ @pytest .mark .parametrize ("store" , ["memory" , "local" , "zip" ], indirect = True )
163
+ def test_v2_and_v3_exist_at_same_path (store : Store ) -> None :
164
+ zarr .create_array (store , shape = (10 ,), dtype = "uint8" , zarr_format = 3 )
165
+ zarr .create_array (store , shape = (10 ,), dtype = "uint8" , zarr_format = 2 )
166
+ msg = f"Both zarr.json (Zarr format 3) and .zarray (Zarr format 2) metadata objects exist at { store } . Zarr v3 will be used."
167
+ with pytest .warns (UserWarning , match = re .escape (msg )):
168
+ zarr .open (store = store , mode = "r" )
144
169
145
170
146
171
@pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
@@ -163,9 +188,9 @@ async def test_open_group(memory_store: MemoryStore) -> None:
163
188
assert "foo" in g
164
189
165
190
# open group, overwrite
166
- # g = open_group(store=store)
167
- # assert isinstance(g, Group)
168
- # assert "foo" not in g
191
+ g = open_group (store = store , mode = "w" )
192
+ assert isinstance (g , Group )
193
+ assert "foo" not in g
169
194
170
195
# open group, read-only
171
196
store_cls = type (store )
@@ -308,7 +333,6 @@ def test_open_with_mode_w_minus(tmp_path: pathlib.Path) -> None:
308
333
zarr .open (store = tmp_path , mode = "w-" )
309
334
310
335
311
- @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
312
336
def test_array_order (zarr_format : ZarrFormat ) -> None :
313
337
arr = zarr .ones (shape = (2 , 2 ), order = None , zarr_format = zarr_format )
314
338
expected = zarr .config .get ("array.order" )
@@ -324,7 +348,6 @@ def test_array_order(zarr_format: ZarrFormat) -> None:
324
348
325
349
326
350
@pytest .mark .parametrize ("order" , ["C" , "F" ])
327
- @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
328
351
def test_array_order_warns (order : MemoryOrder | None , zarr_format : ZarrFormat ) -> None :
329
352
with pytest .warns (RuntimeWarning , match = "The `order` keyword argument .*" ):
330
353
arr = zarr .ones (shape = (2 , 2 ), order = order , zarr_format = zarr_format )
@@ -1095,13 +1118,16 @@ def test_open_falls_back_to_open_group() -> None:
1095
1118
assert group .attrs == {"key" : "value" }
1096
1119
1097
1120
1098
- async def test_open_falls_back_to_open_group_async () -> None :
1121
+ async def test_open_falls_back_to_open_group_async (zarr_format : ZarrFormat ) -> None :
1099
1122
# https://github.com/zarr-developers/zarr-python/issues/2309
1100
1123
store = MemoryStore ()
1101
- await zarr .api .asynchronous .open_group (store , attributes = {"key" : "value" })
1124
+ await zarr .api .asynchronous .open_group (
1125
+ store , attributes = {"key" : "value" }, zarr_format = zarr_format
1126
+ )
1102
1127
1103
1128
group = await zarr .api .asynchronous .open (store = store )
1104
1129
assert isinstance (group , zarr .core .group .AsyncGroup )
1130
+ assert group .metadata .zarr_format == zarr_format
1105
1131
assert group .attrs == {"key" : "value" }
1106
1132
1107
1133
@@ -1137,13 +1163,14 @@ async def test_metadata_validation_error() -> None:
1137
1163
["local" , "memory" , "zip" ],
1138
1164
indirect = True ,
1139
1165
)
1140
- def test_open_array_with_mode_r_plus (store : Store ) -> None :
1166
+ def test_open_array_with_mode_r_plus (store : Store , zarr_format : ZarrFormat ) -> None :
1141
1167
# 'r+' means read/write (must exist)
1142
1168
with pytest .raises (FileNotFoundError ):
1143
- zarr .open_array (store = store , mode = "r+" )
1144
- zarr .ones (store = store , shape = (3 , 3 ))
1169
+ zarr .open_array (store = store , mode = "r+" , zarr_format = zarr_format )
1170
+ zarr .ones (store = store , shape = (3 , 3 ), zarr_format = zarr_format )
1145
1171
z2 = zarr .open_array (store = store , mode = "r+" )
1146
1172
assert isinstance (z2 , Array )
1173
+ assert z2 .metadata .zarr_format == zarr_format
1147
1174
result = z2 [:]
1148
1175
assert isinstance (result , NDArrayLike )
1149
1176
assert (result == 1 ).all ()
0 commit comments