Skip to content

Commit 4c865d6

Browse files
authored
typing fixes for mypy 0.931 and numpy 1.22 (#6155)
* typing fixes for mypy 0.931 and numpy 1.22 * remove duck_array_ops.pad
1 parent 0259063 commit 4c865d6

11 files changed

+17
-15
lines changed

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ repos:
4545
types-PyYAML,
4646
types-pytz,
4747
typing-extensions==3.10.0.0,
48+
numpy,
4849
]
4950
# run this occasionally, ref discussion https://github.com/pydata/xarray/pull/3194
5051
# - repo: https://github.com/asottile/pyupgrade

xarray/backends/file_manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import io
33
import threading
44
import warnings
5-
from typing import Any, Dict, cast
5+
from typing import Any, Dict
66

77
from ..core import utils
88
from ..core.options import OPTIONS
@@ -11,7 +11,7 @@
1111

1212
# Global cache for storing open files.
1313
FILE_CACHE: LRUCache[str, io.IOBase] = LRUCache(
14-
maxsize=cast(int, OPTIONS["file_cache_maxsize"]), on_evict=lambda k, v: v.close()
14+
maxsize=OPTIONS["file_cache_maxsize"], on_evict=lambda k, v: v.close()
1515
)
1616
assert FILE_CACHE.maxsize, "file cache must be at least size one"
1717

xarray/core/accessor_str.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ def func(x):
276276

277277
if isinstance(pat, np.ndarray):
278278
# apply_ufunc doesn't work for numpy arrays with output object dtypes
279-
func = np.vectorize(func)
280-
return func(pat)
279+
func_ = np.vectorize(func)
280+
return func_(pat)
281281
else:
282282
return _apply_str_ufunc(func=func, obj=pat, dtype=np.object_)
283283

xarray/core/dataset.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6918,9 +6918,9 @@ def polyfit(
69186918
if full:
69196919
rank = xr.DataArray(rank, name=xname + "matrix_rank")
69206920
variables[rank.name] = rank
6921-
sing = np.linalg.svd(lhs, compute_uv=False)
6921+
_sing = np.linalg.svd(lhs, compute_uv=False)
69226922
sing = xr.DataArray(
6923-
sing,
6923+
_sing,
69246924
dims=(degree_dim,),
69256925
coords={degree_dim: np.arange(rank - 1, -1, -1)},
69266926
name=xname + "singular_values",

xarray/core/duck_array_ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from numpy import zeros_like # noqa
1717
from numpy import around, broadcast_to # noqa
1818
from numpy import concatenate as _concatenate
19-
from numpy import einsum, isclose, isin, isnan, isnat, pad # noqa
19+
from numpy import einsum, isclose, isin, isnan, isnat # noqa
2020
from numpy import stack as _stack
2121
from numpy import take, tensordot, transpose, unravel_index # noqa
2222
from numpy import where as _where
@@ -168,7 +168,7 @@ def cumulative_trapezoid(y, x, axis):
168168

169169
# Pad so that 'axis' has same length in result as it did in y
170170
pads = [(1, 0) if i == axis else (0, 0) for i in range(y.ndim)]
171-
integrand = pad(integrand, pads, mode="constant", constant_values=0.0)
171+
integrand = np.pad(integrand, pads, mode="constant", constant_values=0.0)
172172

173173
return cumsum(integrand, axis=axis, skipna=False)
174174

xarray/core/formatting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def format_array_flat(array, max_width: int):
200200
(max_possibly_relevant < array.size) or (cum_len > max_width).any()
201201
):
202202
padding = " ... "
203-
max_len = max(int(np.argmax(cum_len + len(padding) - 1 > max_width)), 2) # type: ignore[type-var]
203+
max_len = max(int(np.argmax(cum_len + len(padding) - 1 > max_width)), 2)
204204
count = min(array.size, max_len)
205205
else:
206206
count = array.size

xarray/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ def _decompose_outer_indexer(
872872
backend_indexer: List[Any] = []
873873
np_indexer = []
874874
# make indexer positive
875-
pos_indexer = []
875+
pos_indexer: list[np.ndarray | int | np.number] = []
876876
for k, s in zip(indexer.tuple, shape):
877877
if isinstance(k, np.ndarray):
878878
pos_indexer.append(np.where(k < 0, k + s, k))

xarray/core/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ def read_magic_number_from_file(filename_or_obj, count=8) -> bytes:
652652
"file-like object read/write pointer not at the start of the file, "
653653
"please close and reopen, or use a context manager"
654654
)
655-
magic_number = filename_or_obj.read(count) # type: ignore
655+
magic_number = filename_or_obj.read(count)
656656
filename_or_obj.seek(0)
657657
else:
658658
raise TypeError(f"cannot read the magic number form {type(filename_or_obj)}")

xarray/core/variable.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ def _shift_one_dim(self, dim, count, fill_value=dtypes.NA):
12381238
dim_pad = (width, 0) if count >= 0 else (0, width)
12391239
pads = [(0, 0) if d != dim else dim_pad for d in self.dims]
12401240

1241-
data = duck_array_ops.pad(
1241+
data = np.pad(
12421242
trimmed_data.astype(dtype),
12431243
pads,
12441244
mode="constant",
@@ -1378,7 +1378,7 @@ def pad(
13781378
if reflect_type is not None:
13791379
pad_option_kwargs["reflect_type"] = reflect_type # type: ignore[assignment]
13801380

1381-
array = duck_array_ops.pad(
1381+
array = np.pad( # type: ignore[call-overload]
13821382
self.data.astype(dtype, copy=False),
13831383
pad_width_by_index,
13841384
mode=mode,

xarray/tests/test_computation.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,8 @@ def test_polyval(use_dask, use_datetime) -> None:
19341934
)
19351935
x = xr.core.missing.get_clean_interp_index(xcoord, "x")
19361936
else:
1937-
xcoord = x = np.arange(10)
1937+
x = np.arange(10)
1938+
xcoord = xr.DataArray(x, dims=("x",), name="x")
19381939

19391940
da = xr.DataArray(
19401941
np.stack((1.0 + x + 2.0 * x ** 2, 1.0 + 2.0 * x + 3.0 * x ** 2)),

xarray/tests/test_conventions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def test_decode_cf_datetime_transition_to_invalid(self) -> None:
292292
warnings.filterwarnings("ignore", "unable to decode time")
293293
ds_decoded = conventions.decode_cf(ds)
294294

295-
expected = [datetime(2000, 1, 1, 0, 0), datetime(2265, 10, 28, 0, 0)]
295+
expected = np.array([datetime(2000, 1, 1, 0, 0), datetime(2265, 10, 28, 0, 0)])
296296

297297
assert_array_equal(ds_decoded.time.values, expected)
298298

0 commit comments

Comments
 (0)