Skip to content

Commit 969d991

Browse files
authored
Fix mypy (#10232)
1 parent a66d5b5 commit 969d991

File tree

7 files changed

+25
-15
lines changed

7 files changed

+25
-15
lines changed

xarray/computation/fit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def polyfit(
103103
dim: Hashable,
104104
deg: int,
105105
skipna: bool | None = None,
106-
rcond: float | None = None,
106+
rcond: np.floating[Any] | float | None = None,
107107
w: Hashable | Any = None,
108108
full: bool = False,
109109
cov: bool | Literal["unscaled"] = False,
@@ -278,6 +278,7 @@ def polyfit(
278278
dims=other_dims,
279279
)
280280

281+
fac: Variable | int
281282
if cov:
282283
Vbase = np.linalg.inv(np.dot(lhs.T, lhs))
283284
Vbase /= np.outer(scale, scale)

xarray/core/utils.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,16 @@
6767
from enum import Enum
6868
from pathlib import Path
6969
from types import EllipsisType, ModuleType
70-
from typing import TYPE_CHECKING, Any, Generic, Literal, TypeGuard, TypeVar, overload
70+
from typing import (
71+
TYPE_CHECKING,
72+
Any,
73+
Generic,
74+
Literal,
75+
TypeGuard,
76+
TypeVar,
77+
cast,
78+
overload,
79+
)
7180

7281
import numpy as np
7382
import pandas as pd
@@ -751,7 +760,7 @@ def decode_numpy_dict_values(attrs: Mapping[K, V]) -> dict[K, V]:
751760
attrs = dict(attrs)
752761
for k, v in attrs.items():
753762
if isinstance(v, np.ndarray):
754-
attrs[k] = v.tolist()
763+
attrs[k] = cast(V, v.tolist())
755764
elif isinstance(v, np.generic):
756765
attrs[k] = v.item()
757766
return attrs

xarray/plot/dataarray_plot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,8 @@ def hist(
697697

698698
ax = get_axis(figsize, size, aspect, ax)
699699

700-
no_nan = np.ravel(darray.to_numpy())
701-
no_nan = no_nan[pd.notnull(no_nan)]
700+
no_nan_arr = np.ravel(darray.to_numpy())
701+
no_nan = no_nan_arr[pd.notnull(no_nan_arr)]
702702

703703
n, bins, patches = cast(
704704
tuple[np.ndarray, np.ndarray, Union["BarContainer", "Polygon"]],

xarray/plot/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
)
1414
from datetime import date, datetime
1515
from inspect import getfullargspec
16-
from typing import TYPE_CHECKING, Any, Literal, overload
16+
from typing import TYPE_CHECKING, Any, Literal, cast, overload
1717

1818
import numpy as np
1919
import pandas as pd
@@ -1744,8 +1744,8 @@ def _add_legend(
17441744
# Only save unique values:
17451745
u, ind = np.unique(lbl, return_index=True)
17461746
ind = np.argsort(ind)
1747-
lbl = u[ind].tolist()
1748-
hdl = np.array(hdl)[ind].tolist()
1747+
lbl = cast(list, u[ind].tolist())
1748+
hdl = cast(list, np.array(hdl)[ind].tolist())
17491749

17501750
# Add a subtitle:
17511751
hdl, lbl = _legend_add_subtitle(hdl, lbl, label_from_attrs(huesizeplt.data))

xarray/tests/test_dataarray.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ def test_getitem_empty_index(self) -> None:
724724

725725
def test_setitem(self) -> None:
726726
# basic indexing should work as numpy's indexing
727-
tuples = [
727+
tuples: list[tuple[int | list[int] | slice, int | list[int] | slice]] = [
728728
(0, 0),
729729
(0, slice(None, None)),
730730
(slice(None, None), slice(None, None)),
@@ -3608,8 +3608,8 @@ def test_to_and_from_dict(
36083608
return_data = array.to_numpy()
36093609
coords_data = np.array(["a", "b"])
36103610
if data == "list" or data is True:
3611-
return_data = return_data.tolist()
3612-
coords_data = coords_data.tolist()
3611+
return_data = return_data.tolist() # type:ignore[assignment]
3612+
coords_data = coords_data.tolist() # type:ignore[assignment]
36133613

36143614
expected: dict[str, Any] = {
36153615
"name": "foo",

xarray/tests/test_datatree.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,11 +1025,11 @@ def test_map_keep_attrs(self) -> None:
10251025
ds = xr.Dataset({"data": data}, attrs={"ds": "attrs"})
10261026
dt = DataTree(ds)
10271027

1028-
def func(ds):
1028+
def func_keep(ds):
10291029
# x.mean() removes the attrs of the data_vars
10301030
return ds.map(lambda x: x.mean(), keep_attrs=True)
10311031

1032-
result = xr.map_over_datasets(func, dt)
1032+
result = xr.map_over_datasets(func_keep, dt)
10331033
expected = dt.mean(keep_attrs=True)
10341034
xr.testing.assert_identical(result, expected)
10351035

xarray/tests/test_pandas_to_xarray.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
timedelta_range,
5252
)
5353

54-
indices_dict = {
54+
indices_dict: dict[str, Index] = {
5555
"object": Index([f"pandas_{i}" for i in range(10)], dtype=object),
5656
"string": Index([f"pandas_{i}" for i in range(10)], dtype="str"),
5757
"datetime": date_range("2020-01-01", periods=10),
@@ -78,7 +78,7 @@
7878
np.arange(10, dtype="complex128") + 1.0j * np.arange(10, dtype="complex128")
7979
),
8080
"categorical": CategoricalIndex(list("abcd") * 2),
81-
"interval": IntervalIndex.from_breaks(np.linspace(0, 100, num=11)),
81+
"interval": IntervalIndex.from_breaks(np.linspace(0, 100, num=11, dtype="int")),
8282
"empty": Index([]),
8383
# "tuples": MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])),
8484
# "mi-with-dt64tz-level": _create_mi_with_dt64tz_level(),

0 commit comments

Comments
 (0)