Skip to content

Fix mypy failures on main #10232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion xarray/computation/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def polyfit(
dim: Hashable,
deg: int,
skipna: bool | None = None,
rcond: float | None = None,
rcond: np.floating[Any] | float | None = None,
w: Hashable | Any = None,
full: bool = False,
cov: bool | Literal["unscaled"] = False,
Expand Down Expand Up @@ -278,6 +278,7 @@ def polyfit(
dims=other_dims,
)

fac: Variable | int
if cov:
Vbase = np.linalg.inv(np.dot(lhs.T, lhs))
Vbase /= np.outer(scale, scale)
Expand Down
13 changes: 11 additions & 2 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@
from enum import Enum
from pathlib import Path
from types import EllipsisType, ModuleType
from typing import TYPE_CHECKING, Any, Generic, Literal, TypeGuard, TypeVar, overload
from typing import (
TYPE_CHECKING,
Any,
Generic,
Literal,
TypeGuard,
TypeVar,
cast,
overload,
)

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -751,7 +760,7 @@ def decode_numpy_dict_values(attrs: Mapping[K, V]) -> dict[K, V]:
attrs = dict(attrs)
for k, v in attrs.items():
if isinstance(v, np.ndarray):
attrs[k] = v.tolist()
attrs[k] = cast(V, v.tolist())
elif isinstance(v, np.generic):
attrs[k] = v.item()
return attrs
Expand Down
4 changes: 2 additions & 2 deletions xarray/plot/dataarray_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,8 @@ def hist(

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

no_nan = np.ravel(darray.to_numpy())
no_nan = no_nan[pd.notnull(no_nan)]
no_nan_arr = np.ravel(darray.to_numpy())
no_nan = no_nan_arr[pd.notnull(no_nan_arr)]

n, bins, patches = cast(
tuple[np.ndarray, np.ndarray, Union["BarContainer", "Polygon"]],
Expand Down
6 changes: 3 additions & 3 deletions xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)
from datetime import date, datetime
from inspect import getfullargspec
from typing import TYPE_CHECKING, Any, Literal, overload
from typing import TYPE_CHECKING, Any, Literal, cast, overload

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -1744,8 +1744,8 @@ def _add_legend(
# Only save unique values:
u, ind = np.unique(lbl, return_index=True)
ind = np.argsort(ind)
lbl = u[ind].tolist()
hdl = np.array(hdl)[ind].tolist()
lbl = cast(list, u[ind].tolist())
hdl = cast(list, np.array(hdl)[ind].tolist())

# Add a subtitle:
hdl, lbl = _legend_add_subtitle(hdl, lbl, label_from_attrs(huesizeplt.data))
Expand Down
6 changes: 3 additions & 3 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ def test_getitem_empty_index(self) -> None:

def test_setitem(self) -> None:
# basic indexing should work as numpy's indexing
tuples = [
tuples: list[tuple[int | list[int] | slice, int | list[int] | slice]] = [
(0, 0),
(0, slice(None, None)),
(slice(None, None), slice(None, None)),
Expand Down Expand Up @@ -3608,8 +3608,8 @@ def test_to_and_from_dict(
return_data = array.to_numpy()
coords_data = np.array(["a", "b"])
if data == "list" or data is True:
return_data = return_data.tolist()
coords_data = coords_data.tolist()
return_data = return_data.tolist() # type:ignore[assignment]
coords_data = coords_data.tolist() # type:ignore[assignment]

expected: dict[str, Any] = {
"name": "foo",
Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/test_datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,11 +1025,11 @@ def test_map_keep_attrs(self) -> None:
ds = xr.Dataset({"data": data}, attrs={"ds": "attrs"})
dt = DataTree(ds)

def func(ds):
def func_keep(ds):
# x.mean() removes the attrs of the data_vars
return ds.map(lambda x: x.mean(), keep_attrs=True)

result = xr.map_over_datasets(func, dt)
result = xr.map_over_datasets(func_keep, dt)
expected = dt.mean(keep_attrs=True)
xr.testing.assert_identical(result, expected)

Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/test_pandas_to_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
timedelta_range,
)

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