Skip to content

Commit c5c5fa5

Browse files
authored
Merge pull request #1063 from danielballan/zarr-detection
refactor: Improve zarr version detection.
2 parents 24f378e + 5cdf2e0 commit c5c5fa5

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Write the date in place of the "Unreleased" in the case a new version is release
1414

1515
- Refactored internal server function ``get_root_tree()`` to not use FastAPI
1616
dependencies injection
17+
- Refactored internal Zarr version detection
1718

1819
### Fixed
1920

tiled/_tests/test_container_fields.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import sys
2-
31
import anyio
42
import h5py
53
import numpy as np
64
import pandas
75
import pytest
86
import zarr
97

8+
from ..adapters.zarr import ZARR_LIB_V2
109
from ..catalog import in_memory
1110
from ..client import Context, from_context, record_history
1211
from ..client.register import register
@@ -84,7 +83,7 @@ def zarr_data_dir(tmpdir_factory):
8483
try:
8584
root = zarr.open(str(tmpdir / "zarr_group.zarr"), mode="w")
8685
for i, name in enumerate("abcde"):
87-
if sys.version_info < (3, 11):
86+
if ZARR_LIB_V2:
8887
root.create_dataset(name, data=np.arange(i, i + 3))
8988
else:
9089
root.create_array(name, data=np.arange(i, i + 3))

tiled/_tests/test_container_files.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
from pathlib import Path
32

43
import h5py
@@ -7,6 +6,7 @@
76
import pytest
87
import zarr
98

9+
from ..adapters.zarr import ZARR_LIB_V2
1010
from ..catalog import in_memory
1111
from ..client import Context, from_context, tree
1212
from ..client.register import register
@@ -46,7 +46,7 @@ async def test_zarr_group(tmp_path: Path):
4646
x_array = np.array([1, 2, 3])
4747
y_array = np.array([4, 5, 6])
4848

49-
if sys.version_info < (3, 11):
49+
if ZARR_LIB_V2:
5050
root.create_dataset("x", data=x_array)
5151
root.create_dataset("y", data=y_array)
5252
else:

tiled/adapters/zarr.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import builtins
22
import copy
33
import os
4-
import sys
54
from collections.abc import Mapping
5+
from importlib.metadata import version
66
from typing import Any, Iterator, List, Optional, Tuple, Union, cast
77
from urllib.parse import quote_plus
88

99
import zarr.core
10-
11-
if sys.version_info < (3, 11):
12-
from zarr.storage import DirectoryStore as LocalStore
13-
from zarr.storage import init_array as create_array
14-
else:
15-
from zarr.storage import LocalStore
16-
from zarr import create_array
17-
1810
from numpy._typing import NDArray
11+
from packaging.version import Version
1912

2013
from ..adapters.utils import IndexersMixin
2114
from ..catalog.orm import Node
@@ -29,6 +22,14 @@
2922
from ..utils import Conflicts, node_repr, path_from_uri
3023
from .array import ArrayAdapter, slice_and_shape_from_block_and_chunks
3124

25+
ZARR_LIB_V2 = Version(version("zarr")) < Version("3")
26+
if ZARR_LIB_V2:
27+
from zarr.storage import DirectoryStore as LocalStore
28+
from zarr.storage import init_array as create_array
29+
else:
30+
from zarr import create_array
31+
from zarr.storage import LocalStore
32+
3233
INLINED_DEPTH = int(os.getenv("TILED_HDF5_INLINED_CONTENTS_MAX_DEPTH", "7"))
3334

3435

0 commit comments

Comments
 (0)