Skip to content

Commit a9d2c9e

Browse files
committed
Clean up zarr.core.buffer API
1 parent 584d66d commit a9d2c9e

File tree

11 files changed

+28
-30
lines changed

11 files changed

+28
-30
lines changed

src/zarr/abc/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from itertools import starmap
66
from typing import TYPE_CHECKING, Protocol, runtime_checkable
77

8-
from zarr.core.buffer.core import default_buffer_prototype
8+
from zarr.core.buffer._core import default_buffer_prototype
99
from zarr.core.common import concurrent_map
1010
from zarr.core.config import config
1111

src/zarr/core/buffer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from zarr.core.buffer.core import (
1+
from zarr.core.buffer._core import (
22
ArrayLike,
33
Buffer,
44
BufferPrototype,

src/zarr/core/buffer/core.py renamed to src/zarr/core/buffer/_core.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
from zarr.codecs.bytes import Endian
2424
from zarr.core.common import BytesLike, ChunkCoords
2525

26-
# Everything here is imported into ``zarr.core.buffer`` namespace.
27-
__all__: list[str] = []
28-
2926

3027
@runtime_checkable
3128
class ArrayLike(Protocol):

src/zarr/core/buffer/cpu.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import numpy as np
1010
import numpy.typing as npt
1111

12-
from zarr.core.buffer import core
12+
import zarr.core.buffer
1313
from zarr.registry import (
1414
register_buffer,
1515
register_ndbuffer,
@@ -19,11 +19,11 @@
1919
from collections.abc import Callable, Iterable
2020
from typing import Self
2121

22-
from zarr.core.buffer.core import ArrayLike, NDArrayLike
22+
from zarr.core.buffer import ArrayLike, NDArrayLike
2323
from zarr.core.common import BytesLike
2424

2525

26-
class Buffer(core.Buffer):
26+
class Buffer(zarr.core.buffer.Buffer):
2727
"""A flat contiguous memory block
2828
2929
We use Buffer throughout Zarr to represent a contiguous block of memory.
@@ -52,7 +52,7 @@ def create_zero_length(cls) -> Self:
5252
return cls(np.array([], dtype="b"))
5353

5454
@classmethod
55-
def from_buffer(cls, buffer: core.Buffer) -> Self:
55+
def from_buffer(cls, buffer: zarr.core.buffer.Buffer) -> Self:
5656
"""Create a new buffer of an existing Buffer
5757
5858
This is useful if you want to ensure that an existing buffer is
@@ -107,7 +107,7 @@ def as_numpy_array(self) -> npt.NDArray[Any]:
107107
"""
108108
return np.asanyarray(self._data)
109109

110-
def __add__(self, other: core.Buffer) -> Self:
110+
def __add__(self, other: zarr.core.buffer.Buffer) -> Self:
111111
"""Concatenate two buffers"""
112112

113113
other_array = other.as_array_like()
@@ -117,7 +117,7 @@ def __add__(self, other: core.Buffer) -> Self:
117117
)
118118

119119

120-
class NDBuffer(core.NDBuffer):
120+
class NDBuffer(zarr.core.buffer.NDBuffer):
121121
"""An n-dimensional memory block
122122
123123
We use NDBuffer throughout Zarr to represent a n-dimensional memory block.
@@ -186,8 +186,10 @@ def __setitem__(self, key: Any, value: Any) -> None:
186186

187187

188188
def as_numpy_array_wrapper(
189-
func: Callable[[npt.NDArray[Any]], bytes], buf: core.Buffer, prototype: core.BufferPrototype
190-
) -> core.Buffer:
189+
func: Callable[[npt.NDArray[Any]], bytes],
190+
buf: zarr.core.buffer.Buffer,
191+
prototype: zarr.core.buffer.BufferPrototype,
192+
) -> zarr.core.buffer.Buffer:
191193
"""Converts the input of `func` to a numpy array and the output back to `Buffer`.
192194
193195
This function is useful when calling a `func` that only support host memory such
@@ -214,13 +216,13 @@ def as_numpy_array_wrapper(
214216

215217

216218
# CPU buffer prototype using numpy arrays
217-
buffer_prototype = core.BufferPrototype(buffer=Buffer, nd_buffer=NDBuffer)
219+
buffer_prototype = zarr.core.buffer.BufferPrototype(buffer=Buffer, nd_buffer=NDBuffer)
218220
# default_buffer_prototype = buffer_prototype
219221

220222

221223
# The numpy prototype used for E.g. when reading the shard index
222-
def numpy_buffer_prototype() -> core.BufferPrototype:
223-
return core.BufferPrototype(buffer=Buffer, nd_buffer=NDBuffer)
224+
def numpy_buffer_prototype() -> zarr.core.buffer.BufferPrototype:
225+
return zarr.core.buffer.BufferPrototype(buffer=Buffer, nd_buffer=NDBuffer)
224226

225227

226228
register_buffer(Buffer)

src/zarr/core/buffer/gpu.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import numpy as np
1212
import numpy.typing as npt
1313

14-
from zarr.core.buffer import core
15-
from zarr.core.buffer.core import ArrayLike, BufferPrototype, NDArrayLike
14+
import zarr.core.buffer
15+
from zarr.core.buffer import ArrayLike, BufferPrototype, NDArrayLike
1616

1717
if TYPE_CHECKING:
1818
from collections.abc import Iterable
@@ -26,7 +26,7 @@
2626
cp = None
2727

2828

29-
class Buffer(core.Buffer):
29+
class Buffer(zarr.core.buffer.Buffer):
3030
"""A flat contiguous memory block on the GPU
3131
3232
We use Buffer throughout Zarr to represent a contiguous block of memory.
@@ -83,7 +83,7 @@ def create_zero_length(cls) -> Self:
8383
return cls(cp.array([], dtype="b"))
8484

8585
@classmethod
86-
def from_buffer(cls, buffer: core.Buffer) -> Self:
86+
def from_buffer(cls, buffer: zarr.core.buffer.Buffer) -> Self:
8787
"""Create an GPU Buffer given an arbitrary Buffer
8888
This will try to be zero-copy if `buffer` is already on the
8989
GPU and will trigger a copy if not.
@@ -101,7 +101,7 @@ def from_bytes(cls, bytes_like: BytesLike) -> Self:
101101
def as_numpy_array(self) -> npt.NDArray[Any]:
102102
return cast(npt.NDArray[Any], cp.asnumpy(self._data))
103103

104-
def __add__(self, other: core.Buffer) -> Self:
104+
def __add__(self, other: zarr.core.buffer.Buffer) -> Self:
105105
other_array = other.as_array_like()
106106
assert other_array.dtype == np.dtype("b")
107107
gpu_other = Buffer(other_array)
@@ -111,7 +111,7 @@ def __add__(self, other: core.Buffer) -> Self:
111111
)
112112

113113

114-
class NDBuffer(core.NDBuffer):
114+
class NDBuffer(zarr.core.buffer.NDBuffer):
115115
"""A n-dimensional memory block on the GPU
116116
117117
We use NDBuffer throughout Zarr to represent a n-dimensional memory block.
@@ -208,7 +208,7 @@ def __getitem__(self, key: Any) -> Self:
208208
def __setitem__(self, key: Any, value: Any) -> None:
209209
if isinstance(value, NDBuffer):
210210
value = value._data
211-
elif isinstance(value, core.NDBuffer):
211+
elif isinstance(value, zarr.core.buffer.NDBuffer):
212212
gpu_value = NDBuffer(value.as_ndarray_like())
213213
value = gpu_value._data
214214
self._data.__setitem__(key, value)

src/zarr/core/metadata/v3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import TYPE_CHECKING, TypedDict, overload
55

66
from zarr.abc.metadata import Metadata
7-
from zarr.core.buffer.core import default_buffer_prototype
7+
from zarr.core.buffer._core import default_buffer_prototype
88

99
if TYPE_CHECKING:
1010
from typing import Self

src/zarr/storage/_local.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from zarr.abc.store import ByteRangeRequest, Store
1111
from zarr.core.buffer import Buffer
12-
from zarr.core.buffer.core import default_buffer_prototype
12+
from zarr.core.buffer._core import default_buffer_prototype
1313
from zarr.core.common import concurrent_map
1414

1515
if TYPE_CHECKING:

src/zarr/testing/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from typing import Any
1111

1212
from zarr.abc.store import ByteRangeRequest
13-
from zarr.core.buffer.core import BufferPrototype
13+
from zarr.core.buffer._core import BufferPrototype
1414

1515
import pytest
1616

tests/test_codecs/test_codecs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
if TYPE_CHECKING:
2525
from zarr.abc.store import Store
26-
from zarr.core.buffer.core import NDArrayLike
26+
from zarr.core.buffer import NDArrayLike
2727
from zarr.core.common import MemoryOrder
2828

2929

tests/test_indexing.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
if TYPE_CHECKING:
3232
from collections.abc import AsyncGenerator
3333

34-
from zarr.core.buffer import BufferPrototype
35-
from zarr.core.buffer.core import Buffer
34+
from zarr.core.buffer import Buffer, BufferPrototype
3635
from zarr.core.common import ChunkCoords
3736

3837

0 commit comments

Comments
 (0)