44import os
55import re
66import tempfile
7+ import warnings
78from collections .abc import Generator , Iterable
8- from contextlib import contextmanager
9- from dataclasses import dataclass
9+ from contextlib import contextmanager , nullcontext
10+ from dataclasses import dataclass , field
1011from json import JSONDecodeError
1112from pathlib import Path
1213from typing import TYPE_CHECKING
@@ -69,6 +70,7 @@ class PartialReadTestCase:
6970 expected_elements : list [str ]
7071 expected_exceptions : type [Exception ] | tuple [type [Exception ] | IOError , ...]
7172 warnings_patterns : list [str ]
73+ zarr_version : int = field (default = 3 )
7274
7375
7476@pytest .fixture (scope = "session" )
@@ -103,6 +105,7 @@ def sdata_with_corrupted_elem_types_zgroup(session_tmp_path: Path) -> PartialRea
103105 expected_elements = not_corrupted ,
104106 expected_exceptions = (JSONDecodeError , ZarrUserWarning ),
105107 warnings_patterns = ["labels: JSONDecodeError" , "Object at" ],
108+ zarr_version = 2 ,
106109 )
107110
108111
@@ -173,6 +176,7 @@ def sdata_with_corrupted_zattrs_elements(session_tmp_path: Path) -> PartialReadT
173176 expected_elements = not_corrupted ,
174177 expected_exceptions = (OSError , JSONDecodeError ),
175178 warnings_patterns = warnings_patterns ,
179+ zarr_version = 2 ,
176180 )
177181
178182
@@ -216,6 +220,7 @@ def sdata_with_corrupted_image_chunks_zarrv2(session_tmp_path: Path) -> PartialR
216220 expected_elements = not_corrupted ,
217221 expected_exceptions = (ArrayNotFoundError ,),
218222 warnings_patterns = [rf"images/{ corrupted } : ArrayNotFoundError" ],
223+ zarr_version = 2 ,
219224 )
220225
221226
@@ -264,6 +269,7 @@ def sdata_with_corrupted_parquet_zarrv2(session_tmp_path: Path) -> PartialReadTe
264269 expected_elements = not_corrupted ,
265270 expected_exceptions = ArrowInvalid ,
266271 warnings_patterns = [rf"points/{ corrupted } : ArrowInvalid" ],
272+ zarr_version = 2 ,
267273 )
268274
269275
@@ -303,6 +309,7 @@ def sdata_with_missing_zattrs_element(session_tmp_path: Path) -> PartialReadTest
303309 expected_elements = not_corrupted ,
304310 expected_exceptions = OSError ,
305311 warnings_patterns = ["OSError: Image location" ],
312+ zarr_version = 2 ,
306313 )
307314
308315
@@ -349,6 +356,7 @@ def sdata_with_missing_image_chunks_zarrv2(
349356 expected_elements = not_corrupted ,
350357 expected_exceptions = (ArrayNotFoundError ,),
351358 warnings_patterns = [rf"images/{ corrupted } : (ArrayNotFoundError|TypeError)" ],
359+ zarr_version = 2 ,
352360 )
353361
354362
@@ -372,6 +380,7 @@ def sdata_with_invalid_zattrs_element_violating_spec(session_tmp_path: Path) ->
372380 expected_elements = not_corrupted ,
373381 expected_exceptions = KeyError ,
374382 warnings_patterns = [rf"images/{ corrupted } : KeyError: coordinateTransformations" ],
383+ zarr_version = 2 ,
375384 )
376385
377386
@@ -424,6 +433,7 @@ def _create_sdata_with_table_region_not_found(session_tmp_path: Path, zarr_versi
424433 warnings_patterns = [
425434 rf"The table is annotating '{ re .escape (corrupted )} ', which is not present in the SpatialData object"
426435 ],
436+ zarr_version = zarr_version ,
427437 )
428438
429439
@@ -460,11 +470,15 @@ def sdata_with_table_region_not_found_zarrv2(session_tmp_path: Path) -> PartialR
460470 indirect = True ,
461471)
462472def test_read_zarr_with_error (test_case : PartialReadTestCase ):
463- if test_case .expected_exceptions :
464- with pytest .raises (test_case .expected_exceptions ):
473+ ctx = warnings .catch_warnings () if test_case .zarr_version < 3 else nullcontext ()
474+ with ctx :
475+ if test_case .zarr_version < 3 :
476+ warnings .filterwarnings ("ignore" , message = "SpatialData is not stored in the most current format" , category = UserWarning )
477+ if test_case .expected_exceptions :
478+ with pytest .raises (test_case .expected_exceptions ):
479+ read_zarr (test_case .path , on_bad_files = "error" )
480+ else :
465481 read_zarr (test_case .path , on_bad_files = "error" )
466- else :
467- read_zarr (test_case .path , on_bad_files = "error" )
468482
469483
470484@pytest .mark .parametrize (
@@ -490,8 +504,12 @@ def test_read_zarr_with_error(test_case: PartialReadTestCase):
490504 indirect = True ,
491505)
492506def test_read_zarr_with_warnings (test_case : PartialReadTestCase ):
493- with pytest_warns_multiple (UserWarning , matches = test_case .warnings_patterns ):
494- actual : SpatialData = read_zarr (test_case .path , on_bad_files = "warn" )
507+ ctx = warnings .catch_warnings () if test_case .zarr_version < 3 else nullcontext ()
508+ with ctx :
509+ if test_case .zarr_version < 3 :
510+ warnings .filterwarnings ("ignore" , message = "SpatialData is not stored in the most current format" , category = UserWarning )
511+ with pytest_warns_multiple (UserWarning , matches = test_case .warnings_patterns ):
512+ actual : SpatialData = read_zarr (test_case .path , on_bad_files = "warn" )
495513
496514 actual_elements = {name for _ , name , _ in actual .gen_elements ()}
497515 assert set (test_case .expected_elements ) == actual_elements
0 commit comments