Skip to content

Commit 0d9aa8c

Browse files
LucaMarconatoclaude
andcommitted
fix: suppress 'not stored in most current format' UserWarning in old-format tests
Use `re.match`-compatible pattern (anchored at start) and add `@pytest.mark.filterwarnings` to standalone parametrized tests outside `TestReadWrite` that intentionally exercise the V01 container format. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1fe839b commit 0d9aa8c

4 files changed

Lines changed: 73 additions & 9 deletions

File tree

tests/io/test_attrs_io.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import tempfile
6+
import warnings
67
from pathlib import Path
78

89
import pytest
@@ -11,6 +12,7 @@
1112
from spatialdata._io.format import (
1213
SpatialDataContainerFormats,
1314
SpatialDataContainerFormatType,
15+
CurrentSpatialDataContainerFormat,
1416
)
1517

1618
FORMAT_V01 = SpatialDataContainerFormats["0.1"]
@@ -21,6 +23,17 @@
2123
class TestAttrsIO:
2224
"""Test SpatialData.attrs read/write for all container formats."""
2325

26+
@pytest.fixture(autouse=True)
27+
def _suppress_old_format_warning(self, sdata_container_format: SpatialDataContainerFormatType):
28+
if isinstance(sdata_container_format, CurrentSpatialDataContainerFormat):
29+
yield
30+
else:
31+
with warnings.catch_warnings():
32+
warnings.filterwarnings(
33+
"ignore", message="SpatialData is not stored in the most current format", category=UserWarning
34+
)
35+
yield
36+
2437
def test_attrs_write_and_read(
2538
self,
2639
sdata_container_format: SpatialDataContainerFormatType,
@@ -111,7 +124,11 @@ def test_attrs_v1_to_v2() -> None:
111124
sdata.write(f_v1, sdata_formats=FORMAT_V01)
112125

113126
# Read with V01
114-
sdata_v1 = read_zarr(f_v1)
127+
with warnings.catch_warnings():
128+
warnings.filterwarnings(
129+
"ignore", message="SpatialData is not stored in the most current format", category=UserWarning
130+
)
131+
sdata_v1 = read_zarr(f_v1)
115132
assert sdata_v1.attrs == my_attrs
116133

117134
# Write with V02

tests/io/test_format.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import tempfile
5+
import warnings
56
from pathlib import Path
67
from typing import Any
78

@@ -125,6 +126,14 @@ def test_format_raster_v1_v2_v3(self, images, rformat: type[SpatialDataFormatTyp
125126
class TestFormatConversions:
126127
"""Test format conversions between older formats and newer."""
127128

129+
@pytest.fixture(autouse=True)
130+
def _suppress_old_format_warning(self):
131+
with warnings.catch_warnings():
132+
warnings.filterwarnings(
133+
"ignore", message="SpatialData is not stored in the most current format", category=UserWarning
134+
)
135+
yield
136+
128137
def test_shapes_v1_to_v2_to_v3(self, shapes):
129138
with tempfile.TemporaryDirectory() as tmpdir:
130139
f1 = Path(tmpdir) / "data1.zarr"

tests/io/test_partial_read.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import os
55
import re
66
import tempfile
7+
import warnings
78
from 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
1011
from json import JSONDecodeError
1112
from pathlib import Path
1213
from 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
)
462472
def 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
)
492506
def 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

tests/io/test_readwrite.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import os
55
import tempfile
6+
import warnings
67
from collections.abc import Callable
78
from pathlib import Path
89
from typing import Any, Literal
@@ -57,6 +58,17 @@
5758

5859
@pytest.mark.parametrize("sdata_container_format", SDATA_FORMATS)
5960
class TestReadWrite:
61+
@pytest.fixture(autouse=True)
62+
def _suppress_old_format_warning(self, sdata_container_format: SpatialDataContainerFormatType):
63+
if isinstance(sdata_container_format, CurrentSpatialDataContainerFormat):
64+
yield
65+
else:
66+
with warnings.catch_warnings():
67+
warnings.filterwarnings(
68+
"ignore", message="SpatialData is not stored in the most current format", category=UserWarning
69+
)
70+
yield
71+
6072
def test_images(
6173
self,
6274
tmp_path: str,
@@ -743,6 +755,7 @@ def test_single_scale_image_roundtrip_stays_dataarray(tmp_path: Path) -> None:
743755
assert list(image_group.keys()) == ["s0"]
744756

745757

758+
@pytest.mark.filterwarnings("ignore:SpatialData is not stored in the most current format:UserWarning")
746759
@pytest.mark.parametrize("sdata_container_format", SDATA_FORMATS)
747760
def test_self_contained(full_sdata: SpatialData, sdata_container_format: SpatialDataContainerFormatType) -> None:
748761
# data only in-memory, so the SpatialData object and all its elements are self-contained
@@ -804,6 +817,7 @@ def test_self_contained(full_sdata: SpatialData, sdata_container_format: Spatial
804817
assert all(description[element_name] for element_name in description if element_name != "combined")
805818

806819

820+
@pytest.mark.filterwarnings("ignore:SpatialData is not stored in the most current format:UserWarning")
807821
@pytest.mark.parametrize("sdata_container_format", SDATA_FORMATS)
808822
def test_symmetric_difference_with_zarr_store(
809823
full_sdata: SpatialData, sdata_container_format: SpatialDataContainerFormatType
@@ -846,6 +860,7 @@ def test_symmetric_difference_with_zarr_store(
846860
}
847861

848862

863+
@pytest.mark.filterwarnings("ignore:SpatialData is not stored in the most current format:UserWarning")
849864
@pytest.mark.parametrize("sdata_container_format", SDATA_FORMATS)
850865
def test_change_path_of_subset(full_sdata: SpatialData, sdata_container_format: SpatialDataContainerFormatType) -> None:
851866
"""A subset SpatialData object has not Zarr path associated, show that we can reassign the path"""
@@ -917,6 +932,7 @@ def test_incremental_io_valid_name(full_sdata: SpatialData) -> None:
917932
_check_valid_name(full_sdata.write_transformations)
918933

919934

935+
@pytest.mark.filterwarnings("ignore:SpatialData is not stored in the most current format:UserWarning")
920936
@pytest.mark.parametrize("sdata_container_format", SDATA_FORMATS)
921937
def test_incremental_io_attrs(points: SpatialData, sdata_container_format: SpatialDataContainerFormatType) -> None:
922938
with tempfile.TemporaryDirectory() as tmpdir:
@@ -945,6 +961,7 @@ def test_incremental_io_attrs(points: SpatialData, sdata_container_format: Spati
945961
cached_sdata_blobs = blobs()
946962

947963

964+
@pytest.mark.filterwarnings("ignore:SpatialData is not stored in the most current format:UserWarning")
948965
@pytest.mark.parametrize("element_name", ["image2d", "labels2d", "points_0", "circles", "table"])
949966
@pytest.mark.parametrize("sdata_container_format", SDATA_FORMATS)
950967
def test_delete_element_from_disk(
@@ -996,6 +1013,7 @@ def test_delete_element_from_disk(
9961013
assert element_path not in on_disk
9971014

9981015

1016+
@pytest.mark.filterwarnings("ignore:SpatialData is not stored in the most current format:UserWarning")
9991017
@pytest.mark.parametrize("element_name", ["image2d", "labels2d", "points_0", "circles", "table"])
10001018
@pytest.mark.parametrize("sdata_container_format", SDATA_FORMATS)
10011019
def test_element_already_on_disk_different_type(
@@ -1127,6 +1145,7 @@ def test_reading_invalid_name(tmp_path: Path):
11271145
)
11281146

11291147

1148+
@pytest.mark.filterwarnings("ignore:SpatialData is not stored in the most current format:UserWarning")
11301149
@pytest.mark.parametrize("sdata_container_format", SDATA_FORMATS)
11311150
def test_write_store_unconsolidated_and_read(full_sdata, sdata_container_format: SpatialDataContainerFormatType):
11321151
with tempfile.TemporaryDirectory() as tmpdir:
@@ -1139,6 +1158,7 @@ def test_write_store_unconsolidated_and_read(full_sdata, sdata_container_format:
11391158
assert_spatial_data_objects_are_identical(full_sdata, second_read)
11401159

11411160

1161+
@pytest.mark.filterwarnings("ignore:SpatialData is not stored in the most current format:UserWarning")
11421162
@pytest.mark.parametrize("sdata_container_format", SDATA_FORMATS)
11431163
def test_can_read_sdata_with_reconsolidation(full_sdata, sdata_container_format: SpatialDataContainerFormatType):
11441164
with tempfile.TemporaryDirectory() as tmpdir:

0 commit comments

Comments
 (0)