From 7ab5fff4cc14b7702563eda8fa1c6998944b3836 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Wed, 3 Sep 2025 15:09:21 -0700 Subject: [PATCH 1/2] Enable mypy checking for test_utils and test_parallelcompat MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removes xarray.tests.test_utils and xarray.tests.test_parallelcompat from the mypy exclusion list by fixing type issues in these files: - Added type annotations where needed - Added targeted type: ignore comments for intentional test behaviors - Fixed function call signatures to match expected types All tests continue to pass and mypy now checks these files with check_untyped_defs=true. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- pyproject.toml | 2 -- xarray/tests/test_parallelcompat.py | 2 +- xarray/tests/test_utils.py | 16 ++++++++-------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 7426ff05518..dc67e345d89 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -199,11 +199,9 @@ module = [ "xarray.tests.test_indexing", "xarray.tests.test_merge", "xarray.tests.test_missing", - "xarray.tests.test_parallelcompat", "xarray.tests.test_sparse", "xarray.tests.test_ufuncs", "xarray.tests.test_units", - "xarray.tests.test_utils", "xarray.tests.test_variable", "xarray.tests.test_weighted", ] diff --git a/xarray/tests/test_parallelcompat.py b/xarray/tests/test_parallelcompat.py index 67e3d00cfbe..558df62138b 100644 --- a/xarray/tests/test_parallelcompat.py +++ b/xarray/tests/test_parallelcompat.py @@ -48,7 +48,7 @@ def __new__( def __array_finalize__(self, obj): if obj is None: return - self.chunks = getattr(obj, "chunks", None) + self.chunks = getattr(obj, "chunks", None) # type: ignore[assignment] def rechunk(self, chunks, **kwargs): copied = self.copy() diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index 0e6bbf29a45..f3333d188cc 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -23,7 +23,7 @@ def new_method(): pass old_method = utils.alias(new_method, "old_method") - assert "deprecated" in old_method.__doc__ + assert "deprecated" in old_method.__doc__ # type: ignore[operator] with pytest.warns(Warning, match="deprecated"): old_method() @@ -102,10 +102,10 @@ def test_compat_dict_union(self): utils.compat_dict_union(self.x, self.z) def test_dict_equiv(self): - x = {} + x: dict = {} x["a"] = 3 x["b"] = np.array([1, 2, 3]) - y = {} + y: dict = {} y["b"] = np.array([1.0, 2.0, 3.0]) y["a"] = 3 assert utils.dict_equiv(x, y) # two nparrays are equal @@ -129,11 +129,11 @@ def test_dict_equiv(self): def test_frozen(self): x = utils.Frozen(self.x) with pytest.raises(TypeError): - x["foo"] = "bar" + x["foo"] = "bar" # type: ignore[index] with pytest.raises(TypeError): - del x["a"] + del x["a"] # type: ignore[attr-defined] with pytest.raises(AttributeError): - x.update(self.y) + x.update(self.y) # type: ignore[attr-defined] assert x.mapping == self.x assert repr(x) in ( "Frozen({'a': 'A', 'b': 'B'})", @@ -231,11 +231,11 @@ def test_hidden_key_dict(): def test_either_dict_or_kwargs(): - result = either_dict_or_kwargs(dict(a=1), None, "foo") + result = either_dict_or_kwargs(dict(a=1), {}, "foo") expected = dict(a=1) assert result == expected - result = either_dict_or_kwargs(None, dict(a=1), "foo") + result = either_dict_or_kwargs({}, dict(a=1), "foo") expected = dict(a=1) assert result == expected From 2bfd13195a35ad8045e2b32cc04b9ee6de02dc38 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Wed, 3 Sep 2025 15:22:56 -0700 Subject: [PATCH 2/2] Also enable mypy for test_missing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removes xarray.tests.test_missing from the mypy exclusion list by fixing type issues: - Added type: ignore comments for intentional test cases with invalid methods - Added type annotation for kwargs dict All 115 tests continue to pass. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- pyproject.toml | 1 - xarray/tests/test_missing.py | 13 +++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index dc67e345d89..b8f0f0ad829 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -198,7 +198,6 @@ module = [ "xarray.tests.test_duck_array_ops", "xarray.tests.test_indexing", "xarray.tests.test_merge", - "xarray.tests.test_missing", "xarray.tests.test_sparse", "xarray.tests.test_ufuncs", "xarray.tests.test_units", diff --git a/xarray/tests/test_missing.py b/xarray/tests/test_missing.py index 271813d477b..ba6616a6784 100644 --- a/xarray/tests/test_missing.py +++ b/xarray/tests/test_missing.py @@ -1,6 +1,7 @@ from __future__ import annotations import itertools +from typing import Any from unittest import mock import numpy as np @@ -204,7 +205,7 @@ def test_interpolate_unsorted_index_raises(): vals = np.array([1, 2, 3], dtype=np.float64) expected = xr.DataArray(vals, dims="x", coords={"x": [2, 1, 3]}) with pytest.raises(ValueError, match=r"Index 'x' must be monotonically increasing"): - expected.interpolate_na(dim="x", method="index") + expected.interpolate_na(dim="x", method="index") # type: ignore[arg-type] def test_interpolate_no_dim_raises(): @@ -216,14 +217,14 @@ def test_interpolate_no_dim_raises(): def test_interpolate_invalid_interpolator_raises(): da = xr.DataArray(np.array([1, 2, np.nan, 5], dtype=np.float64), dims="x") with pytest.raises(ValueError, match=r"not a valid"): - da.interpolate_na(dim="x", method="foo") + da.interpolate_na(dim="x", method="foo") # type: ignore[arg-type] def test_interpolate_duplicate_values_raises(): data = np.random.randn(2, 3) da = xr.DataArray(data, coords=[("x", ["a", "a"]), ("y", [0, 1, 2])]) with pytest.raises(ValueError, match=r"Index 'x' has duplicate values"): - da.interpolate_na(dim="x", method="foo") + da.interpolate_na(dim="x", method="foo") # type: ignore[arg-type] def test_interpolate_multiindex_raises(): @@ -331,15 +332,15 @@ def test_interpolate_limits(): @requires_scipy def test_interpolate_methods(): for method in ["linear", "nearest", "zero", "slinear", "quadratic", "cubic"]: - kwargs = {} + kwargs: dict[str, Any] = {} da = xr.DataArray( np.array([0, 1, 2, np.nan, np.nan, np.nan, 6, 7, 8], dtype=np.float64), dims="x", ) - actual = da.interpolate_na("x", method=method, **kwargs) + actual = da.interpolate_na("x", method=method, **kwargs) # type: ignore[arg-type] assert actual.isnull().sum() == 0 - actual = da.interpolate_na("x", method=method, limit=2, **kwargs) + actual = da.interpolate_na("x", method=method, limit=2, **kwargs) # type: ignore[arg-type] assert actual.isnull().sum() == 1