Skip to content

Commit 7228e8f

Browse files
max-sixtyclaude
andauthored
Remove mypy exclusion for test_dataarray (#10761)
* Remove mypy exclusion for xarray.tests.test_dataarray This commit removes test_dataarray from the mypy exclusions list and fixes all resulting type errors: - Add type ignores for intentional test cases that verify error handling - Add type ignore for scipy import fallback to object - Split test_astype_attrs to handle Variable separately from DataArray/Dataset due to different method signatures (Variable.astype doesn't have keep_attrs) - Add type ignores for MultiIndex.get_level_values calls where names may be None All tests continue to pass after these changes. Co-authored-by: Claude <[email protected]> * Update CLAUDE.md to use [email protected] for co-authorship This email format is more likely to be recognized by GitHub for bot/AI co-authorship attribution. Co-authored-by: Claude <[email protected]> * Fix mypy unused-ignore errors Use the established pattern of including unused-ignore in the type ignore comments for cases where the ignore necessity varies by environment: - scipy_.py: scipy import is optional, so ignore is unused when scipy is installed - test_dataarray.py: pandas-stubs versions vary in their MultiIndex.names typing Co-authored-by: Claude <[email protected]> * Clarify comment about test_astype_attrs split The split is required for mypy type checking, not due to API differences. Variable, DataArray, and Dataset don't share a common base class that mypy recognizes, so when they're in the same list, mypy infers the type as 'object', which lacks the attrs and astype methods. Co-authored-by: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
1 parent 41c7990 commit 7228e8f

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

CLAUDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ uv run dmypy run # Type checking with mypy
2929
- Never post "update" messages, progress reports, or explanatory comments on
3030
GitHub issues/PRs unless specifically instructed
3131
- When creating commits, always include a co-authorship trailer:
32-
`Co-authored-by: Claude <claude@anthropic.com>`
32+
`Co-authored-by: Claude <noreply@anthropic.com>`

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ check_untyped_defs = false
190190
module = [
191191
"xarray.tests.test_coding_times",
192192
"xarray.tests.test_dask",
193-
"xarray.tests.test_dataarray",
194193
"xarray.tests.test_duck_array_ops",
195194
"xarray.tests.test_indexing",
196195
"xarray.tests.test_sparse",

xarray/backends/scipy_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
try:
3939
from scipy.io import netcdf_file as netcdf_file_base
4040
except ImportError:
41-
netcdf_file_base = object
41+
netcdf_file_base = object # type: ignore[assignment,misc,unused-ignore] # scipy is optional
4242

4343

4444
if TYPE_CHECKING:

xarray/tests/test_dataarray.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def test_properties(self) -> None:
162162
with pytest.raises(ValueError, match=r"must be 1-dimensional"):
163163
self.ds["foo"].to_index()
164164
with pytest.raises(AttributeError):
165-
self.dv.variable = self.v
165+
self.dv.variable = self.v # type: ignore[misc]
166166

167167
def test_data_property(self) -> None:
168168
array = DataArray(np.zeros((3, 4)))
@@ -1243,7 +1243,7 @@ def test_head(self) -> None:
12431243
self.dv.isel({dim: slice(5) for dim in self.dv.dims}), self.dv.head()
12441244
)
12451245
with pytest.raises(TypeError, match=r"either dict-like or a single int"):
1246-
self.dv.head([3])
1246+
self.dv.head([3]) # type: ignore[arg-type]
12471247
with pytest.raises(TypeError, match=r"expected integer type"):
12481248
self.dv.head(x=3.1)
12491249
with pytest.raises(ValueError, match=r"expected positive int"):
@@ -1260,7 +1260,7 @@ def test_tail(self) -> None:
12601260
self.dv.isel({dim: slice(-5, None) for dim in self.dv.dims}), self.dv.tail()
12611261
)
12621262
with pytest.raises(TypeError, match=r"either dict-like or a single int"):
1263-
self.dv.tail([3])
1263+
self.dv.tail([3]) # type: ignore[arg-type]
12641264
with pytest.raises(TypeError, match=r"expected integer type"):
12651265
self.dv.tail(x=3.1)
12661266
with pytest.raises(ValueError, match=r"expected positive int"):
@@ -1273,7 +1273,7 @@ def test_thin(self) -> None:
12731273
self.dv.thin(6),
12741274
)
12751275
with pytest.raises(TypeError, match=r"either dict-like or a single int"):
1276-
self.dv.thin([3])
1276+
self.dv.thin([3]) # type: ignore[arg-type]
12771277
with pytest.raises(TypeError, match=r"expected integer type"):
12781278
self.dv.thin(x=3.1)
12791279
with pytest.raises(ValueError, match=r"expected positive int"):
@@ -2206,7 +2206,7 @@ def test_expand_dims_with_greater_dim_size(self) -> None:
22062206
assert_identical(other_way_expected, other_way)
22072207

22082208
def test_set_index(self) -> None:
2209-
indexes = [self.mindex.get_level_values(n) for n in self.mindex.names]
2209+
indexes = [self.mindex.get_level_values(n) for n in self.mindex.names] # type: ignore[arg-type,unused-ignore] # pandas-stubs varies
22102210
coords = {idx.name: ("x", idx) for idx in indexes}
22112211
array = DataArray(self.mda.values, coords=coords, dims="x")
22122212
expected = self.mda.copy()
@@ -2237,7 +2237,7 @@ def test_set_index(self) -> None:
22372237
obj.set_index(x="level_4")
22382238

22392239
def test_reset_index(self) -> None:
2240-
indexes = [self.mindex.get_level_values(n) for n in self.mindex.names]
2240+
indexes = [self.mindex.get_level_values(n) for n in self.mindex.names] # type: ignore[arg-type,unused-ignore] # pandas-stubs varies
22412241
coords = {idx.name: ("x", idx) for idx in indexes}
22422242
expected = DataArray(self.mda.values, coords=coords, dims="x")
22432243

@@ -2335,10 +2335,18 @@ def test_array_interface(self) -> None:
23352335
assert_equal(self.dv, np.maximum(self.dv, bar))
23362336

23372337
def test_astype_attrs(self) -> None:
2338-
for v in [self.va.copy(), self.mda.copy(), self.ds.copy()]:
2338+
# Split into two loops for mypy - Variable, DataArray, and Dataset
2339+
# don't share a common base class, so mypy infers type object for v,
2340+
# which doesn't have the attrs or astype methods
2341+
for v in [self.mda.copy(), self.ds.copy()]:
23392342
v.attrs["foo"] = "bar"
23402343
assert v.attrs == v.astype(float).attrs
23412344
assert not v.astype(float, keep_attrs=False).attrs
2345+
# Test Variable separately to avoid mypy inferring object type
2346+
va = self.va.copy()
2347+
va.attrs["foo"] = "bar"
2348+
assert va.attrs == va.astype(float).attrs
2349+
assert not va.astype(float, keep_attrs=False).attrs
23422350

23432351
def test_astype_dtype(self) -> None:
23442352
original = DataArray([-1, 1, 2, 3, 1000])

0 commit comments

Comments
 (0)