Skip to content

Commit

Permalink
Adds copy parameter to __array__ for numpy 2.0
Browse files Browse the repository at this point in the history
andrew-s28 committed Aug 21, 2024
1 parent ed5900b commit ae0d8fc
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
@@ -162,8 +162,22 @@ def __int__(self: Any) -> int:
def __complex__(self: Any) -> complex:
return complex(self.values)

def __array__(self: Any, dtype: DTypeLike | None = None) -> np.ndarray:
return np.asarray(self.values, dtype=dtype)
def __array__(
self: Any, dtype: DTypeLike | None = None, copy: bool | None = None
) -> np.ndarray:
if not copy:
if np.lib.NumpyVersion(np.__version__) >= "2.0.0":
copy = None
elif np.lib.NumpyVersion(np.__version__) <= "1.28.0":
copy = False
else:
# 2.0.0 dev versions, handle cases where copy may or may not exist
try:
np.array([1]).__array__(copy=None)
copy = None
except TypeError:
copy = False
return np.array(self.values, dtype=dtype, copy=copy)

def __repr__(self) -> str:
return formatting.array_repr(self)

0 comments on commit ae0d8fc

Please sign in to comment.