Skip to content

Commit c60f9b0

Browse files
authored
Fix mypy issues & reenable in tests (#6581)
* Run mypy tests (but always pass) So we can at least see the result * Fix mypy
1 parent 6fbeb13 commit c60f9b0

14 files changed

+38
-32
lines changed

.github/workflows/ci-additional.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,9 @@ jobs:
105105
run: |
106106
python -m pip install mypy
107107
108-
# Temporarily overriding to be true due to https://github.com/pydata/xarray/issues/6551
109-
# python -m mypy --install-types --non-interactive
110108
- name: Run mypy
111109
run: |
112-
python -m mypy --install-types --non-interactive || true
110+
python -m mypy --install-types --non-interactive
113111
114112
min-version-policy:
115113
name: Minimum Version Policy

xarray/backends/api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
try:
3636
from dask.delayed import Delayed
3737
except ImportError:
38-
Delayed = None
38+
Delayed = None # type: ignore
3939

4040

4141
DATAARRAY_NAME = "__xarray_dataarray_name__"

xarray/backends/locks.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
from dask.utils import SerializableLock
88
except ImportError:
99
# no need to worry about serializing the lock
10-
SerializableLock = threading.Lock
10+
SerializableLock = threading.Lock # type: ignore
1111

1212
try:
1313
from dask.distributed import Lock as DistributedLock
1414
except ImportError:
15-
DistributedLock = None
15+
DistributedLock = None # type: ignore
1616

1717

1818
# Locks used by multiple backends.

xarray/core/_typed_ops.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ from .variable import Variable
2121
try:
2222
from dask.array import Array as DaskArray
2323
except ImportError:
24-
DaskArray = np.ndarray
24+
DaskArray = np.ndarray # type: ignore
2525

2626
# DatasetOpsMixin etc. are parent classes of Dataset etc.
2727
# Because of https://github.com/pydata/xarray/issues/5755, we redefine these. Generally

xarray/core/computation.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
Iterable,
1818
Mapping,
1919
Sequence,
20-
overload,
2120
)
2221

2322
import numpy as np
@@ -1846,24 +1845,26 @@ def where(cond, x, y, keep_attrs=None):
18461845
)
18471846

18481847

1849-
@overload
1850-
def polyval(coord: DataArray, coeffs: DataArray, degree_dim: Hashable) -> DataArray:
1851-
...
1848+
# These overloads seem not to work — mypy says it can't find a matching overload for
1849+
# `DataArray` & `DataArray`, despite that being in the first overload. Would be nice to
1850+
# have overloaded functions rather than just `T_Xarray` for everything.
18521851

1852+
# @overload
1853+
# def polyval(coord: DataArray, coeffs: DataArray, degree_dim: Hashable) -> DataArray:
1854+
# ...
18531855

1854-
@overload
1855-
def polyval(coord: T_Xarray, coeffs: Dataset, degree_dim: Hashable) -> Dataset:
1856-
...
18571856

1857+
# @overload
1858+
# def polyval(coord: T_Xarray, coeffs: Dataset, degree_dim: Hashable) -> Dataset:
1859+
# ...
18581860

1859-
@overload
1860-
def polyval(coord: Dataset, coeffs: T_Xarray, degree_dim: Hashable) -> Dataset:
1861-
...
1861+
1862+
# @overload
1863+
# def polyval(coord: Dataset, coeffs: T_Xarray, degree_dim: Hashable) -> Dataset:
1864+
# ...
18621865

18631866

1864-
def polyval(
1865-
coord: T_Xarray, coeffs: T_Xarray, degree_dim: Hashable = "degree"
1866-
) -> T_Xarray:
1867+
def polyval(coord: T_Xarray, coeffs: T_Xarray, degree_dim="degree") -> T_Xarray:
18671868
"""Evaluate a polynomial at specific values
18681869
18691870
Parameters

xarray/core/dask_array_compat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
try:
66
import dask.array as da
77
except ImportError:
8-
da = None
8+
da = None # type: ignore
99

1010

1111
def _validate_pad_output_shape(input_shape, pad_width, output_shape):

xarray/core/dataarray.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import numpy as np
1818
import pandas as pd
1919

20+
from ..backends.common import AbstractDataStore, ArrayWriter
2021
from ..coding.calendar_ops import convert_calendar, interp_calendar
2122
from ..coding.cftimeindex import CFTimeIndex
2223
from ..plot.plot import _PlotMethods
@@ -67,7 +68,7 @@
6768
try:
6869
from dask.delayed import Delayed
6970
except ImportError:
70-
Delayed = None
71+
Delayed = None # type: ignore
7172
try:
7273
from cdms2 import Variable as cdms2_Variable
7374
except ImportError:
@@ -2875,7 +2876,9 @@ def to_masked_array(self, copy: bool = True) -> np.ma.MaskedArray:
28752876
isnull = pd.isnull(values)
28762877
return np.ma.MaskedArray(data=values, mask=isnull, copy=copy)
28772878

2878-
def to_netcdf(self, *args, **kwargs) -> bytes | Delayed | None:
2879+
def to_netcdf(
2880+
self, *args, **kwargs
2881+
) -> tuple[ArrayWriter, AbstractDataStore] | bytes | Delayed | None:
28792882
"""Write DataArray contents to a netCDF file.
28802883
28812884
All parameters are passed directly to :py:meth:`xarray.Dataset.to_netcdf`.

