Skip to content

Commit d8f7d68

Browse files
committed
add test
1 parent d42e0e6 commit d8f7d68

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/zarr/core/group.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import warnings
88
from collections import defaultdict
99
from dataclasses import asdict, dataclass, field, fields, replace
10-
from typing import TYPE_CHECKING, Literal, TypeVar, assert_never, cast, overload, Self
10+
from typing import TYPE_CHECKING, Literal, Self, TypeVar, assert_never, cast, overload
1111

1212
import numpy as np
1313
import numpy.typing as npt
@@ -55,6 +55,7 @@
5555

5656
if TYPE_CHECKING:
5757
from collections.abc import AsyncGenerator, Generator, Iterable, Iterator
58+
from types import TracebackType
5859
from typing import Any
5960

6061
from zarr.core.array_spec import ArrayConfig, ArrayConfigLike
@@ -1755,7 +1756,9 @@ def open(
17551756
def __enter__(self) -> Self:
17561757
return self
17571758

1758-
def __exit__(self) -> None: # noqa: PYI036
1759+
def __exit__(
1760+
self, typ: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None
1761+
) -> None:
17591762
self.store.close()
17601763

17611764
def __getitem__(self, path: str) -> Array | Group:

tests/test_group.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,28 @@ def test_group_len(store: Store, zarr_format: ZarrFormat) -> None:
428428
assert len(group) == 0
429429

430430

431+
def test_group_with_context_manager(store: Store, zarr_format: ZarrFormat, overwrite: bool) -> None:
432+
spath = StorePath(store)
433+
434+
# attempt to open a group that does not exist.
435+
with pytest.raises(FileNotFoundError):
436+
with Group.open(store) as store:
437+
pass
438+
439+
attrs = {"path": "foo"}
440+
441+
with Group.from_store(
442+
store, attributes=attrs, zarr_format=zarr_format, overwrite=overwrite
443+
) as group:
444+
assert store._is_open
445+
assert group.attrs == attrs
446+
assert group.metadata.zarr_format == zarr_format
447+
assert group.store_path == spath
448+
449+
# Check if store was closed after exit.
450+
assert not store._is_open
451+
452+
431453
def test_group_setitem(store: Store, zarr_format: ZarrFormat) -> None:
432454
"""
433455
Test the `Group.__setitem__` method.

0 commit comments

Comments
 (0)