Skip to content

Commit c2aebd8

Browse files
authored
Fix mypy on main (#9252)
- Solve the numpy issues with appropriate ignores - Hide the NamedArray issues with blanket ignores
1 parent 5d9d984 commit c2aebd8

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

xarray/core/dataset.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929

3030
# remove once numpy 2.0 is the oldest supported version
3131
try:
32-
from numpy.exceptions import RankWarning # type: ignore[attr-defined,unused-ignore]
32+
from numpy.exceptions import RankWarning
3333
except ImportError:
34-
from numpy import RankWarning
34+
from numpy import RankWarning # type: ignore[no-redef,attr-defined,unused-ignore]
3535

3636
import pandas as pd
3737

xarray/tests/test_dataarray.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
# remove once numpy 2.0 is the oldest supported version
1717
try:
18-
from numpy.exceptions import RankWarning # type: ignore[attr-defined,unused-ignore]
18+
from numpy.exceptions import RankWarning
1919
except ImportError:
20-
from numpy import RankWarning
20+
from numpy import RankWarning # type: ignore[no-redef,attr-defined,unused-ignore]
2121

2222
import xarray as xr
2323
from xarray import (
@@ -3522,9 +3522,9 @@ def test_from_multiindex_series_sparse(self) -> None:
35223522
import sparse
35233523

35243524
idx = pd.MultiIndex.from_product([np.arange(3), np.arange(5)], names=["a", "b"])
3525-
series = pd.Series(np.random.RandomState(0).random(len(idx)), index=idx).sample(
3526-
n=5, random_state=3
3527-
)
3525+
series: pd.Series = pd.Series(
3526+
np.random.RandomState(0).random(len(idx)), index=idx
3527+
).sample(n=5, random_state=3)
35283528

35293529
dense = DataArray.from_series(series, sparse=False)
35303530
expected_coords = sparse.COO.from_numpy(dense.data, np.nan).coords

xarray/tests/test_dataset.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
# remove once numpy 2.0 is the oldest supported version
1919
try:
20-
from numpy.exceptions import RankWarning # type: ignore[attr-defined,unused-ignore]
20+
from numpy.exceptions import RankWarning
2121
except ImportError:
22-
from numpy import RankWarning
22+
from numpy import RankWarning # type: ignore[no-redef,attr-defined,unused-ignore]
2323

2424
import xarray as xr
2525
from xarray import (
@@ -85,7 +85,9 @@
8585
try:
8686
from numpy import trapezoid # type: ignore[attr-defined,unused-ignore]
8787
except ImportError:
88-
from numpy import trapz as trapezoid
88+
from numpy import ( # type: ignore[arg-type,no-redef,attr-defined,unused-ignore]
89+
trapz as trapezoid,
90+
)
8991

9092
sparse_array_type = array_type("sparse")
9193

xarray/tests/test_namedarray.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def test_init(self, expected: Any) -> None:
180180
# Fail:
181181
(
182182
("x",),
183-
NamedArray("time", np.array([1, 2, 3])),
183+
NamedArray("time", np.array([1, 2, 3])), # type: ignore
184184
np.array([1, 2, 3]),
185185
True,
186186
),
@@ -341,7 +341,7 @@ def test_dims_setter(
341341
def test_duck_array_class(self) -> None:
342342
numpy_a: NDArray[np.int64]
343343
numpy_a = np.array([2.1, 4], dtype=np.dtype(np.int64))
344-
check_duck_array_typevar(numpy_a)
344+
check_duck_array_typevar(numpy_a) # type: ignore
345345

346346
masked_a: np.ma.MaskedArray[Any, np.dtype[np.int64]]
347347
masked_a = np.ma.asarray([2.1, 4], dtype=np.dtype(np.int64)) # type: ignore[no-untyped-call]
@@ -560,4 +560,4 @@ def test_broadcast_to_errors(
560560

561561
def test_warn_on_repeated_dimension_names(self) -> None:
562562
with pytest.warns(UserWarning, match="Duplicate dimension names"):
563-
NamedArray(("x", "x"), np.arange(4).reshape(2, 2))
563+
NamedArray(("x", "x"), np.arange(4).reshape(2, 2)) # type: ignore

xarray/tests/test_strategies.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def test_make_strategies_namespace(self, data):
217217
warnings.filterwarnings(
218218
"ignore", category=UserWarning, message=".+See NEP 47."
219219
)
220-
from numpy import ( # type: ignore[no-redef,unused-ignore]
220+
from numpy import ( # type: ignore[attr-defined,no-redef,unused-ignore]
221221
array_api as nxp,
222222
)
223223

0 commit comments

Comments
 (0)