Skip to content

Commit d2ba1fe

Browse files
max-sixtyclaude
andauthored
Fix mypy errors with numpy 2.3 type stubs (#10792)
NumPy 2.3 has stricter type annotations that revealed legitimate type issues in our codebase. This commit: 1. Updates numpy constraint: removes numpy<2.3 and updates numba>=0.62 since numba 0.62+ supports numpy 2.3 2. Fixes type annotations in indexing.py for lists that hold mixed types 3. Adds appropriate type ignores for known limitations Fixes #10791 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Claude <[email protected]>
1 parent 2b947e9 commit d2ba1fe

File tree

5 files changed

+8
-9
lines changed

5 files changed

+8
-9
lines changed

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ accel = [
3030
"scipy>=1.13",
3131
"bottleneck",
3232
"numbagg>=0.8",
33-
"numba>=0.59",
33+
"numba>=0.62", # numba 0.62 added support for numpy 2.3
3434
"flox>=0.9",
3535
"opt_einsum",
36-
"numpy<2.3", # numba has not updated yet: https://github.com/numba/numba/issues/10105
3736
]
3837
complete = ["xarray[accel,etc,io,parallel,viz]"]
3938
io = [

xarray/core/indexing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,8 @@ def __init__(self, array: Any, key: ExplicitIndexer | None = None):
694694
"""
695695
if isinstance(array, type(self)) and key is None:
696696
# unwrap
697-
key = array.key # type: ignore[has-type]
698-
array = array.array # type: ignore[has-type]
697+
key = array.key # type: ignore[has-type, unused-ignore]
698+
array = array.array # type: ignore[has-type, unused-ignore]
699699

700700
if key is None:
701701
key = BasicIndexer((slice(None),) * array.ndim)
@@ -1251,8 +1251,8 @@ def _decompose_vectorized_indexer(
12511251
if indexing_support is IndexingSupport.VECTORIZED:
12521252
return indexer, BasicIndexer(())
12531253

1254-
backend_indexer_elems = []
1255-
np_indexer_elems = []
1254+
backend_indexer_elems: list[slice | np.ndarray[Any, np.dtype[np.generic]]] = []
1255+
np_indexer_elems: list[slice | np.ndarray[Any, np.dtype[np.generic]]] = []
12561256
# convert negative indices
12571257
indexer_elems = [
12581258
np.where(k < 0, k + s, k) if isinstance(k, np.ndarray) else k

xarray/tests/test_dataarray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3883,7 +3883,7 @@ def test_to_masked_array(self) -> None:
38833883
v = range(N)
38843884
da = DataArray(v)
38853885
ma = da.to_masked_array()
3886-
assert len(ma.mask) == N
3886+
assert isinstance(ma.mask, np.ndarray) and len(ma.mask) == N
38873887

38883888
def test_to_dataset_whole(self) -> None:
38893889
unnamed = DataArray([1, 2], dims="x")

xarray/tests/test_namedarray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def test_duck_array_class(self) -> None:
374374

375375
masked_a: np.ma.MaskedArray[Any, np.dtype[np.int64]]
376376
masked_a = np.ma.asarray([2.1, 4], dtype=np.dtype(np.int64)) # type: ignore[no-untyped-call]
377-
check_duck_array_typevar(masked_a)
377+
check_duck_array_typevar(masked_a) # type: ignore[arg-type] # MaskedArray not in duckarray union
378378

379379
custom_a: CustomArrayIndexable[Any, np.dtype[np.int64]]
380380
custom_a = CustomArrayIndexable(numpy_a)

xarray/tests/test_parallelcompat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def from_array(
9090
def rechunk(self, data: DummyChunkedArray, chunks, **kwargs) -> DummyChunkedArray:
9191
return data.rechunk(chunks, **kwargs)
9292

93-
def compute(self, *data: DummyChunkedArray, **kwargs) -> tuple[np.ndarray, ...]:
93+
def compute(self, *data: DummyChunkedArray, **kwargs) -> tuple[np.ndarray, ...]: # type: ignore[override]
9494
from dask.array import compute
9595

9696
return compute(*data, **kwargs)

0 commit comments

Comments
 (0)