xarray/core/dataset.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import xarray as xr
3434

35+
from ..backends.common import ArrayWriter
3536
from ..coding.calendar_ops import convert_calendar, interp_calendar
3637
from ..coding.cftimeindex import CFTimeIndex, _parse_array_of_cftime_strings
3738
from ..plot.dataset_plot import _Dataset_PlotMethods
@@ -110,7 +111,7 @@
110111
try:
111112
from dask.delayed import Delayed
112113
except ImportError:
113-
Delayed = None
114+
Delayed = None # type: ignore
114115

115116

116117
# list of attributes of pd.DatetimeIndex that are ndarrays of time info
@@ -1686,7 +1687,7 @@ def to_netcdf(
16861687
unlimited_dims: Iterable[Hashable] = None,
16871688
compute: bool = True,
16881689
invalid_netcdf: bool = False,
1689-
) -> bytes | Delayed | None:
1690+
) -> tuple[ArrayWriter, AbstractDataStore] | bytes | Delayed | None:
16901691
"""Write dataset contents to a netCDF file.
16911692
16921693
Parameters

xarray/core/duck_array_ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import dask.array as dask_array
3131
from dask.base import tokenize
3232
except ImportError:
33-
dask_array = None
33+
dask_array = None # type: ignore
3434

3535

3636
def _dask_or_eager_func(

xarray/core/nanops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from . import dask_array_compat
1313
except ImportError:
14-
dask_array = None
14+
dask_array = None # type: ignore[assignment]
1515
dask_array_compat = None # type: ignore[assignment]
1616

1717

xarray/core/types.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
try:
1717
from dask.array import Array as DaskArray
1818
except ImportError:
19-
DaskArray = np.ndarray
19+
DaskArray = np.ndarray # type: ignore
2020

2121

2222
T_Dataset = TypeVar("T_Dataset", bound="Dataset")

xarray/tests/test_computation.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import functools
24
import operator
35
import pickle
@@ -22,6 +24,7 @@
2224
unified_dim_sizes,
2325
)
2426
from xarray.core.pycompat import dask_version
27+
from xarray.core.types import T_Xarray
2528

2629
from . import has_dask, raise_if_dask_computes, requires_dask
2730

@@ -2009,14 +2012,14 @@ def test_where_attrs() -> None:
20092012
),
20102013
],
20112014
)
2012-
def test_polyval(use_dask, x, coeffs, expected) -> None:
2015+
def test_polyval(use_dask, x: T_Xarray, coeffs: T_Xarray, expected) -> None:
20132016
if use_dask:
20142017
if not has_dask:
20152018
pytest.skip("requires dask")
20162019
coeffs = coeffs.chunk({"degree": 2})
20172020
x = x.chunk({"x": 2})
20182021
with raise_if_dask_computes():
2019-
actual = xr.polyval(x, coeffs)
2022+
actual = xr.polyval(coord=x, coeffs=coeffs)
20202023
xr.testing.assert_allclose(actual, expected)
20212024

20222025

xarray/tests/test_testing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
try:
1111
from dask.array import from_array as dask_from_array
1212
except ImportError:
13-
dask_from_array = lambda x: x
13+
dask_from_array = lambda x: x # type: ignore
1414

1515
try:
1616
import pint

xarray/util/generate_ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def inplace():
210210
try:
211211
from dask.array import Array as DaskArray
212212
except ImportError:
213-
DaskArray = np.ndarray
213+
DaskArray = np.ndarray # type: ignore
214214
215215
# DatasetOpsMixin etc. are parent classes of Dataset etc.
216216
# Because of https://github.com/pydata/xarray/issues/5755, we redefine these. Generally

0 commit comments

Comments
 (0)