Skip to content

Commit defe8ae

Browse files
committed
ENH: Add arrayproxy.ArrayLike protocol
1 parent d2992ad commit defe8ae

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

nibabel/arrayproxy.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
2626
See :mod:`nibabel.tests.test_proxy_api` for proxy API conformance checks.
2727
"""
28+
import typing as ty
2829
import warnings
2930
from contextlib import contextmanager
3031
from threading import RLock
3132

3233
import numpy as np
34+
import numpy.typing as npt
3335

3436
from . import openers
3537
from .fileslice import canonical_slicers, fileslice
@@ -53,7 +55,25 @@
5355
KEEP_FILE_OPEN_DEFAULT = False
5456

5557

56-
class ArrayProxy:
58+
@ty.runtime_checkable
59+
class ArrayLike(ty.Protocol):
60+
"""Protocol for numpy ndarray-like objects
61+
62+
This is more stringent than :class:`numpy.typing.ArrayLike`, but guarantees
63+
access to shape, ndim and slicing.
64+
"""
65+
66+
shape: tuple[int, ...]
67+
ndim: int
68+
69+
def __array__(self, dtype: npt.DTypeLike | None = None, /) -> npt.NDArray:
70+
...
71+
72+
def __getitem__(self, key, /) -> npt.NDArray:
73+
...
74+
75+
76+
class ArrayProxy(ArrayLike):
5777
"""Class to act as proxy for the array that can be read from a file
5878
5979
The array proxy allows us to freeze the passed fileobj and header such that

0 commit comments

Comments
 (0)