Skip to content

Commit befc72f

Browse files
authored
Enforce mypy compliance in CI (#3197)
* Enforce mypy compliance in CI * Fix mypy errors * black * docstrings * Update doc/whats-new.rst Co-Authored-By: Maximilian Roos <[email protected]> * typo * Pre-commit hook
1 parent c517bc3 commit befc72f

File tree

7 files changed

+22
-43
lines changed

7 files changed

+22
-43
lines changed

.github/PULL_REQUEST_TEMPLATE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
- [ ] Closes #xxxx
44
- [ ] Tests added
5-
- [ ] Passes `black .` & `flake8`
5+
- [ ] Passes `black . && mypy . && flake8`
66
- [ ] Fully documented, including `whats-new.rst` for all changes and `api.rst` for new API

.pre-commit-config.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ repos:
1010
rev: v2.2.3
1111
hooks:
1212
- id: flake8
13+
- repo: https://github.com/pre-commit/mirrors-mypy
14+
rev: v0.720
15+
hooks:
16+
- id: mypy
1317
# run these occasionally, ref discussion https://github.com/pydata/xarray/pull/3194
1418
# - repo: https://github.com/asottile/pyupgrade
1519
# rev: v1.22.1

azure-pipelines.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ jobs:
7575
- template: ci/azure/install.yml
7676
- bash: |
7777
source activate xarray-tests
78-
mypy . || exit 0
78+
mypy .
7979
displayName: mypy type checks
8080
8181
- job: Docs

doc/whats-new.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ New functions/methods
2929
<http://github.com/DavidMertz>`_.
3030

3131
- The xarray package is now discoverably by mypy (although typing hints
32-
coverage is not complete yet). mypy users can now remove from their setup.cfg
33-
the lines::
32+
coverage is not complete yet). mypy type checking is now enforced by CI.
33+
Libraries that depend on xarray and use mypy can now remove from their setup.cfg the lines::
3434

3535
[mypy-xarray]
3636
ignore_missing_imports = True

xarray/core/common.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,7 @@ def wrapped_func(self, dim=None, axis=None, skipna=None, **kwargs):
4646

4747
else:
4848

49-
def wrapped_func(
50-
self,
51-
dim=None,
52-
axis=None, # type: ignore
53-
**kwargs
54-
):
49+
def wrapped_func(self, dim=None, axis=None, **kwargs): # type: ignore
5550
return self.reduce(func, dim, axis, allow_lazy=True, **kwargs)
5651

5752
return wrapped_func

xarray/core/dataset.py

+10-22
Original file line numberDiff line numberDiff line change
@@ -408,17 +408,9 @@ def __init__(
408408
self,
409409
# could make a VariableArgs to use more generally, and refine these
410410
# categories
411-
data_vars: Mapping[
412-
Hashable,
413-
Union[
414-
"DataArray",
415-
Variable,
416-
Tuple[Hashable, Any],
417-
Tuple[Sequence[Hashable], Any],
418-
],
419-
] = None,
411+
data_vars: Mapping[Hashable, Any] = None,
420412
coords: Mapping[Hashable, Any] = None,
421-
attrs: Mapping = None,
413+
attrs: Mapping[Hashable, Any] = None,
422414
compat=None,
423415
):
424416
"""To load data from a file or file-like object, use the `open_dataset`
@@ -439,6 +431,8 @@ def __init__(
439431
- mapping {var name: Variable}
440432
- mapping {var name: (dimension name, array-like)}
441433
- mapping {var name: (tuple of dimension names, array-like)}
434+
- mapping {dimension name: array-like}
435+
(it will be automatically moved to coords, see below)
442436
443437
Each dimension must have the same length in all variables in which
444438
it appears.
@@ -460,6 +454,7 @@ def __init__(
460454
- mapping {coord name: (dimension name, array-like)}
461455
- mapping {coord name: (tuple of dimension names, array-like)}
462456
- mapping {dimension name: array-like}
457+
(the dimension name is implicitly set to be the same as the coord name)
463458
464459
The last notation implies that the coord name is the same as the
465460
dimension name.
@@ -2052,17 +2047,13 @@ def relevant_keys(mapping):
20522047
]
20532048

20542049
coords = relevant_keys(self.coords)
2055-
indexers = [
2056-
(k, np.asarray(v)) # type: ignore
2057-
for k, v in indexers.items()
2058-
]
2059-
indexers_dict = dict(indexers)
2050+
indexers = {k: np.asarray(v) for k, v in indexers.items()}
20602051
non_indexed_dims = set(self.dims) - indexer_dims
20612052
non_indexed_coords = set(self.coords) - set(coords)
20622053

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

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

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

21112102
for name, var in reordered.variables.items():
2112-
if name in indexers_dict or any(d in indexer_dims for d in var.dims):
2103+
if name in indexers or any(d in indexer_dims for d in var.dims):
21132104
# slice if var is an indexer or depends on an indexed dim
2114-
slc = [
2115-
indexers_dict[k] if k in indexers_dict else slice(None)
2116-
for k in var.dims
2117-
]
2105+
slc = [indexers.get(k, slice(None)) for k in var.dims]
21182106

21192107
var_dims = [dim_name] + [d for d in var.dims if d in non_indexed_dims]
21202108
selection = take(var, tuple(slc))

xarray/core/groupby.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -775,12 +775,8 @@ def wrapped_func(
775775

776776
else:
777777

778-
def wrapped_func(
779-
self,
780-
dim=DEFAULT_DIMS,
781-
axis=None, # type: ignore
782-
keep_attrs=None,
783-
**kwargs
778+
def wrapped_func( # type: ignore
779+
self, dim=DEFAULT_DIMS, axis=None, keep_attrs=None, **kwargs
784780
):
785781
return self.reduce(
786782
func, dim, axis, keep_attrs=keep_attrs, allow_lazy=True, **kwargs
@@ -912,11 +908,7 @@ def wrapped_func(self, dim=DEFAULT_DIMS, skipna=None, **kwargs):
912908

913909
else:
914910

915-
def wrapped_func(
916-
self,
917-
dim=DEFAULT_DIMS, # type: ignore
918-
**kwargs
919-
):
911+
def wrapped_func(self, dim=DEFAULT_DIMS, **kwargs): # type: ignore
920912
return self.reduce(
921913
func, dim, numeric_only=numeric_only, allow_lazy=True, **kwargs
922914
)

0 commit comments

Comments
 (0)