Skip to content

Commit 8eee7f5

Browse files
committed
ENH: Add arrayproxy.ArrayLike protocol
1 parent f48a2d9 commit 8eee7f5

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

nibabel/arrayproxy.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@
2525
2626
See :mod:`nibabel.tests.test_proxy_api` for proxy API conformance checks.
2727
"""
28+
from __future__ import annotations
29+
30+
import typing as ty
2831
import warnings
2932
from contextlib import contextmanager
3033
from threading import RLock
3134

3235
import numpy as np
36+
import numpy.typing as npt
3337

3438
from . import openers
3539
from .fileslice import canonical_slicers, fileslice
@@ -53,7 +57,24 @@
5357
KEEP_FILE_OPEN_DEFAULT = False
5458

5559

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

0 commit comments

Comments
 (0)