Skip to content

Commit

Permalink
Enforce mypy compliance in CI (#3197)
Browse files Browse the repository at this point in the history
* Enforce mypy compliance in CI

* Fix mypy errors

* black

* docstrings

* Update doc/whats-new.rst

Co-Authored-By: Maximilian Roos <5635139+max-sixty@users.noreply.github.com>

* typo

* Pre-commit hook
  • Loading branch information
crusaderky authored Aug 10, 2019
1 parent c517bc3 commit befc72f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

- [ ] Closes #xxxx
- [ ] Tests added
- [ ] Passes `black .` & `flake8`
- [ ] Passes `black . && mypy . && flake8`
- [ ] Fully documented, including `whats-new.rst` for all changes and `api.rst` for new API
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ repos:
rev: v2.2.3
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.720
hooks:
- id: mypy
# run these occasionally, ref discussion https://github.com/pydata/xarray/pull/3194
# - repo: https://github.com/asottile/pyupgrade
# rev: v1.22.1
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
- template: ci/azure/install.yml
- bash: |
source activate xarray-tests
mypy . || exit 0
mypy .
displayName: mypy type checks
- job: Docs
Expand Down
4 changes: 2 additions & 2 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ New functions/methods
<http://github.com/DavidMertz>`_.

- 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::
coverage is not complete yet). mypy type checking is now enforced by CI.
Libraries that depend on xarray and use mypy can now remove from their setup.cfg the lines::

[mypy-xarray]
ignore_missing_imports = True
Expand Down
7 changes: 1 addition & 6 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ def wrapped_func(self, dim=None, axis=None, skipna=None, **kwargs):

else:

def wrapped_func(
self,
dim=None,
axis=None, # type: ignore
**kwargs
):
def wrapped_func(self, dim=None, axis=None, **kwargs): # type: ignore
return self.reduce(func, dim, axis, allow_lazy=True, **kwargs)

return wrapped_func
Expand Down
32 changes: 10 additions & 22 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,17 +408,9 @@ def __init__(
self,
# could make a VariableArgs to use more generally, and refine these
# categories
data_vars: Mapping[
Hashable,
Union[
"DataArray",
Variable,
Tuple[Hashable, Any],
Tuple[Sequence[Hashable], Any],
],
] = None,
data_vars: Mapping[Hashable, Any] = None,
coords: Mapping[Hashable, Any] = None,
attrs: Mapping = None,
attrs: Mapping[Hashable, Any] = None,
compat=None,
):
"""To load data from a file or file-like object, use the `open_dataset`
Expand All @@ -439,6 +431,8 @@ def __init__(
- mapping {var name: Variable}
- mapping {var name: (dimension name, array-like)}
- mapping {var name: (tuple of dimension names, array-like)}
- mapping {dimension name: array-like}
(it will be automatically moved to coords, see below)
Each dimension must have the same length in all variables in which
it appears.
Expand All @@ -460,6 +454,7 @@ def __init__(
- mapping {coord name: (dimension name, array-like)}
- mapping {coord name: (tuple of dimension names, array-like)}
- mapping {dimension name: array-like}
(the dimension name is implicitly set to be the same as the coord name)
The last notation implies that the coord name is the same as the
dimension name.
Expand Down Expand Up @@ -2052,17 +2047,13 @@ def relevant_keys(mapping):
]

coords = relevant_keys(self.coords)
indexers = [
(k, np.asarray(v)) # type: ignore
for k, v in indexers.items()
]
indexers_dict = dict(indexers)
indexers = {k: np.asarray(v) for k, v in indexers.items()}
non_indexed_dims = set(self.dims) - indexer_dims
non_indexed_coords = set(self.coords) - set(coords)

# All the indexers should be iterables
# Check that indexers are valid dims, integers, and 1D
for k, v in indexers:
for k, v in indexers.items():
if k not in self.dims:
raise ValueError("dimension %s does not exist" % k)
if v.dtype.kind != "i": # type: ignore
Expand All @@ -2071,7 +2062,7 @@ def relevant_keys(mapping):
raise ValueError("Indexers must be 1 dimensional")

# all the indexers should have the same length
lengths = {len(v) for k, v in indexers}
lengths = {len(v) for k, v in indexers.items()}
if len(lengths) > 1:
raise ValueError("All indexers must be the same length")

Expand Down Expand Up @@ -2109,12 +2100,9 @@ def relevant_keys(mapping):
variables = OrderedDict() # type: ignore

for name, var in reordered.variables.items():
if name in indexers_dict or any(d in indexer_dims for d in var.dims):
if name in indexers or any(d in indexer_dims for d in var.dims):
# slice if var is an indexer or depends on an indexed dim
slc = [
indexers_dict[k] if k in indexers_dict else slice(None)
for k in var.dims
]
slc = [indexers.get(k, slice(None)) for k in var.dims]

var_dims = [dim_name] + [d for d in var.dims if d in non_indexed_dims]
selection = take(var, tuple(slc))
Expand Down
14 changes: 3 additions & 11 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,12 +775,8 @@ def wrapped_func(

else:

def wrapped_func(
self,
dim=DEFAULT_DIMS,
axis=None, # type: ignore
keep_attrs=None,
**kwargs
def wrapped_func( # type: ignore
self, dim=DEFAULT_DIMS, axis=None, keep_attrs=None, **kwargs
):
return self.reduce(
func, dim, axis, keep_attrs=keep_attrs, allow_lazy=True, **kwargs
Expand Down Expand Up @@ -912,11 +908,7 @@ def wrapped_func(self, dim=DEFAULT_DIMS, skipna=None, **kwargs):

else:

def wrapped_func(
self,
dim=DEFAULT_DIMS, # type: ignore
**kwargs
):
def wrapped_func(self, dim=DEFAULT_DIMS, **kwargs): # type: ignore
return self.reduce(
func, dim, numeric_only=numeric_only, allow_lazy=True, **kwargs
)
Expand Down

0 comments on commit befc72f

Please sign in to comment.