Skip to content

Commit

Permalink
Fix mypy errors (#2753)
Browse files Browse the repository at this point in the history
Apparently I wasn't paying attention in my last PR :)
  • Loading branch information
shoyer authored Feb 8, 2019
1 parent 8a1a8a1 commit 6d20766
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 30 deletions.
2 changes: 1 addition & 1 deletion ci/requirements-py36.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ dependencies:
- lxml
- pip:
- cfgrib>=0.9.2
- mypy==0.650
- mypy==0.660
6 changes: 3 additions & 3 deletions xarray/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import warnings
from collections import OrderedDict, defaultdict
from contextlib import suppress
from typing import Any, Mapping, Optional
from typing import Any, Mapping, Optional, Tuple

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -317,7 +317,7 @@ def reindex_variables(

# build up indexers for assignment along each dimension
int_indexers = {}
targets = OrderedDict()
targets = OrderedDict() # type: OrderedDict[Any, pd.Index]
masked_dims = set()
unchanged_dims = set()

Expand Down Expand Up @@ -357,7 +357,7 @@ def reindex_variables(
'the new index %r' % (dim, existing_size, new_size))

# create variables for the new dataset
reindexed = OrderedDict()
reindexed = OrderedDict() # type: OrderedDict[Any, Variable]

for dim, indexer in indexers.items():
if isinstance(indexer, DataArray) and indexer.dims != (dim,):
Expand Down
13 changes: 6 additions & 7 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def apply_dataarray_ufunc(func, *args, **kwargs):


def ordered_set_union(all_keys: List[Iterable]) -> Iterable:
result_dict = OrderedDict()
result_dict = OrderedDict() # type: OrderedDict[Any, None]
for keys in all_keys:
for key in keys:
result_dict[key] = None
Expand Down Expand Up @@ -284,11 +284,10 @@ def _as_variables_or_variable(arg):


def _unpack_dict_tuples(
result_vars, # type: Mapping[Any, Tuple[Variable]]
num_outputs, # type: int
):
# type: (...) -> Tuple[Dict[Any, Variable], ...]
out = tuple(OrderedDict() for _ in range(num_outputs))
result_vars: Mapping[Any, Tuple[Variable]],
num_outputs: int,
) -> 'Tuple[OrderedDict[Any, Variable], ...]':
out = tuple(OrderedDict() for _ in range(num_outputs)) # type: ignore
for name, values in result_vars.items():
for value, results_dict in zip(values, out):
results_dict[name] = value
Expand Down Expand Up @@ -444,7 +443,7 @@ def unified_dim_sizes(
exclude_dims: AbstractSet = frozenset(),
) -> 'OrderedDict[Any, int]':

dim_sizes = OrderedDict()
dim_sizes = OrderedDict() # type: OrderedDict[Any, int]

for var in variables:
if len(set(var.dims)) < len(var.dims):
Expand Down
31 changes: 17 additions & 14 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ def merge_indexes(


def split_indexes(
dims_or_levels, # type: Union[Any, List[Any]]
variables, # type: Dict[Any, Variable]
coord_names, # type: Set
level_coords, # type: Dict[Any, Any]
drop=False, # type: bool
dims_or_levels, # type: Union[Any, List[Any]]
variables, # type: OrderedDict[Any, Variable]
coord_names, # type: Set
level_coords, # type: Dict[Any, Any]
drop=False, # type: bool
):
# type: (...) -> Tuple[OrderedDict[Any, Variable], Set]
"""Extract (multi-)indexes (levels) as variables.
Expand All @@ -216,7 +216,7 @@ def split_indexes(
dims.append(k)

vars_to_replace = {}
vars_to_create = OrderedDict()
vars_to_create = OrderedDict() # type: OrderedDict[Any, Variable]
vars_to_remove = []

for d in dims:
Expand Down Expand Up @@ -696,11 +696,14 @@ def _from_vars_and_coord_names(cls, variables, coord_names, attrs=None):
dims = dict(calculate_dimensions(variables))
return cls._construct_direct(variables, coord_names, dims, attrs)

def _replace(
# TODO(shoyer): renable type checking on this signature when pytype has a
# good way to handle defaulting arguments to a sentinel value:
# https://github.com/python/mypy/issues/1803
def _replace( # type: ignore
self: T,
variables: 'OrderedDict[Any, Variable]' = None,
coord_names: set = None,
dims: 'OrderedDict[Any, int]' = None,
dims: Dict[Any, int] = None,
attrs: 'Optional[OrderedDict]' = __default,
indexes: 'Optional[OrderedDict[Any, pd.Index]]' = __default,
encoding: Optional[dict] = __default,
Expand Down Expand Up @@ -745,7 +748,7 @@ def _replace(
variables, coord_names, dims, attrs, indexes, encoding)
return obj

def _replace_with_new_dims(
def _replace_with_new_dims( # type: ignore
self: T,
variables: 'OrderedDict[Any, Variable]' = None,
coord_names: set = None,
Expand All @@ -758,7 +761,7 @@ def _replace_with_new_dims(
return self._replace(
variables, coord_names, dims, attrs, indexes, inplace=inplace)

def _replace_vars_and_dims(
def _replace_vars_and_dims( # type: ignore
self: T,
variables: 'OrderedDict[Any, Variable]' = None,
coord_names: set = None,
Expand Down Expand Up @@ -931,7 +934,7 @@ def _copy_listed(self: T, names) -> T:
"""Create a new Dataset with the listed variables from this dataset and
the all relevant coordinates. Skips all validation.
"""
variables = OrderedDict()
variables = OrderedDict() # type: OrderedDict[Any, Variable]
coord_names = set()

for name in names:
Expand Down Expand Up @@ -976,7 +979,7 @@ def _construct_dataarray(self, name) -> 'DataArray':

needed_dims = set(variable.dims)

coords = OrderedDict()
coords = OrderedDict() # type: OrderedDict[Any, Variable]
for k in self.coords:
if set(self.variables[k].dims) <= needed_dims:
coords[k] = self.variables[k]
Expand Down Expand Up @@ -1823,12 +1826,12 @@ def relevant_keys(mapping):
else:
# dim is a string
dim_name = dim
dim_coord = None
dim_coord = None # type: ignore

reordered = self.transpose(
*(list(indexer_dims) + list(non_indexed_dims)))

variables = OrderedDict()
variables = OrderedDict() # type: ignore

for name, var in reordered.variables.items():
if name in indexers_dict or any(
Expand Down
11 changes: 6 additions & 5 deletions xarray/core/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
'no_conflicts': 4})


def broadcast_dimension_size(variables):
# type: (List[Variable],) -> Variable
def broadcast_dimension_size(
variables: List[Variable],
) -> 'OrderedDict[Any, int]':
"""Extract dimension sizes from a dictionary of variables.
Raises ValueError if any dimensions have different sizes.
"""
dims = OrderedDict()
dims = OrderedDict() # type: OrderedDict[Any, int]
for var in variables:
for dim, size in zip(var.dims, var.shape):
if dim in dims and size != dims[dim]:
Expand Down Expand Up @@ -149,7 +150,7 @@ def merge_variables(

# n.b. it's important to fill up merged in the original order in which
# variables appear
merged = OrderedDict()
merged = OrderedDict() # type: OrderedDict[Any, Variable]

for name, var_list in lookup.items():
if name in priority_vars:
Expand Down Expand Up @@ -177,7 +178,7 @@ def merge_variables(

def expand_variable_dicts(
list_of_variable_dicts: 'List[Union[Dataset, OrderedDict]]',
) -> 'List[OrderedDict[Any, Variable]]':
) -> 'List[Mapping[Any, Variable]]':
"""Given a list of dicts with xarray object values, expand the values.
Parameters
Expand Down

0 comments on commit 6d20766

Please sign in to comment.