Skip to content

Commit 638677a

Browse files
Merge pull request #327 from scverse/zarrv3-context-manager
fix zarr.open (v3) lacks context manager
2 parents 02a487d + e4a5b09 commit 638677a

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/spatialdata_io/_utils.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@
22

33
import functools
44
import warnings
5+
from contextlib import contextmanager
56
from typing import TYPE_CHECKING, Any, TypeVar
67

8+
import zarr
9+
710
if TYPE_CHECKING:
8-
from collections.abc import Callable
11+
from collections.abc import Callable, Generator
12+
13+
from zarr import Array, Group
14+
from zarr.core.common import (
15+
AccessModeLiteral,
16+
)
17+
from zarr.storage import StoreLike
918

1019
RT = TypeVar("RT")
1120

1221

1322
# these two functions should be removed and imported from spatialdata._utils once the multi_table branch, which
1423
# introduces them, is merged
15-
def deprecation_alias(**aliases: str) -> Callable[[Callable[..., RT]], Callable[..., RT]]:
24+
def deprecation_alias(
25+
**aliases: str,
26+
) -> Callable[[Callable[..., RT]], Callable[..., RT]]:
1627
"""Decorate a function to warn user of use of arguments set for deprecation.
1728
1829
Parameters
@@ -52,7 +63,12 @@ def wrapper(*args: Any, **kwargs: Any) -> RT:
5263
return deprecation_decorator
5364

5465

55-
def rename_kwargs(func_name: str, kwargs: dict[str, Any], aliases: dict[str, str], class_name: None | str) -> None:
66+
def rename_kwargs(
67+
func_name: str,
68+
kwargs: dict[str, Any],
69+
aliases: dict[str, str],
70+
class_name: None | str,
71+
) -> None:
5672
"""Rename function arguments set for deprecation and gives warning in case of usage of these arguments."""
5773
for alias, new in aliases.items():
5874
if alias in kwargs:
@@ -71,3 +87,17 @@ def rename_kwargs(func_name: str, kwargs: dict[str, Any], aliases: dict[str, str
7187
stacklevel=3,
7288
)
7389
kwargs[new] = kwargs.pop(alias)
90+
91+
92+
# workaround until https://github.com/zarr-developers/zarr-python/issues/2619 is closed
93+
@contextmanager
94+
def zarr_open(
95+
store: StoreLike | None = None,
96+
*,
97+
mode: AccessModeLiteral | None = None,
98+
) -> Generator[Array | Group, Any, None]:
99+
f = zarr.open(store=store, mode=mode)
100+
try:
101+
yield f
102+
finally:
103+
f.store.close()

src/spatialdata_io/readers/xenium.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
from spatialdata_io._constants._constants import XeniumKeys
4040
from spatialdata_io._docs import inject_docs
41-
from spatialdata_io._utils import deprecation_alias
41+
from spatialdata_io._utils import deprecation_alias, zarr_open
4242
from spatialdata_io.readers._utils._read_10x_h5 import _read_10x_h5
4343
from spatialdata_io.readers._utils._utils import _initialize_raster_models_kwargs
4444

@@ -417,7 +417,7 @@ def _get_labels_and_indices_mapping(
417417
with zipfile.ZipFile(zip_file, "r") as zip_ref:
418418
zip_ref.extractall(tmpdir)
419419

420-
with zarr.open(str(tmpdir), mode="r") as z:
420+
with zarr_open(str(tmpdir), mode="r") as z:
421421
# get the labels
422422
masks = z["masks"][f"{mask_index}"][...]
423423
labels = Labels2DModel.parse(
@@ -492,7 +492,7 @@ def _get_cells_metadata_table_from_zarr(
492492
with zipfile.ZipFile(zip_file, "r") as zip_ref:
493493
zip_ref.extractall(tmpdir)
494494

495-
with zarr.open(str(tmpdir), mode="r") as z:
495+
with zarr_open(str(tmpdir), mode="r") as z:
496496
x = z["cell_summary"][...]
497497
column_names = z["cell_summary"].attrs["column_names"]
498498
df = pd.DataFrame(x, columns=column_names)

0 commit comments

Comments
 (0)