Skip to content

Commit d2890ef

Browse files
committed
Remove ndarray_like code
1 parent 6c0ea0f commit d2890ef

File tree

3 files changed

+8
-114
lines changed

3 files changed

+8
-114
lines changed

numcodecs/compat.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import codecs
33

44
import numpy as np
5+
import numpy.typing as npt
56

6-
from .ndarray_like import NDArrayLike, is_ndarray_like
7+
from .ndarray_like import is_ndarray_like
78

89

9-
def ensure_ndarray_like(buf) -> NDArrayLike:
10+
def ensure_ndarray_like(buf) -> npt.NDArray:
1011
"""Convenience function to coerce `buf` to ndarray-like array.
1112
1213
Parameters
@@ -38,7 +39,7 @@ def ensure_ndarray_like(buf) -> NDArrayLike:
3839
mem = memoryview(buf)
3940
# instantiate array from memoryview, ensures no copy
4041
buf = np.array(mem, copy=False)
41-
return buf
42+
return np.asanyarray(buf, copy=False)
4243

4344

4445
def ensure_ndarray(buf) -> np.ndarray:
@@ -63,7 +64,7 @@ def ensure_ndarray(buf) -> np.ndarray:
6364
return np.array(ensure_ndarray_like(buf), copy=False)
6465

6566

66-
def ensure_contiguous_ndarray_like(buf, max_buffer_size=None, flatten=True) -> NDArrayLike:
67+
def ensure_contiguous_ndarray_like(buf, max_buffer_size=None, flatten=True) -> npt.ArrayLike:
6768
"""Convenience function to coerce `buf` to ndarray-like array.
6869
Also ensures that the returned value exports fully contiguous memory,
6970
and supports the new-style buffer interface. If the optional max_buffer_size is
@@ -174,7 +175,7 @@ def ensure_text(s, encoding="utf-8"):
174175
return s
175176

176177

177-
def ndarray_copy(src, dst) -> NDArrayLike:
178+
def ndarray_copy(src, dst) -> npt.NDArray:
178179
"""Copy the contents of the array from `src` to `dst`."""
179180

180181
if dst is None:

numcodecs/ndarray_like.py

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,6 @@
1-
from typing import Any, ClassVar, Protocol, runtime_checkable
2-
3-
4-
class _CachedProtocolMeta(Protocol.__class__): # type: ignore[name-defined]
5-
"""Custom implementation of @runtime_checkable
6-
7-
The native implementation of @runtime_checkable is slow,
8-
see <https://github.com/zarr-developers/numcodecs/issues/379>.
9-
10-
This metaclass keeps an unbounded cache of the result of
11-
isinstance checks using the object's class as the cache key.
12-
"""
13-
14-
_instancecheck_cache: ClassVar[dict[tuple[type, type], bool]] = {}
15-
16-
def __instancecheck__(self, instance):
17-
key = (self, instance.__class__)
18-
ret = self._instancecheck_cache.get(key)
19-
if ret is None:
20-
ret = super().__instancecheck__(instance)
21-
self._instancecheck_cache[key] = ret
22-
return ret
23-
24-
25-
@runtime_checkable
26-
class DType(Protocol, metaclass=_CachedProtocolMeta):
27-
itemsize: int
28-
name: str
29-
kind: str
30-
31-
32-
@runtime_checkable
33-
class FlagsObj(Protocol, metaclass=_CachedProtocolMeta):
34-
c_contiguous: bool
35-
f_contiguous: bool
36-
owndata: bool
37-
38-
39-
@runtime_checkable
40-
class NDArrayLike(Protocol, metaclass=_CachedProtocolMeta):
41-
dtype: DType
42-
shape: tuple[int, ...]
43-
strides: tuple[int, ...]
44-
ndim: int
45-
size: int
46-
itemsize: int
47-
nbytes: int
48-
flags: FlagsObj
49-
50-
def __len__(self) -> int: ... # pragma: no cover
51-
52-
def __getitem__(self, key) -> Any: ... # pragma: no cover
53-
54-
def __setitem__(self, key, value): ... # pragma: no cover
55-
56-
def tobytes(self, order: str | None = ...) -> bytes: ... # pragma: no cover
57-
58-
def reshape(self, *shape: int, order: str = ...) -> "NDArrayLike": ... # pragma: no cover
59-
60-
def view(self, dtype: DType = ...) -> "NDArrayLike": ... # pragma: no cover
1+
import numpy.typing as npt
612

623

634
def is_ndarray_like(obj: object) -> bool:
645
"""Return True when `obj` is ndarray-like"""
65-
return isinstance(obj, NDArrayLike)
6+
return isinstance(obj, npt.ArrayLike)

numcodecs/tests/test_ndarray_like.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)