Skip to content

Commit ca35193

Browse files
authored
Merge branch 'main' into perf/remove-isinstance-check
2 parents f0d4b2e + e03cfc8 commit ca35193

6 files changed

Lines changed: 55 additions & 25 deletions

File tree

changes/3657.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix obstore _transform_list_dir implementation to correctly relativize paths (removing lstrip usage).

changes/3706.misc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow NumPy ints as input when declaring a shape.

src/zarr/core/common.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
overload,
2222
)
2323

24+
import numpy as np
2425
from typing_extensions import ReadOnly
2526

2627
from zarr.core.config import config as zarr_config
@@ -37,7 +38,7 @@
3738
ZMETADATA_V2_JSON = ".zmetadata"
3839

3940
BytesLike = bytes | bytearray | memoryview
40-
ShapeLike = Iterable[int] | int
41+
ShapeLike = Iterable[int | np.integer[Any]] | int | np.integer[Any]
4142
# For backwards compatibility
4243
ChunkCoords = tuple[int, ...]
4344
ZarrFormat = Literal[2, 3]
@@ -185,23 +186,28 @@ def parse_named_configuration(
185186

186187

187188
def parse_shapelike(data: ShapeLike) -> tuple[int, ...]:
188-
if isinstance(data, int):
189+
"""
190+
Parse a shape-like input into an explicit shape.
191+
"""
192+
if isinstance(data, int | np.integer):
189193
if data < 0:
190194
raise ValueError(f"Expected a non-negative integer. Got {data} instead")
191-
return (data,)
195+
return (int(data),)
192196
try:
193197
data_tuple = tuple(data)
194198
except TypeError as e:
195199
msg = f"Expected an integer or an iterable of integers. Got {data} instead."
196200
raise TypeError(msg) from e
197201

198-
if not all(isinstance(v, int) for v in data_tuple):
202+
if not all(isinstance(v, int | np.integer) for v in data_tuple):
199203
msg = f"Expected an iterable of integers. Got {data} instead."
200204
raise TypeError(msg)
201205
if not all(v > -1 for v in data_tuple):
202206
msg = f"Expected all values to be non-negative. Got {data} instead."
203207
raise ValueError(msg)
204-
return data_tuple
208+
209+
# cast NumPy scalars to plain python ints
210+
return tuple(int(x) for x in data_tuple)
205211

206212

207213
def parse_fill_value(data: Any) -> Any:

src/zarr/storage/_obstore.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import contextlib
55
import pickle
66
from collections import defaultdict
7+
from itertools import chain
8+
from operator import itemgetter
79
from typing import TYPE_CHECKING, Generic, Self, TypedDict, TypeVar
810

911
from zarr.abc.store import (
@@ -15,6 +17,7 @@
1517
)
1618
from zarr.core.common import concurrent_map
1719
from zarr.core.config import config
20+
from zarr.storage._utils import _relativize_path
1821

1922
if TYPE_CHECKING:
2023
from collections.abc import AsyncGenerator, Coroutine, Iterable, Sequence
@@ -263,10 +266,11 @@ async def _transform_list_dir(
263266
# We assume that the underlying object-store implementation correctly handles the
264267
# prefix, so we don't double-check that the returned results actually start with the
265268
# given prefix.
266-
prefixes = [obj.lstrip(prefix).lstrip("/") for obj in list_result["common_prefixes"]]
267-
objects = [obj["path"].removeprefix(prefix).lstrip("/") for obj in list_result["objects"]]
268-
for item in prefixes + objects:
269-
yield item
269+
prefix = prefix.rstrip("/")
270+
for path in chain(
271+
list_result["common_prefixes"], map(itemgetter("path"), list_result["objects"])
272+
):
273+
yield _relativize_path(path=path, prefix=prefix)
270274

271275

272276
class _BoundedRequest(TypedDict):

src/zarr/testing/store.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -492,24 +492,36 @@ async def test_list_empty_path(self, store: S) -> None:
492492
assert observed_prefix_sorted == expected_prefix_sorted
493493

494494
async def test_list_dir(self, store: S) -> None:
495-
root = "foo"
496-
store_dict = {
497-
root + "/zarr.json": self.buffer_cls.from_bytes(b"bar"),
498-
root + "/c/1": self.buffer_cls.from_bytes(b"\x01"),
499-
}
495+
roots_and_keys: list[tuple[str, dict[str, Buffer]]] = [
496+
(
497+
"foo",
498+
{
499+
"foo/zarr.json": self.buffer_cls.from_bytes(b"bar"),
500+
"foo/c/1": self.buffer_cls.from_bytes(b"\x01"),
501+
},
502+
),
503+
(
504+
"foo/bar",
505+
{
506+
"foo/bar/foobar_first_child": self.buffer_cls.from_bytes(b"1"),
507+
"foo/bar/foobar_second_child/zarr.json": self.buffer_cls.from_bytes(b"2"),
508+
},
509+
),
510+
]
500511

501512
assert await _collect_aiterator(store.list_dir("")) == ()
502-
assert await _collect_aiterator(store.list_dir(root)) == ()
503513

504-
await store._set_many(store_dict.items())
514+
for root, store_dict in roots_and_keys:
515+
assert await _collect_aiterator(store.list_dir(root)) == ()
505516

506-
keys_observed = await _collect_aiterator(store.list_dir(root))
507-
keys_expected = {k.removeprefix(root + "/").split("/")[0] for k in store_dict}
517+
await store._set_many(store_dict.items())
508518

509-
assert sorted(keys_observed) == sorted(keys_expected)
519+
keys_observed = await _collect_aiterator(store.list_dir(root))
520+
keys_expected = {k.removeprefix(root + "/").split("/")[0] for k in store_dict}
521+
assert sorted(keys_observed) == sorted(keys_expected)
510522

511-
keys_observed = await _collect_aiterator(store.list_dir(root + "/"))
512-
assert sorted(keys_expected) == sorted(keys_observed)
523+
keys_observed = await _collect_aiterator(store.list_dir(root + "/"))
524+
assert sorted(keys_expected) == sorted(keys_observed)
513525

514526
async def test_set_if_not_exists(self, store: S) -> None:
515527
key = "k"

tests/test_common.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from collections.abc import Iterable
34
from typing import TYPE_CHECKING, get_args
45

56
import numpy as np
@@ -15,7 +16,6 @@
1516
from zarr.core.config import parse_indexing_order
1617

1718
if TYPE_CHECKING:
18-
from collections.abc import Iterable
1919
from typing import Any, Literal
2020

2121

@@ -115,9 +115,15 @@ def test_parse_shapelike_invalid_iterable_values(data: Any) -> None:
115115
parse_shapelike(data)
116116

117117

118-
@pytest.mark.parametrize("data", [range(10), [0, 1, 2, 3], (3, 4, 5), ()])
119-
def test_parse_shapelike_valid(data: Iterable[int]) -> None:
120-
assert parse_shapelike(data) == tuple(data)
118+
@pytest.mark.parametrize(
119+
"data", [range(10), [0, 1, 2, np.uint64(3)], (3, 4, 5), (), 1, np.uint8(1)]
120+
)
121+
def test_parse_shapelike_valid(data: Iterable[int] | int) -> None:
122+
if isinstance(data, Iterable):
123+
expected = tuple(data)
124+
else:
125+
expected = (data,)
126+
assert parse_shapelike(data) == expected
121127

122128

123129
# todo: more dtypes

0 commit comments

Comments
 (0)