From f38cba5966bb5a2d40a1989f7605916a313fa0c6 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 12 Apr 2023 23:07:22 +0200 Subject: [PATCH 01/12] test newest mypy --- .github/workflows/ci-additional.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci-additional.yaml b/.github/workflows/ci-additional.yaml index 6f069af5da6..47fc4e63f34 100644 --- a/.github/workflows/ci-additional.yaml +++ b/.github/workflows/ci-additional.yaml @@ -119,7 +119,7 @@ jobs: python xarray/util/print_versions.py - name: Install mypy run: | - python -m pip install 'mypy<0.990' + python -m pip install 'mypy' - name: Run mypy run: | @@ -173,7 +173,7 @@ jobs: python xarray/util/print_versions.py - name: Install mypy run: | - python -m pip install 'mypy<0.990' + python -m pip install 'mypy' - name: Run mypy run: | From 701dd110acda6994a617fd7770bc38f917403015 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 12 Apr 2023 23:08:15 +0200 Subject: [PATCH 02/12] Update ci-additional.yaml --- .github/workflows/ci-additional.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci-additional.yaml b/.github/workflows/ci-additional.yaml index 47fc4e63f34..41dbd89e1a0 100644 --- a/.github/workflows/ci-additional.yaml +++ b/.github/workflows/ci-additional.yaml @@ -119,7 +119,7 @@ jobs: python xarray/util/print_versions.py - name: Install mypy run: | - python -m pip install 'mypy' + python -m pip install mypy - name: Run mypy run: | @@ -173,7 +173,7 @@ jobs: python xarray/util/print_versions.py - name: Install mypy run: | - python -m pip install 'mypy' + python -m pip install mypy - name: Run mypy run: | From 7f7893c92726d3b8daaadb1392ec81b60ae44713 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 12 Apr 2023 23:59:02 +0200 Subject: [PATCH 03/12] remove ignores --- xarray/core/combine.py | 2 +- xarray/core/rolling.py | 4 ++-- xarray/core/utils.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/xarray/core/combine.py b/xarray/core/combine.py index 946f71e5d28..39eb9a07668 100644 --- a/xarray/core/combine.py +++ b/xarray/core/combine.py @@ -371,7 +371,7 @@ def _nested_combine( # Define type for arbitrarily-nested list of lists recursively # Currently mypy cannot handle this but other linters can (https://stackoverflow.com/a/53845083/3154101) -DATASET_HYPERCUBE = Union[Dataset, Iterable["DATASET_HYPERCUBE"]] # type: ignore[misc] +DATASET_HYPERCUBE = Union[Dataset, Iterable["DATASET_HYPERCUBE"]] def combine_nested( diff --git a/xarray/core/rolling.py b/xarray/core/rolling.py index 8b9f31bfdfd..7eb4e9c7687 100644 --- a/xarray/core/rolling.py +++ b/xarray/core/rolling.py @@ -376,7 +376,7 @@ def _construct( window_dim = {d: window_dim_kwargs[str(d)] for d in self.dim} window_dims = self._mapping_to_list( - window_dim, allow_default=False, allow_allsame=False # type: ignore[arg-type] # https://github.com/python/mypy/issues/12506 + window_dim, allow_default=False, allow_allsame=False ) strides = self._mapping_to_list(stride, default=1) @@ -753,7 +753,7 @@ def construct( window_dim = {d: window_dim_kwargs[str(d)] for d in self.dim} window_dims = self._mapping_to_list( - window_dim, allow_default=False, allow_allsame=False # type: ignore[arg-type] # https://github.com/python/mypy/issues/12506 + window_dim, allow_default=False, allow_allsame=False ) strides = self._mapping_to_list(stride, default=1) diff --git a/xarray/core/utils.py b/xarray/core/utils.py index 08625fe7d95..1c90a2410f2 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -113,7 +113,7 @@ def get_valid_numpy_dtype(array: np.ndarray | pd.Index): dtype = np.dtype("O") elif hasattr(array, "categories"): # category isn't a real numpy dtype - dtype = array.categories.dtype # type: ignore[union-attr] + dtype = array.categories.dtype elif not is_valid_numpy_dtype(array.dtype): dtype = np.dtype("O") else: From 629f6dec91408af1ac1d3923ac15c22267edc79e Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 12 Apr 2023 23:59:23 +0200 Subject: [PATCH 04/12] add typing --- xarray/tests/test_concat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_concat.py b/xarray/tests/test_concat.py index f60308f8863..a170c79ecda 100644 --- a/xarray/tests/test_concat.py +++ b/xarray/tests/test_concat.py @@ -537,7 +537,7 @@ def test_concat_data_vars_typing(self) -> None: actual = concat(objs, dim="x", data_vars="minimal") assert_identical(data, actual) - def test_concat_data_vars(self): + def test_concat_data_vars(self) -> None: # TODO: annotating this func fails data = Dataset({"foo": ("x", np.random.randn(10))}) objs: list[Dataset] = [data.isel(x=slice(5)), data.isel(x=slice(5, None))] From e5a49877956cff2c473a895808df968735399fcf Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 12 Apr 2023 23:59:40 +0200 Subject: [PATCH 05/12] Use ClassVar --- xarray/tests/test_variable.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 2f571eb9a83..b84a8202bc5 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -4,6 +4,7 @@ from copy import copy, deepcopy from datetime import datetime, timedelta from textwrap import dedent +from typing import ClassVar import numpy as np import pandas as pd @@ -62,7 +63,7 @@ def var(): class VariableSubclassobjects: - cls: staticmethod[Variable] + cls: ClassVar[staticmethod[Variable]] def test_properties(self): data = 0.5 * np.arange(10) From 403adfdfd2ca1ccedb952efe50cc237e51f873cd Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 13 Apr 2023 19:25:02 +0200 Subject: [PATCH 06/12] Generalize data_vars typing concat. --- xarray/core/concat.py | 16 +++++++++------- xarray/tests/test_concat.py | 1 - 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/xarray/core/concat.py b/xarray/core/concat.py index f092911948f..6960345b15f 100644 --- a/xarray/core/concat.py +++ b/xarray/core/concat.py @@ -1,7 +1,7 @@ from __future__ import annotations from collections.abc import Hashable, Iterable -from typing import TYPE_CHECKING, Any, cast, overload +from typing import TYPE_CHECKING, Any, cast, overload, Union import pandas as pd @@ -27,12 +27,14 @@ JoinOptions, ) + T_DataVars = Union[ConcatOptions, Iterable[Hashable]] + @overload def concat( objs: Iterable[T_Dataset], dim: Hashable | T_DataArray | pd.Index, - data_vars: ConcatOptions | list[Hashable] = "all", + data_vars: T_DataVars = "all", coords: ConcatOptions | list[Hashable] = "different", compat: CompatOptions = "equals", positions: Iterable[Iterable[int]] | None = None, @@ -47,7 +49,7 @@ def concat( def concat( objs: Iterable[T_DataArray], dim: Hashable | T_DataArray | pd.Index, - data_vars: ConcatOptions | list[Hashable] = "all", + data_vars: T_DataVars = "all", coords: ConcatOptions | list[Hashable] = "different", compat: CompatOptions = "equals", positions: Iterable[Iterable[int]] | None = None, @@ -61,7 +63,7 @@ def concat( def concat( objs, dim, - data_vars="all", + data_vars: T_DataVars = "all", coords="different", compat: CompatOptions = "equals", positions=None, @@ -291,7 +293,7 @@ def _calc_concat_dim_index( return dim, index -def _calc_concat_over(datasets, dim, dim_names, data_vars, coords, compat): +def _calc_concat_over(datasets, dim, dim_names, data_vars: T_DataVars, coords, compat): """ Determine which dataset variables need to be concatenated in the result, """ @@ -445,7 +447,7 @@ def _parse_datasets( def _dataset_concat( datasets: list[T_Dataset], dim: str | T_DataArray | pd.Index, - data_vars: str | list[str], + data_vars: T_DataVars, coords: str | list[str], compat: CompatOptions, positions: Iterable[Iterable[int]] | None, @@ -665,7 +667,7 @@ def get_indexes(name): def _dataarray_concat( arrays: Iterable[T_DataArray], dim: str | T_DataArray | pd.Index, - data_vars: str | list[str], + data_vars: T_DataVars, coords: str | list[str], compat: CompatOptions, positions: Iterable[Iterable[int]] | None, diff --git a/xarray/tests/test_concat.py b/xarray/tests/test_concat.py index a170c79ecda..9e8fb127ae2 100644 --- a/xarray/tests/test_concat.py +++ b/xarray/tests/test_concat.py @@ -538,7 +538,6 @@ def test_concat_data_vars_typing(self) -> None: assert_identical(data, actual) def test_concat_data_vars(self) -> None: - # TODO: annotating this func fails data = Dataset({"foo": ("x", np.random.randn(10))}) objs: list[Dataset] = [data.isel(x=slice(5)), data.isel(x=slice(5, None))] for data_vars in ["minimal", "different", "all", [], ["foo"]]: From 4a880c5d7e6a4ab7b3c71d2bbef1c8dd7af6a2de Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 13 Apr 2023 17:25:45 +0000 Subject: [PATCH 07/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/core/concat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/concat.py b/xarray/core/concat.py index 6960345b15f..dcf2a23d311 100644 --- a/xarray/core/concat.py +++ b/xarray/core/concat.py @@ -1,7 +1,7 @@ from __future__ import annotations from collections.abc import Hashable, Iterable -from typing import TYPE_CHECKING, Any, cast, overload, Union +from typing import TYPE_CHECKING, Any, Union, cast, overload import pandas as pd From 13b356ead5ef587a86df421f0735a1e024c989aa Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 13 Apr 2023 20:23:18 +0200 Subject: [PATCH 08/12] Use a normal method to retrieve a type of Variable --- xarray/tests/test_variable.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index b84a8202bc5..fdf9bc5f945 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -4,7 +4,9 @@ from copy import copy, deepcopy from datetime import datetime, timedelta from textwrap import dedent -from typing import ClassVar +from typing import ClassVar, Callable +from abc import ABC, abstractmethod + import numpy as np import pandas as pd @@ -62,8 +64,10 @@ def var(): return Variable(dims=list("xyz"), data=np.random.rand(3, 4, 5)) -class VariableSubclassobjects: - cls: ClassVar[staticmethod[Variable]] +class VariableSubclassobjects(ABC): + @abstractmethod + def cls(self, *args, **kwargs) -> Variable: + raise NotImplementedError def test_properties(self): data = 0.5 * np.arange(10) @@ -1049,9 +1053,9 @@ def test_rolling_window_errors(self, dim, window, window_dim, center): center=center, ) - class TestVariable(VariableSubclassobjects): - cls = staticmethod(Variable) + def cls(self, *args, **kwargs) -> Variable: + return Variable(*args, **kwargs) @pytest.fixture(autouse=True) def setup(self): @@ -2219,13 +2223,11 @@ def test_coarsen_keep_attrs(self, operation="mean"): assert new.attrs == _attrs -def _init_dask_variable(*args, **kwargs): - return Variable(*args, **kwargs).chunk() - @requires_dask class TestVariableWithDask(VariableSubclassobjects): - cls = staticmethod(_init_dask_variable) + def cls(self, *args, **kwargs) -> Variable: + return Variable(*args, **kwargs).chunk() def test_chunk(self): unblocked = Variable(["dim_0", "dim_1"], np.ones((3, 4))) @@ -2337,7 +2339,8 @@ def test_as_sparse(self): class TestIndexVariable(VariableSubclassobjects): - cls = staticmethod(IndexVariable) + def cls(self, *args, **kwargs) -> IndexVariable: + return IndexVariable(*args, **kwargs) def test_init(self): with pytest.raises(ValueError, match=r"must be 1-dimensional"): From 718001958f6cd8c5c9ad93eb6913d586e2ee5174 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 13 Apr 2023 18:24:13 +0000 Subject: [PATCH 09/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/tests/test_variable.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index fdf9bc5f945..8b984e96e5d 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -1,12 +1,10 @@ from __future__ import annotations import warnings +from abc import ABC, abstractmethod from copy import copy, deepcopy from datetime import datetime, timedelta from textwrap import dedent -from typing import ClassVar, Callable -from abc import ABC, abstractmethod - import numpy as np import pandas as pd @@ -1053,6 +1051,7 @@ def test_rolling_window_errors(self, dim, window, window_dim, center): center=center, ) + class TestVariable(VariableSubclassobjects): def cls(self, *args, **kwargs) -> Variable: return Variable(*args, **kwargs) @@ -2223,7 +2222,6 @@ def test_coarsen_keep_attrs(self, operation="mean"): assert new.attrs == _attrs - @requires_dask class TestVariableWithDask(VariableSubclassobjects): def cls(self, *args, **kwargs) -> Variable: From 0c2c0b79360d4a0a32c1b5058ce54dae29b399d4 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 13 Apr 2023 21:12:32 +0200 Subject: [PATCH 10/12] ignore plotfunc error --- xarray/tests/test_plot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 02f7f4b9be2..8cacf719a86 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -2042,7 +2042,7 @@ def test_seaborn_palette_as_cmap(self) -> None: def test_convenient_facetgrid(self) -> None: a = easy_array((10, 15, 4)) d = DataArray(a, dims=["y", "x", "z"]) - g = self.plotfunc(d, x="x", y="y", col="z", col_wrap=2) + g = self.plotfunc(d, x="x", y="y", col="z", col_wrap=2) # type: ignore[arg-type] # https://github.com/python/mypy/issues/15015 assert_array_equal(g.axs.shape, [2, 2]) for (y, x), ax in np.ndenumerate(g.axs): @@ -2051,7 +2051,7 @@ def test_convenient_facetgrid(self) -> None: assert "x" == ax.get_xlabel() # Inferring labels - g = self.plotfunc(d, col="z", col_wrap=2) + g = self.plotfunc(d, col="z", col_wrap=2) # type: ignore[arg-type] # https://github.com/python/mypy/issues/15015 assert_array_equal(g.axs.shape, [2, 2]) for (y, x), ax in np.ndenumerate(g.axs): assert ax.has_data() From ecba29d51c31f27efc187ad0915435434925d897 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 14 Apr 2023 22:28:42 +0200 Subject: [PATCH 11/12] force reinstall --- .github/workflows/ci-additional.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci-additional.yaml b/.github/workflows/ci-additional.yaml index 41dbd89e1a0..d9956570991 100644 --- a/.github/workflows/ci-additional.yaml +++ b/.github/workflows/ci-additional.yaml @@ -119,7 +119,7 @@ jobs: python xarray/util/print_versions.py - name: Install mypy run: | - python -m pip install mypy + python -m pip install mypy --force-reinstall - name: Run mypy run: | @@ -173,7 +173,7 @@ jobs: python xarray/util/print_versions.py - name: Install mypy run: | - python -m pip install mypy + python -m pip install mypy --force-reinstall - name: Run mypy run: | From 61c23ac9bd678e43720ad0fae48a45463ee04302 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 15 Apr 2023 08:08:26 +0200 Subject: [PATCH 12/12] remove outdated comments --- xarray/core/combine.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/xarray/core/combine.py b/xarray/core/combine.py index 39eb9a07668..8106c295f5a 100644 --- a/xarray/core/combine.py +++ b/xarray/core/combine.py @@ -369,8 +369,7 @@ def _nested_combine( return combined -# Define type for arbitrarily-nested list of lists recursively -# Currently mypy cannot handle this but other linters can (https://stackoverflow.com/a/53845083/3154101) +# Define type for arbitrarily-nested list of lists recursively: DATASET_HYPERCUBE = Union[Dataset, Iterable["DATASET_HYPERCUBE"]]