Skip to content

Commit

Permalink
Fix mypy on main (#9252)
Browse files Browse the repository at this point in the history
- Solve the numpy issues with appropriate ignores
- Hide the NamedArray issues with blanket ignores
  • Loading branch information
max-sixty authored Jul 17, 2024
1 parent 5d9d984 commit c2aebd8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@

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

import pandas as pd

Expand Down
10 changes: 5 additions & 5 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

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

import xarray as xr
from xarray import (
Expand Down Expand Up @@ -3522,9 +3522,9 @@ def test_from_multiindex_series_sparse(self) -> None:
import sparse

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

dense = DataArray.from_series(series, sparse=False)
expected_coords = sparse.COO.from_numpy(dense.data, np.nan).coords
Expand Down
8 changes: 5 additions & 3 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

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

import xarray as xr
from xarray import (
Expand Down Expand Up @@ -85,7 +85,9 @@
try:
from numpy import trapezoid # type: ignore[attr-defined,unused-ignore]
except ImportError:
from numpy import trapz as trapezoid
from numpy import ( # type: ignore[arg-type,no-redef,attr-defined,unused-ignore]
trapz as trapezoid,
)

sparse_array_type = array_type("sparse")

Expand Down
6 changes: 3 additions & 3 deletions xarray/tests/test_namedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_init(self, expected: Any) -> None:
# Fail:
(
("x",),
NamedArray("time", np.array([1, 2, 3])),
NamedArray("time", np.array([1, 2, 3])), # type: ignore
np.array([1, 2, 3]),
True,
),
Expand Down Expand Up @@ -341,7 +341,7 @@ def test_dims_setter(
def test_duck_array_class(self) -> None:
numpy_a: NDArray[np.int64]
numpy_a = np.array([2.1, 4], dtype=np.dtype(np.int64))
check_duck_array_typevar(numpy_a)
check_duck_array_typevar(numpy_a) # type: ignore

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

def test_warn_on_repeated_dimension_names(self) -> None:
with pytest.warns(UserWarning, match="Duplicate dimension names"):
NamedArray(("x", "x"), np.arange(4).reshape(2, 2))
NamedArray(("x", "x"), np.arange(4).reshape(2, 2)) # type: ignore
2 changes: 1 addition & 1 deletion xarray/tests/test_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def test_make_strategies_namespace(self, data):
warnings.filterwarnings(
"ignore", category=UserWarning, message=".+See NEP 47."
)
from numpy import ( # type: ignore[no-redef,unused-ignore]
from numpy import ( # type: ignore[attr-defined,no-redef,unused-ignore]
array_api as nxp,
)

Expand Down

0 comments on commit c2aebd8

Please sign in to comment.