Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid a couple of warnings in polyfit #8939

Merged
merged 15 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ def __int__(self: Any) -> int:
def __complex__(self: Any) -> complex:
return complex(self.values)

def __array__(self: Any, dtype: DTypeLike | None = None) -> np.ndarray:
def __array__(
self: Any, dtype: DTypeLike | None = None, copy: bool | None = None
) -> np.ndarray:
return np.asarray(self.values, dtype=dtype)

def __repr__(self) -> str:
Expand Down
6 changes: 2 additions & 4 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ def __iter__(self) -> Iterator[Hashable]:

else:

def __array__(self, dtype=None):
def __array__(self, dtype=None, copy=None):
raise TypeError(
"cannot directly convert an xarray.Dataset into a "
"numpy array. Instead, create an xarray.DataArray "
Expand Down Expand Up @@ -8862,9 +8862,7 @@ def polyfit(
lhs = np.vander(x, order)

if rcond is None:
rcond = (
x.shape[0] * np.core.finfo(x.dtype).eps # type: ignore[attr-defined]
)
rcond = x.shape[0] * np.finfo(x.dtype).eps # type: ignore[attr-defined]
dcherian marked this conversation as resolved.
Show resolved Hide resolved

# Weights:
if w is not None:
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ def __bool__(self) -> bool:
def __iter__(self) -> Iterator[Hashable]:
return itertools.chain(self.ds.data_vars, self.children)

def __array__(self, dtype=None):
def __array__(self, dtype=None, copy=None):
raise TypeError(
"cannot directly convert a DataTree into a "
"numpy array. Instead, create an xarray.DataArray "
Expand Down
4 changes: 2 additions & 2 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ def values(self) -> range:
def data(self) -> range:
return range(self.size)

def __array__(self) -> np.ndarray:
return np.arange(self.size)
def __array__(self, dtype=None, copy=None) -> np.ndarray:
return np.arange(self.size, dtype=dtype)

@property
def shape(self) -> tuple[int]:
Expand Down
24 changes: 18 additions & 6 deletions xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,9 @@ class ExplicitlyIndexed:

__slots__ = ()

def __array__(self, dtype: np.typing.DTypeLike = None) -> np.ndarray:
def __array__(
self, dtype: np.typing.DTypeLike = None, copy: bool | None = None
) -> np.ndarray:
# Leave casting to an array up to the underlying array type.
return np.asarray(self.get_duck_array(), dtype=dtype)

Expand All @@ -518,7 +520,9 @@ def get_duck_array(self):
key = BasicIndexer((slice(None),) * self.ndim)
return self[key]

def __array__(self, dtype: np.typing.DTypeLike = None) -> np.ndarray:
def __array__(
self, dtype: np.typing.DTypeLike = None, copy: bool | None = None
) -> np.ndarray:
# This is necessary because we apply the indexing key in self.get_duck_array()
# Note this is the base class for all lazy indexing classes
return np.asarray(self.get_duck_array(), dtype=dtype)
Expand Down Expand Up @@ -568,7 +572,9 @@ def __init__(self, array, indexer_cls: type[ExplicitIndexer] = BasicIndexer):
self.array = as_indexable(array)
self.indexer_cls = indexer_cls

def __array__(self, dtype: np.typing.DTypeLike = None) -> np.ndarray:
def __array__(
self, dtype: np.typing.DTypeLike = None, copy: bool | None = None
) -> np.ndarray:
return np.asarray(self.get_duck_array(), dtype=dtype)

def get_duck_array(self):
Expand Down Expand Up @@ -826,7 +832,9 @@ def __init__(self, array):
def _ensure_cached(self):
self.array = as_indexable(self.array.get_duck_array())

def __array__(self, dtype: np.typing.DTypeLike = None) -> np.ndarray:
def __array__(
self, dtype: np.typing.DTypeLike = None, copy: bool | None = None
) -> np.ndarray:
return np.asarray(self.get_duck_array(), dtype=dtype)

def get_duck_array(self):
Expand Down Expand Up @@ -1667,7 +1675,9 @@ def __init__(self, array: pd.Index, dtype: DTypeLike = None):
def dtype(self) -> np.dtype:
return self._dtype

def __array__(self, dtype: DTypeLike = None) -> np.ndarray:
def __array__(
self, dtype: DTypeLike = None, copy: bool | None = None
) -> np.ndarray:
if dtype is None:
dtype = self.dtype
array = self.array
Expand Down Expand Up @@ -1821,7 +1831,9 @@ def __init__(
super().__init__(array, dtype)
self.level = level

def __array__(self, dtype: DTypeLike = None) -> np.ndarray:
def __array__(
self, dtype: DTypeLike = None, copy: bool | None = None
) -> np.ndarray:
if dtype is None:
dtype = self.dtype
if self.level is not None:
Expand Down
8 changes: 5 additions & 3 deletions xarray/namedarray/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,15 @@ def __getitem__(
) -> _arrayfunction[Any, _DType_co] | Any: ...

@overload
def __array__(self, dtype: None = ..., /) -> np.ndarray[Any, _DType_co]: ...
def __array__(
self, dtype: None = ..., copy: None = ..., /
) -> np.ndarray[Any, _DType_co]: ...

@overload
def __array__(self, dtype: _DType, /) -> np.ndarray[Any, _DType]: ...
def __array__(self, dtype: _DType, copy: bool, /) -> np.ndarray[Any, _DType]: ...

def __array__(
self, dtype: _DType | None = ..., /
self, dtype: _DType | None = ..., copy: bool | None = None, /
Copy link
Collaborator Author

@keewis keewis Apr 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Illviljan (or anyone else), I might need some help to get the typing right... basically, __array__ is supposed to have a new kwarg, copy, which is either a bool or None.

Edit: my best guess, adding copy: bool | None = None to the overloads, didn't work

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's more places with __array__ to fix:

xarray/core/indexing.py:472: note:     Got:
xarray/core/indexing.py:472: note:         @overload
xarray/core/indexing.py:472: note:         def __array__(self, None = ..., /) -> ndarray[Any, dtype[generic]]
xarray/core/indexing.py:472: note:         @overload
xarray/core/indexing.py:472: note:         def [_DType <: dtype[Any]] __array__(self, _DType, /) -> ndarray[Any, _DType]

Copy link
Contributor

@Illviljan Illviljan Apr 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numpy's type hints:

    @overload
    def __array__(
        self, dtype: None = ..., /, *, copy: None | bool = ...
    ) -> ndarray[Any, _DType_co]: ...
    @overload
    def __array__(
        self, dtype: _DType, /, *, copy: None | bool = ...
    ) -> ndarray[Any, _DType]: ...

https://github.com/numpy/numpy/blob/ab7649fe2ed8f0f0260322d66631b8dfab57deff/numpy/__init__.pyi#L1466-L1473

) -> np.ndarray[Any, _DType] | np.ndarray[Any, _DType_co]: ...

# TODO: Should return the same subclass but with a new dtype generic.
Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def __init__(self, array):
def get_duck_array(self):
raise UnexpectedDataAccess("Tried accessing data")

def __array__(self, dtype: np.typing.DTypeLike = None):
def __array__(self, dtype: np.typing.DTypeLike = None, copy: bool | None = None):
raise UnexpectedDataAccess("Tried accessing data")

def __getitem__(self, key):
Expand All @@ -226,7 +226,7 @@ def __init__(self, array: np.ndarray):
def __getitem__(self, key):
return type(self)(self.array[key])

def __array__(self, dtype: np.typing.DTypeLike = None):
def __array__(self, dtype: np.typing.DTypeLike = None, copy: bool | None = None):
raise UnexpectedDataAccess("Tried accessing data")

def __array_namespace__(self):
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def dims(self):
warnings.warn("warning in test")
return super().dims

def __array__(self):
def __array__(self, dtype=None, copy=None):
warnings.warn("warning in test")
return super().__array__()

Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ def test_lazy_array_wont_compute() -> None:
from xarray.core.indexing import LazilyIndexedArray

class LazilyIndexedArrayNotComputable(LazilyIndexedArray):
def __array__(self, dtype=None):
def __array__(self, dtype=None, copy=None):
raise NotImplementedError("Computing this array is not possible.")

arr = LazilyIndexedArrayNotComputable(np.array([1, 2]))
Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/test_namedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def shape(self) -> _Shape:
class CustomArray(
CustomArrayBase[_ShapeType_co, _DType_co], Generic[_ShapeType_co, _DType_co]
):
def __array__(self) -> np.ndarray[Any, np.dtype[np.generic]]:
return np.array(self.array)
def __array__(self, dtype=None, copy=None) -> np.ndarray[Any, np.dtype[np.generic]]:
return np.array(self.array, dtype=dtype)


class CustomArrayIndexable(
Expand Down
Loading