From c2aebd81f144d9c28dec06b447371f540935c1a5 Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Wed, 17 Jul 2024 00:01:51 -0700 Subject: [PATCH] Fix mypy on main (#9252) - Solve the numpy issues with appropriate ignores - Hide the NamedArray issues with blanket ignores --- xarray/core/dataset.py | 4 ++-- xarray/tests/test_dataarray.py | 10 +++++----- xarray/tests/test_dataset.py | 8 +++++--- xarray/tests/test_namedarray.py | 6 +++--- xarray/tests/test_strategies.py | 2 +- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 1793abf02d8..fbe82153204 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -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 diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index b689bb8c02d..69f1a377513 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -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 ( @@ -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 diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 4db005ca3fb..f4d5e4681b4 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -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 ( @@ -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") diff --git a/xarray/tests/test_namedarray.py b/xarray/tests/test_namedarray.py index 3d3584448de..d8cea07e6ca 100644 --- a/xarray/tests/test_namedarray.py +++ b/xarray/tests/test_namedarray.py @@ -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, ), @@ -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] @@ -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 diff --git a/xarray/tests/test_strategies.py b/xarray/tests/test_strategies.py index 47f54382c03..79ae4769005 100644 --- a/xarray/tests/test_strategies.py +++ b/xarray/tests/test_strategies.py @@ -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, )