Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3] implement / deprecate zarr.tree #2537

Merged
merged 4 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np
import numpy.typing as npt
from typing_extensions import deprecated

from zarr.core.array import Array, AsyncArray, get_array_metadata
from zarr.core.buffer import NDArrayLike
Expand Down Expand Up @@ -493,8 +494,29 @@ async def save_group(
await asyncio.gather(*aws)


async def tree(*args: Any, **kwargs: Any) -> None:
raise NotImplementedError
@deprecated("Use AsyncGroup.tree instead.")
async def tree(grp: AsyncGroup, expand: bool | None = None, level: int | None = None) -> Any:
"""Provide a rich display of the hierarchy.

Parameters
----------
grp : Group
Zarr or h5py group.
expand : bool, optional
Only relevant for HTML representation. If True, tree will be fully expanded.
level : int, optional
Maximum depth to descend into hierarchy.

Returns
-------
TreeRepr
A pretty-printable object displaying the hierarchy.

.. deprecated:: 3.0.0
`zarr.tree()` is deprecated and will be removed in a future release.
Use `group.tree()` instead.
"""
return await grp.tree(expand=expand, level=level)


async def array(
Expand Down
7 changes: 5 additions & 2 deletions src/zarr/api/synchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from typing import TYPE_CHECKING, Any, Literal

from typing_extensions import deprecated

import zarr.api.asynchronous as async_api
from zarr._compat import _deprecate_positional_args
from zarr.core.array import Array, AsyncArray
Expand Down Expand Up @@ -155,8 +157,9 @@ def save_group(
)


def tree(*args: Any, **kwargs: Any) -> None:
return sync(async_api.tree(*args, **kwargs))
@deprecated("Use Group.tree instead.")
def tree(grp: Group, expand: bool | None = None, level: int | None = None) -> Any:
return sync(async_api.tree(grp._async_group, expand=expand, level=level))


# TODO: add type annotations for kwargs
Expand Down
2 changes: 1 addition & 1 deletion src/zarr/core/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,7 @@ async def tree(self, expand: bool | None = None, level: int | None = None) -> An
from zarr.core._tree import group_tree_async

if expand is not None:
raise NotImplementedError("'expanded' is not yet implemented.")
raise NotImplementedError("'expand' is not yet implemented.")
return await group_tree_async(self, max_depth=level)

async def empty(
Expand Down
7 changes: 4 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,16 @@ def test_load_array(memory_store: Store) -> None:


def test_tree() -> None:
pytest.importorskip("rich")
g1 = zarr.group()
g1.create_group("foo")
g3 = g1.create_group("bar")
g3.create_group("baz")
g5 = g3.create_group("qux")
g5.create_array("baz", shape=100, chunks=10)
# TODO: complete after tree has been reimplemented
# assert repr(zarr.tree(g1)) == repr(g1.tree())
# assert str(zarr.tree(g1)) == str(g1.tree())
with pytest.warns(DeprecationWarning):
assert repr(zarr.tree(g1)) == repr(g1.tree())
assert str(zarr.tree(g1)) == str(g1.tree())


# @pytest.mark.parametrize("stores_from_path", [False, True])
Expand Down
Loading