Skip to content

Improve typehints of xr.Dataset.__getitem__ #4144

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 7 commits into from
Jun 15, 2020
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements/py38.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
TypeVar,
Union,
cast,
overload,
)

import numpy as np
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is fixed now and can be removed? Or perhaps we more it below above the third @overload and add a comment that Any means list?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's fixed. Specifically, mypy can't deal in the signature with an overload of Mapping and Hashable. Curiously however, once you add #type: ignore to the overloaded signature, the actual type inspection works just fine (see my test script above).

@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))

Expand Down
6 changes: 3 additions & 3 deletions xarray/core/weighted.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down