Skip to content
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

More annotations in Dataset #3112

Merged
merged 14 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 9 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ New functions/methods
(:issue:`3026`).
By `Julia Kent <https://github.com/jukent>`_.

- The xarray package is now discoverably by mypy (although typing hints
coverage is not complete yet). mypy users can now remove from their setup.cfg
the lines::

[mypy-xarray]
ignore_missing_imports = True

By `Guido Imperiale <https://github.com/crusaderky>`_
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's move this up to 0.12.4?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


Enhancements
~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@
tests_require=TESTS_REQUIRE,
url=URL,
packages=find_packages(),
package_data={'xarray': ['tests/data/*']})
package_data={'xarray': ['py.typed', 'tests/data/*']})
33 changes: 25 additions & 8 deletions xarray/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
import warnings
from collections import OrderedDict, defaultdict
from contextlib import suppress
from typing import Any, Mapping, Optional, Tuple
from typing import (
Any,
Dict,
Hashable,
Mapping,
Optional,
Tuple,
Union,
TYPE_CHECKING,
)

import numpy as np
import pandas as pd
Expand All @@ -13,6 +22,10 @@
from .utils import is_dict_like, is_full_slice
from .variable import IndexVariable, Variable

if TYPE_CHECKING:
from .dataarray import DataArray
from .dataset import Dataset


def _get_joiner(join):
if join == 'outer':
Expand Down Expand Up @@ -169,8 +182,8 @@ def deep_align(objects, join='inner', copy=True, indexes=None,

This function is not public API.
"""
from .dataarray import DataArray
from .dataset import Dataset
from .dataarray import DataArray # noqa: F811
from .dataset import Dataset # noqa: F811
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a workaround to a flake8 limitation https://gitlab.com/pycqa/flake8/issues/553


if indexes is None:
indexes = {}
Expand Down Expand Up @@ -222,7 +235,10 @@ def is_alignable(obj):
return out


def reindex_like_indexers(target, other):
def reindex_like_indexers(
target: Union['DataArray', 'Dataset'],
other: Union['DataArray', 'Dataset'],
) -> Dict[Hashable, pd.Index]:
"""Extract indexers to align target with other.

Not public API.
Expand All @@ -236,7 +252,8 @@ def reindex_like_indexers(target, other):

Returns
-------
Dict[Any, pandas.Index] providing indexes for reindex keyword arguments.
Dict[Hashable, pandas.Index] providing indexes for reindex keyword
arguments.

Raises
------
Expand Down Expand Up @@ -310,7 +327,7 @@ def reindex_variables(
new_indexes : OrderedDict
Dict of indexes associated with the reindexed variables.
"""
from .dataarray import DataArray
from .dataarray import DataArray # noqa: F811

# create variables for the new dataset
reindexed = OrderedDict() # type: OrderedDict[Any, Variable]
Expand Down Expand Up @@ -463,8 +480,8 @@ def broadcast(*args, exclude=None):
a (x, y) int64 1 1 2 2 3 3
b (x, y) int64 5 6 5 6 5 6
"""
from .dataarray import DataArray
from .dataset import Dataset
from .dataarray import DataArray # noqa: F811
from .dataset import Dataset # noqa: F811

if exclude is None:
exclude = set()
Expand Down
48 changes: 31 additions & 17 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,8 +1041,13 @@ def reindex_like(self, other: Union['DataArray', Dataset],
align
"""
indexers = reindex_like_indexers(self, other)
return self.reindex(method=method, tolerance=tolerance, copy=copy,
fill_value=fill_value, **indexers)
return self.reindex(
indexers=indexers,
method=method,
tolerance=tolerance,
copy=copy,
fill_value=fill_value,
)

def reindex(self, indexers: Optional[Mapping[Hashable, Any]] = None,
method: Optional[str] = None, tolerance=None,
Expand Down Expand Up @@ -1329,9 +1334,13 @@ def expand_dims(self, dim: Union[None, Hashable, Sequence[Hashable],
ds = self._to_temp_dataset().expand_dims(dim, axis)
return self._from_temp_dataset(ds)

def set_index(self, indexes: Optional[Mapping[Hashable, Any]] = None,
append: bool = False, inplace: Optional[bool] = None,
**indexes_kwargs: Any) -> Optional['DataArray']:
def set_index(
self,
indexes: Mapping[Hashable, Union[Hashable, Sequence[Hashable]]] = None,
append: bool = False,
inplace: bool = None,
**indexes_kwargs: Union[Hashable, Sequence[Hashable]]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm guessing this is fine because mypy passes - my original thought was whether this should be Optional[Mapping] given it was either None or a dict?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As of mypy 0.701, x: MyType = None and x: Optional[MyType] = None are the same.

from typing import Optional


def f1(x: int = None) -> None:
    pass

def f2(x: Optional[int] = None) -> None:
    pass

def f3(x: int = 1) -> None:
    pass

def f4(x: Optional[int] = 1) -> None:
    pass


f1()
f2()
f3()
f4()
f1(1)
f2(1)
f3(1)
f4(1)
f1(None)
f2(None)
f3(None)
f4(None)

mypy output:

test_mypy.py:27: error: Argument 1 to "f3" has incompatible type "None"; expected "int"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@max-sixty I did a generalised cleanup of all already existing cases; I find it's much more readable now

) -> Optional['DataArray']:
"""Set DataArray (multi-)indexes using one or more existing
coordinates.

Expand Down Expand Up @@ -1370,9 +1379,12 @@ def set_index(self, indexes: Optional[Mapping[Hashable, Any]] = None,
else:
return self._replace(coords=coords)

def reset_index(self, dims_or_levels: Union[Hashable, Sequence[Hashable]],
drop: bool = False, inplace: Optional[bool] = None
) -> Optional['DataArray']:
def reset_index(
self,
dims_or_levels: Union[Hashable, Sequence[Hashable]],
drop: bool = False,
inplace: bool = None
) -> Optional['DataArray']:
"""Reset the specified index(es) or multi-index level(s).

Parameters
Expand Down Expand Up @@ -1406,12 +1418,12 @@ def reset_index(self, dims_or_levels: Union[Hashable, Sequence[Hashable]],
else:
return self._replace(coords=coords)

def reorder_levels(self,
dim_order: Optional[
Mapping[Hashable, Sequence[int]]] = None,
inplace: Optional[bool] = None,
**dim_order_kwargs: Sequence[int]
) -> Optional['DataArray']:
def reorder_levels(
self,
dim_order: Mapping[Hashable, Sequence[int]] = None,
inplace: bool = None,
**dim_order_kwargs: Sequence[int]
) -> Optional['DataArray']:
"""Rearrange index levels using input order.

Parameters
Expand Down Expand Up @@ -1452,9 +1464,11 @@ def reorder_levels(self,
else:
return self._replace(coords=coords)

def stack(self, dimensions: Optional[
Mapping[Hashable, Sequence[Hashable]]] = None,
**dimensions_kwargs: Sequence[Hashable]) -> 'DataArray':
def stack(
self,
dimensions: Mapping[Hashable, Sequence[Hashable]] = None,
**dimensions_kwargs: Sequence[Hashable]
) -> 'DataArray':
"""
Stack any number of existing dimensions into a single new dimension.

Expand Down
Loading