Skip to content

Commit 748bb3a

Browse files
keewisdcherian
andauthored
avoid a couple of warnings in polyfit (#8939)
* 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 <[email protected]>
1 parent bfcb0a7 commit 748bb3a

File tree

5 files changed

+7
-7
lines changed

5 files changed

+7
-7
lines changed

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ filterwarnings = [
330330
"default:Using a non-tuple sequence for multidimensional indexing is deprecated:FutureWarning",
331331
"default:Duplicate dimension names present:UserWarning:xarray.namedarray.core",
332332
"default:::xarray.tests.test_strategies",
333+
# TODO: remove once we know how to deal with a changed signature in protocols
334+
"ignore:__array__ implementation doesn't accept a copy keyword, so passing copy=False failed.",
333335
]
334336

335337
log_cli_level = "INFO"

xarray/core/dataset.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ def __iter__(self) -> Iterator[Hashable]:
15241524

15251525
else:
15261526

1527-
def __array__(self, dtype=None):
1527+
def __array__(self, dtype=None, copy=None):
15281528
raise TypeError(
15291529
"cannot directly convert an xarray.Dataset into a "
15301530
"numpy array. Instead, create an xarray.DataArray "
@@ -8937,9 +8937,7 @@ def polyfit(
89378937
lhs = np.vander(x, order)
89388938

89398939
if rcond is None:
8940-
rcond = (
8941-
x.shape[0] * np.core.finfo(x.dtype).eps # type: ignore[attr-defined]
8942-
)
8940+
rcond = x.shape[0] * np.finfo(x.dtype).eps
89438941

89448942
# Weights:
89458943
if w is not None:

xarray/core/datatree.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ def __bool__(self) -> bool:
624624
def __iter__(self) -> Iterator[Hashable]:
625625
return itertools.chain(self.ds.data_vars, self.children)
626626

627-
def __array__(self, dtype=None):
627+
def __array__(self, dtype=None, copy=None):
628628
raise TypeError(
629629
"cannot directly convert a DataTree into a "
630630
"numpy array. Instead, create an xarray.DataArray "

xarray/tests/test_assertions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def dims(self):
149149
warnings.warn("warning in test")
150150
return super().dims
151151

152-
def __array__(self):
152+
def __array__(self, dtype=None, copy=None):
153153
warnings.warn("warning in test")
154154
return super().__array__()
155155

xarray/tests/test_formatting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ def test_lazy_array_wont_compute() -> None:
877877
from xarray.core.indexing import LazilyIndexedArray
878878

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

883883
arr = LazilyIndexedArrayNotComputable(np.array([1, 2]))

0 commit comments

Comments
 (0)