diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 26bf4803ef6..1d384e58a3c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,7 +16,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.761 # Must match ci/requirements/*.yml + rev: v0.780 # Must match ci/requirements/*.yml hooks: - id: mypy # run this occasionally, ref discussion https://github.com/pydata/xarray/pull/3194 diff --git a/ci/requirements/py38.yml b/ci/requirements/py38.yml index 24602f884e9..7dff3a1bd97 100644 --- a/ci/requirements/py38.yml +++ b/ci/requirements/py38.yml @@ -22,7 +22,7 @@ dependencies: - isort - lxml # Optional dep of pydap - matplotlib - - mypy=0.761 # Must match .pre-commit-config.yaml + - mypy=0.780 # Must match .pre-commit-config.yaml - nc-time-axis - netcdf4 - numba diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 2958cad89b2..a024324bcb1 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -27,6 +27,7 @@ TypeVar, Union, cast, + overload, ) import numpy as np @@ -1241,13 +1242,25 @@ def loc(self) -> _LocIndexer: """ return _LocIndexer(self) - def __getitem__(self, key: Any) -> "Union[DataArray, Dataset]": + # FIXME https://github.com/python/mypy/issues/7328 + @overload + def __getitem__(self, key: Mapping) -> "Dataset": # type: ignore + ... + + @overload + def __getitem__(self, key: Hashable) -> "DataArray": # type: ignore + ... + + @overload + def __getitem__(self, key: Any) -> "Dataset": + ... + + def __getitem__(self, key): """Access variables or coordinates this dataset as a :py:class:`~xarray.DataArray`. Indexing with a list of names will return a new ``Dataset`` object. """ - # TODO(shoyer): type this properly: https://github.com/python/mypy/issues/7328 if utils.is_dict_like(key): return self.isel(**cast(Mapping, key)) diff --git a/xarray/core/weighted.py b/xarray/core/weighted.py index 21ed06ea85f..fa143342c06 100644 --- a/xarray/core/weighted.py +++ b/xarray/core/weighted.py @@ -72,11 +72,11 @@ class Weighted: def __init__(self, obj: "DataArray", weights: "DataArray") -> None: ... - @overload # noqa: F811 - def __init__(self, obj: "Dataset", weights: "DataArray") -> None: # noqa: F811 + @overload + def __init__(self, obj: "Dataset", weights: "DataArray") -> None: ... - def __init__(self, obj, weights): # noqa: F811 + def __init__(self, obj, weights): """ Create a Weighted object