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

Typing for DataArray/Dataset #2929

Merged
merged 35 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
57ddd54
typing for DataArray/Dataset (incomplete)
Apr 29, 2019
2db5fa3
Merge branch 'master'
May 8, 2019
5b380ab
Finished dataarray.py
May 8, 2019
4893640
Bugfixes
May 8, 2019
a784bc3
Bugfixes
May 8, 2019
134bd5b
trivial
May 8, 2019
5dcbfce
Merge branch 'annotations' of https://github.com/crusaderky/xarray in…
May 8, 2019
8746dc2
Dataset (partial)
May 8, 2019
db35082
Bugfixes
May 9, 2019
4cff627
Minor tweaks
May 9, 2019
0b748bf
Merge branch 'master' into annotations
May 10, 2019
5894ea2
final changes to dataset & dataarray for mypy
max-sixty Jun 14, 2019
922cf2c
Merge branch 'master' into annotations
max-sixty Jun 14, 2019
8857765
mypy str conflict
max-sixty Jun 14, 2019
603005c
remove temp ptw setting
max-sixty Jun 15, 2019
d95dc2a
Merge remote-tracking branch 'max-sixty/annotations' into annotations
crusaderky Jun 16, 2019
400bc93
Merge remote-tracking branch 'upstream/master' into annotations
crusaderky Jun 16, 2019
e601bfe
OrderedDict-related fixes
crusaderky Jun 16, 2019
c98ed86
Merge branch 'master' into annotations
max-sixty Jun 20, 2019
69d173c
Merge branch 'master' into annotations
max-sixty Jun 20, 2019
a231838
Merge commit 'refs/pull/2929/head' of https://github.com/pydata/xarra…
max-sixty Jun 21, 2019
eee935a
fix tests re dict
max-sixty Jun 21, 2019
738ba5f
fix mypy re dict
max-sixty Jun 21, 2019
ea1e49b
merge issue re encoding kwarg
max-sixty Jun 21, 2019
8e38802
weird python35 syntax error
max-sixty Jun 21, 2019
c83d40b
Merge branch 'master' into annotations
max-sixty Jun 24, 2019
0bb3472
Hashable -> Any for Dicts
max-sixty Jun 25, 2019
f9864ba
DataArray names as Hashable
max-sixty Jun 25, 2019
a9b748b
resolve some __default vs None issues
max-sixty Jun 25, 2019
2bc87eb
whats-new
max-sixty Jun 25, 2019
d2cb1e1
just specify Tuple at the moment
max-sixty Jun 25, 2019
e5e381b
remove unneeded cast, make arg formatting more black-like
max-sixty Jun 25, 2019
9b75e6a
adjust init args based on @shoyer review
max-sixty Jun 25, 2019
7bd558f
add Tuple[Tuple] to dataset init types
max-sixty Jun 25, 2019
cbe0e7b
tuples not all the way down
max-sixty Jun 25, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pip-log.txt
.tox
nosetests.xml
.cache
.dmypy.json
.mypy_cache
.ropeproject/
.tags*
Expand Down
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ Enhancements
that allows ignoring errors if a passed label or dimension is not in the dataset
(:issue:`2994`).
By `Andrew Ross <https://github.com/andrew-c-ross>`_.
- Argument and return types are added to most methods on ``DataArray`` and ``Dataset``,
allowing static type checking both within xarray and external libraries.
Type checking with ``mypy`` is enabled in CI (though not required yet).
By `Guido Imperiale <https://github.com/crusaderky>`_ and `Maximilian Roos <https://github.com/max-sixty>`_.


Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ tag_prefix = v
parentdir_prefix = xarray-

[aliases]
test = pytest
test = pytest
14 changes: 7 additions & 7 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from . import dtypes, duck_array_ops, formatting, ops
from .arithmetic import SupportsArithmetic
from .npcompat import DTypeLike
from .options import _get_keep_attrs
from .pycompat import dask_array_type
from .rolling_exp import RollingExp
Expand Down Expand Up @@ -100,8 +101,7 @@ def __int__(self: Any) -> int:
def __complex__(self: Any) -> complex:
return complex(self.values)

def __array__(self: Any, dtype: Union[str, np.dtype, None] = None
) -> np.ndarray:
def __array__(self: Any, dtype: Optional[DTypeLike] = None) -> np.ndarray:
return np.asarray(self.values, dtype=dtype)

def __repr__(self) -> str:
Expand Down Expand Up @@ -997,7 +997,7 @@ def __exit__(self, exc_type, exc_value, traceback) -> None:
self.close()


def full_like(other, fill_value, dtype: Union[str, np.dtype, None] = None):
def full_like(other, fill_value, dtype: Optional[DTypeLike] = None):
"""Return a new object with the same shape and type as a given object.

Parameters
Expand Down Expand Up @@ -1038,7 +1038,7 @@ def full_like(other, fill_value, dtype: Union[str, np.dtype, None] = None):


def _full_like_variable(other, fill_value,
dtype: Union[str, np.dtype, None] = None):
dtype: Optional[DTypeLike] = None):
"""Inner function of full_like, where other must be a variable
"""
from .variable import Variable
Expand All @@ -1055,19 +1055,19 @@ def _full_like_variable(other, fill_value,
return Variable(dims=other.dims, data=data, attrs=other.attrs)


def zeros_like(other, dtype: Union[str, np.dtype, None] = None):
def zeros_like(other, dtype: Optional[DTypeLike] = None):
"""Shorthand for full_like(other, 0, dtype)
"""
return full_like(other, 0, dtype)


def ones_like(other, dtype: Union[str, np.dtype, None] = None):
def ones_like(other, dtype: Optional[DTypeLike] = None):
"""Shorthand for full_like(other, 1, dtype)
"""
return full_like(other, 1, dtype)


def is_np_datetime_like(dtype: Union[str, np.dtype]) -> bool:
def is_np_datetime_like(dtype: DTypeLike) -> bool:
"""Check if a dtype is a subclass of the numpy datetime types
"""
return (np.issubdtype(dtype, np.datetime64) or
Expand Down
1 change: 0 additions & 1 deletion xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import functools
import itertools
import operator
import typing
from collections import Counter, OrderedDict
from distutils.version import LooseVersion
from typing import (
Expand Down
15 changes: 10 additions & 5 deletions xarray/core/coordinates.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections.abc
from collections import OrderedDict
from contextlib import contextmanager
from typing import Hashable, Iterable, Iterator, Union

import pandas as pd

Expand All @@ -9,6 +10,11 @@
expand_and_merge_variables, merge_coords, merge_coords_for_inplace_math)
from .utils import Frozen, ReprObject, either_dict_or_kwargs
from .variable import Variable
from .pycompat import TYPE_CHECKING

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

# Used as the key corresponding to a DataArray's variable when converting
# arbitrary DataArray objects to datasets
Expand Down Expand Up @@ -258,21 +264,20 @@ def _ipython_key_completions_(self):
return self._data._ipython_key_completions_()


class LevelCoordinatesSource:
class LevelCoordinatesSource(Iterable[Hashable]):
"""Iterator for MultiIndex level coordinates.

Used for attribute style lookup with AttrAccessMixin. Not returned directly
by any public methods.
"""

def __init__(self, data_object):
def __init__(self, data_object: 'Union[DataArray, Dataset]'):
self._data = data_object

def __getitem__(self, key):
# not necessary -- everything here can already be found in coords.
raise KeyError
raise KeyError()

def __iter__(self):
def __iter__(self) -> Iterator[Hashable]:
return iter(self._data._level_coords)


Expand Down
Loading