Skip to content

Commit

Permalink
Upgrade mypy to 1.11 (pydata#9417)
Browse files Browse the repository at this point in the history
* Upgrade mypy to 1.11

This does a few things:
- Upgrades mypy
- Adds lots of ignores for mpl & pandas type issues in tests -- not as good as fixing them, but better than staying on mypy 1.8
- Removes some old ignores
- Adds a couple of ignores with notes where it's our issue and I'm not sure what's going on
- Starts using the `EllipsisType` in a couple of places

Note that I used a tool to mass-add ignores, I didn't add them all manually
  • Loading branch information
max-sixty authored and hollymandel committed Sep 23, 2024
1 parent 95d999d commit c916fe7
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 109 deletions.
26 changes: 11 additions & 15 deletions .github/workflows/ci-additional.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ jobs:
python xarray/util/print_versions.py
- name: Install mypy
run: |
python -m pip install "mypy<1.9" --force-reinstall
python -m pip install "mypy" --force-reinstall
- name: Run mypy
run: |
Expand Down Expand Up @@ -177,7 +177,7 @@ jobs:
python xarray/util/print_versions.py
- name: Install mypy
run: |
python -m pip install "mypy<1.9" --force-reinstall
python -m pip install "mypy" --force-reinstall
- name: Run mypy
run: |
Expand All @@ -187,22 +187,20 @@ jobs:
uses: codecov/codecov-action@v4.5.0
with:
file: mypy_report/cobertura.xml
flags: mypy39
flags: mypy-min
env_vars: PYTHON_VERSION
name: codecov-umbrella
fail_ci_if_error: false



pyright:
name: Pyright
runs-on: "ubuntu-latest"
needs: detect-ci-trigger
if: |
always()
&& (
contains( github.event.pull_request.labels.*.name, 'run-pyright')
)
always()
&& (
contains( github.event.pull_request.labels.*.name, 'run-pyright')
)
defaults:
run:
shell: bash -l {0}
Expand Down Expand Up @@ -258,10 +256,10 @@ jobs:
runs-on: "ubuntu-latest"
needs: detect-ci-trigger
if: |
always()
&& (
contains( github.event.pull_request.labels.*.name, 'run-pyright')
)
always()
&& (
contains( github.event.pull_request.labels.*.name, 'run-pyright')
)
defaults:
run:
shell: bash -l {0}
Expand Down Expand Up @@ -312,8 +310,6 @@ jobs:
name: codecov-umbrella
fail_ci_if_error: false



min-version-policy:
name: Minimum Version Policy
runs-on: "ubuntu-latest"
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/accessor_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ def normalize(
normalized : same type as values
"""
return self._apply(func=lambda x: normalize(form, x))
return self._apply(func=lambda x: normalize(form, x)) # type: ignore[arg-type]

def isalnum(self) -> T_DataArray:
"""
Expand Down
3 changes: 2 additions & 1 deletion xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from functools import partial
from os import PathLike
from types import EllipsisType
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -2841,7 +2842,7 @@ def stack(
dim: Mapping[Any, Sequence[Hashable]] | None = None,
create_index: bool | None = True,
index_cls: type[Index] = PandasMultiIndex,
**dim_kwargs: Sequence[Hashable],
**dim_kwargs: Sequence[Hashable | EllipsisType],
) -> Self:
"""
Stack any number of existing dimensions into a single new dimension.
Expand Down
5 changes: 3 additions & 2 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from numbers import Number
from operator import methodcaller
from os import PathLike
from types import EllipsisType
from typing import IO, TYPE_CHECKING, Any, Generic, Literal, cast, overload

import numpy as np
Expand Down Expand Up @@ -5364,10 +5365,10 @@ def _stack_once(
@partial(deprecate_dims, old_name="dimensions")
def stack(
self,
dim: Mapping[Any, Sequence[Hashable | ellipsis]] | None = None,
dim: Mapping[Any, Sequence[Hashable | EllipsisType]] | None = None,
create_index: bool | None = True,
index_cls: type[Index] = PandasMultiIndex,
**dim_kwargs: Sequence[Hashable | ellipsis],
**dim_kwargs: Sequence[Hashable | EllipsisType],
) -> Self:
"""
Stack any number of existing dimensions into a single new dimension.
Expand Down
3 changes: 1 addition & 2 deletions xarray/core/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ def __getitem__(self, key: Mapping) -> Dataset: # type: ignore[overload-overlap
...

@overload
def __getitem__(self, key: Hashable) -> DataArray: # type: ignore[overload-overlap]
...
def __getitem__(self, key: Hashable) -> DataArray: ...

# See: https://github.com/pydata/xarray/issues/8855
@overload
Expand Down
8 changes: 5 additions & 3 deletions xarray/core/weighted.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def _weight_check(w):
if is_duck_dask_array(weights.data):
# assign to copy - else the check is not triggered
weights = weights.copy(
data=weights.data.map_blocks(_weight_check, dtype=weights.dtype),
data=weights.data.map_blocks(_weight_check, dtype=weights.dtype), # type: ignore[call-arg, arg-type]
deep=False,
)

Expand Down Expand Up @@ -264,7 +264,9 @@ def _sum_of_squares(

demeaned = da - da.weighted(self.weights).mean(dim=dim)

return self._reduce((demeaned**2), self.weights, dim=dim, skipna=skipna)
# TODO: unsure why mypy complains about these being DataArray return types
# rather than T_DataArray?
return self._reduce((demeaned**2), self.weights, dim=dim, skipna=skipna) # type: ignore[return-value]

def _weighted_sum(
self,
Expand All @@ -274,7 +276,7 @@ def _weighted_sum(
) -> T_DataArray:
"""Reduce a DataArray by a weighted ``sum`` along some dimension(s)."""

return self._reduce(da, self.weights, dim=dim, skipna=skipna)
return self._reduce(da, self.weights, dim=dim, skipna=skipna) # type: ignore[return-value]

def _weighted_mean(
self,
Expand Down
2 changes: 1 addition & 1 deletion xarray/plot/dataset_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ def wrapper(dataset_plotfunc: F) -> F:
dataset_plotfunc.__doc__ = ds_doc
return dataset_plotfunc

return wrapper
return wrapper # type: ignore[return-value]


def _normalize_args(
Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ def test_roundtrip_cftime_datetime_data(self) -> None:
assert actual.t.encoding["calendar"] == expected_calendar

def test_roundtrip_timedelta_data(self) -> None:
time_deltas = pd.to_timedelta(["1h", "2h", "NaT"])
time_deltas = pd.to_timedelta(["1h", "2h", "NaT"]) # type: ignore[arg-type, unused-ignore]
expected = Dataset({"td": ("td", time_deltas), "td0": time_deltas[0]})
with self.roundtrip(expected) as actual:
assert_identical(expected, actual)
Expand Down Expand Up @@ -2204,7 +2204,7 @@ def create_store(self):
store_target, mode="w", **self.version_kwargs
)

def save(self, dataset, store_target, **kwargs):
def save(self, dataset, store_target, **kwargs): # type: ignore[override]
return dataset.to_zarr(store=store_target, **kwargs, **self.version_kwargs)

@contextlib.contextmanager
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_backends_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def open_dataset(
class PassThroughBackendEntrypoint(xr.backends.BackendEntrypoint):
"""Access an object passed to the `open_dataset` method."""

def open_dataset(self, dataset, *, drop_variables=None):
def open_dataset(self, dataset, *, drop_variables=None): # type: ignore[override]
"""Return the first argument."""
return dataset

Expand Down
8 changes: 4 additions & 4 deletions xarray/tests/test_coding_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,10 @@ def test_cf_timedelta_2d() -> None:
@pytest.mark.parametrize(
["deltas", "expected"],
[
(pd.to_timedelta(["1 day", "2 days"]), "days"),
(pd.to_timedelta(["1h", "1 day 1 hour"]), "hours"),
(pd.to_timedelta(["1m", "2m", np.nan]), "minutes"),
(pd.to_timedelta(["1m3s", "1m4s"]), "seconds"),
(pd.to_timedelta(["1 day", "2 days"]), "days"), # type: ignore[arg-type, unused-ignore]
(pd.to_timedelta(["1 day", "2 days"]), "days"), # type: ignore[arg-type, unused-ignore]
(pd.to_timedelta(["1 day", "2 days"]), "days"), # type: ignore[arg-type, unused-ignore]
(pd.to_timedelta(["1 day", "2 days"]), "days"), # type: ignore[arg-type, unused-ignore]
],
)
def test_infer_timedelta_units(deltas, expected) -> None:
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_incompatible_attributes(self) -> None:
Variable(
["t"], pd.date_range("2000-01-01", periods=3), {"units": "foobar"}
),
Variable(["t"], pd.to_timedelta(["1 day"]), {"units": "foobar"}),
Variable(["t"], pd.to_timedelta(["1 day"]), {"units": "foobar"}), # type: ignore[arg-type, unused-ignore]
Variable(["t"], [0, 1, 2], {"add_offset": 0}, {"add_offset": 2}),
Variable(["t"], [0, 1, 2], {"_FillValue": 0}, {"_FillValue": 2}),
]
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7159,7 +7159,7 @@ def test_result_as_expected(self) -> None:
def test_error_on_ellipsis_without_list(self) -> None:
da = DataArray([[1, 2], [1, 2]], dims=("x", "y"))
with pytest.raises(ValueError):
da.stack(flat=...)
da.stack(flat=...) # type: ignore


def test_nD_coord_dataarray() -> None:
Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ def test_format_items(self) -> None:
np.arange(4) * np.timedelta64(500, "ms"),
"00:00:00 00:00:00.500000 00:00:01 00:00:01.500000",
),
(pd.to_timedelta(["NaT", "0s", "1s", "NaT"]), "NaT 00:00:00 00:00:01 NaT"),
(pd.to_timedelta(["NaT", "0s", "1s", "NaT"]), "NaT 00:00:00 00:00:01 NaT"), # type: ignore[arg-type, unused-ignore]
(
pd.to_timedelta(["1 day 1 hour", "1 day", "0 hours"]),
pd.to_timedelta(["1 day 1 hour", "1 day", "0 hours"]), # type: ignore[arg-type, unused-ignore]
"1 days 01:00:00 1 days 00:00:00 0 days 00:00:00",
),
([1, 2, 3], "1 2 3"),
Expand Down
Loading

0 comments on commit c916fe7

Please sign in to comment.