Skip to content

Commit

Permalink
avoid a couple of warnings in polyfit (pydata#8939)
Browse files Browse the repository at this point in the history
* use `numpy.finfo` instead of `numpy.core.finfo`

* add `copy` to the signature of `__array__`

* don't pass `copy` to `asarray`

The `copy` kwarg appears to only exist in `numpy>=2`.

* try making the typing the same for all overloads

* change the typing for `NamedArray.__array__`

* fix the signature of `__array__`

* revert the change to the signature of `__array__`

* ignore all deprecation warnings about `__array__` gaining a new kwarg

I don't know enough about typing to ignore typing issues within a
protocol, so that was the only way I could figure out how to get this
PR unstuck. Once we know what to do here, we can remove the ignore in
a new PR.

* add back the `copy` kwarg if the protocol does not have type hints

* Update xarray/core/dataset.py

---------

Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
  • Loading branch information
keewis and dcherian authored May 1, 2024
1 parent bfcb0a7 commit 748bb3a
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ filterwarnings = [
"default:Using a non-tuple sequence for multidimensional indexing is deprecated:FutureWarning",
"default:Duplicate dimension names present:UserWarning:xarray.namedarray.core",
"default:::xarray.tests.test_strategies",
# TODO: remove once we know how to deal with a changed signature in protocols
"ignore:__array__ implementation doesn't accept a copy keyword, so passing copy=False failed.",
]

log_cli_level = "INFO"
Expand Down
6 changes: 2 additions & 4 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,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 @@ -8937,9 +8937,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

# 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
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 @@ -877,7 +877,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

0 comments on commit 748bb3a

Please sign in to comment.