Skip to content

Commit

Permalink
Check untyped defs with mypy
Browse files Browse the repository at this point in the history
Add missing type annotations, fix a bug found by mypy,
enable --check-untyped-defs in tox.ini
  • Loading branch information
lopuhin committed Nov 7, 2016
1 parent 1ed2fe7 commit a4a7358
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion eli5/_feature_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _get_top_features(feature_names, coef, top):
no more than ``num_neg`` negative features.
"""
if isinstance(top, (list, tuple)):
num_pos, num_neg = top
num_pos, num_neg = list(top) # "list" is just for mypy
pos = _get_top_positive_features(feature_names, coef, num_pos)
neg = _get_top_negative_features(feature_names, coef, num_neg)
else:
Expand Down
3 changes: 2 additions & 1 deletion eli5/formatters/text.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import six
from typing import List

from . import fields
from .features import FormattedFeatureName
Expand All @@ -14,7 +15,7 @@


def format_as_text(expl, show=fields.ALL):
lines = []
lines = [] # type: List[str]

if expl.error: # always shown
lines.extend(_error_lines(expl))
Expand Down
6 changes: 3 additions & 3 deletions eli5/lime/samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import absolute_import
import abc
import six
from typing import Tuple
from typing import List, Tuple

import numpy as np

Expand Down Expand Up @@ -145,8 +145,8 @@ class UnivariateKernelDensitySampler(_BaseKernelDensitySampler):
of the features instead of generating totally new examples.
"""
def fit(self, X, y=None):
self.kdes_ = []
self.grids_ = []
self.kdes_ = [] # type: List[KernelDensity]
self.grids_ = [] # type: List[GridSearchCV]
num_features = X.shape[-1]
for i in range(num_features):
grid, kde = self._fit_kde(self.kde, X[:, i].reshape(-1, 1))
Expand Down
3 changes: 2 additions & 1 deletion eli5/sklearn/text.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from typing import Set, Tuple

from six.moves import xrange
from sklearn.feature_extraction.text import VectorizerMixin
Expand Down Expand Up @@ -56,7 +57,7 @@ def _get_features(feature):
def _get_other(feature_weights, feature_weights_dict, found_features):
# search for items that were not accounted at all.
other_items = []
accounted_keys = set()
accounted_keys = set() # type: Set[Tuple[str, int]]
for feature, (_, key) in feature_weights_dict.items():
if key not in found_features and key not in accounted_keys:
group, idx = key
Expand Down
4 changes: 2 additions & 2 deletions eli5/sklearn/unhashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from collections import defaultdict, Counter
from itertools import chain
from typing import List, Iterable, Any
from typing import List, Iterable, Any, Dict

import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
Expand Down Expand Up @@ -183,7 +183,7 @@ def _get_collisions(indices):
Return a dict ``{column_id: [possible term ids]}``
with collision information.
"""
collisions = defaultdict(list)
collisions = defaultdict(list) # type: Dict[int, List[int]]
for term_id, hash_id in enumerate(indices):
collisions[hash_id].append(term_id)
return dict(collisions)
Expand Down
2 changes: 1 addition & 1 deletion eli5/sklearn/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def get_feature_names(clf, vec=None, bias_name='<BIAS>', feature_names=None):
if feature_names.n_features != num_features:
raise ValueError("feature_names has a wrong n_features: "
"expected=%d, got=%d" % (num_features,
len(feature_names)))
feature_names.n_features))
# Make a shallow copy setting proper bias_name
return FeatureNames(
feature_names.feature_names,
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ deps=
{[testenv]deps}
mypy-lang
commands=
mypy --silent-imports eli5
mypy --silent-imports --check-untyped-defs eli5

0 comments on commit a4a7358

Please sign in to comment.