Skip to content

Commit 646ef40

Browse files
committed
feature: add type hints for recipes (pytoolz#496)
1 parent e9bc1e1 commit 646ef40

File tree

7 files changed

+39
-10
lines changed

7 files changed

+39
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ dist/
44
*.egg-info/
55
bench/shakespeare.txt
66
.coverage
7+
.idea
78

89
\.tox/

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ env:
1818
# command to install dependencies
1919
install:
2020
- pip install coverage pep8 pytest
21+
- if [[ $TRAVIS_PYTHON_VERSION != pypy* ]]; then pip install 'mypy==0.790' ; fi
2122

2223
# command to run tests
2324
# require 100% coverage (not including test files) to pass Travis CI test
@@ -32,6 +33,7 @@ script:
3233
- python setup.py develop
3334
- py.test
3435
- nosetests
36+
- if [[ $TRAVIS_PYTHON_VERSION != pypy* ]]; then mypy toolz/recipes.py ; fi
3537

3638
# load coverage status to https://coveralls.io
3739
after_success:

toolz/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from . import curried, sandbox
2121

22-
functoolz._sigs.create_signature_registry()
22+
functoolz._sigs.create_signature_registry() # type: ignore
2323

2424
from ._version import get_versions
2525
__version__ = get_versions()['version']

toolz/_signatures.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@
571571
lambda a, b: None],
572572
)
573573

574-
module_info['toolz'] = dict(
574+
module_info['toolz'] = dict( # type: ignore
575575
curry=[
576576
(0, lambda *args, **kwargs: None)],
577577
excepts=[
@@ -584,7 +584,7 @@
584584
(0, lambda func=None, cache=None, key=None: None)],
585585
)
586586

587-
module_info['toolz.functoolz'] = dict(
587+
module_info['toolz.functoolz'] = dict( # type: ignore
588588
Compose=[
589589
(0, lambda funcs: None)],
590590
InstanceProperty=[
@@ -653,7 +653,7 @@ def expand_sig(sig):
653653
return num_pos_only, func, keyword_only + keyword_exclude, sigspec
654654

655655

656-
signatures = {}
656+
signatures = {} # type: ignore
657657

658658

659659
def create_signature_registry(module_info=module_info, signatures=signatures):

toolz/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class NotThisMethod(Exception):
5252
"""Exception raised if a method is not valid for the current scenario."""
5353

5454

55-
LONG_VERSION_PY = {}
55+
LONG_VERSION_PY = {} # type: ignore
5656
HANDLERS = {}
5757

5858

toolz/curried/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,5 @@
9999
valfilter = toolz.curry(toolz.valfilter)
100100
valmap = toolz.curry(toolz.valmap)
101101

102-
del exceptions
102+
del exceptions # type: ignore
103103
del toolz

toolz/recipes.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
11
import itertools
2-
from .itertoolz import frequencies, pluck, getter
2+
from typing import TypeVar, Dict, Tuple, Callable, Any, Iterable, Sequence, \
3+
overload, List
34

5+
from .itertoolz import frequencies, pluck, getter
46

57
__all__ = ('countby', 'partitionby')
68

9+
A = TypeVar('A')
10+
B = TypeVar('B')
11+
KeyLike = TypeVar('KeyLike', int, Iterable, Callable, Tuple)
12+
13+
14+
# Case: countby(len, ['cat', 'mouse', 'dog'])
15+
@overload
16+
def countby(key: Callable[[A], B], seq: Iterable[A]) -> Dict[B, int]: ...
17+
18+
19+
# Case: countby('a', [{'a': 1, 'b': 2}, {'a': 10, 'b': 2}])
20+
@overload
21+
def countby(key: A, seq: Iterable[Dict[A, B]]) -> Dict[B, int]: ...
22+
23+
24+
# Case: countby(0, [[1, 2], [10, 2]])
25+
def countby(key: int, seq: Iterable[Iterable[A]]) -> Dict[A, int]: ...
26+
27+
28+
# Case: countby([0, 1], [[1, 2], [10, 2]])
29+
def countby(key: List[int],
30+
seq: Iterable[Iterable[A]]) -> Dict[Tuple[A, ...], int]: ...
31+
732

8-
def countby(key, seq):
33+
def countby(key: KeyLike, seq: Iterable[Any]) -> Dict[Any, int]:
934
""" Count elements of a collection by a key function
1035
1136
>>> countby(len, ['cat', 'mouse', 'dog'])
@@ -20,10 +45,11 @@ def countby(key, seq):
2045
"""
2146
if not callable(key):
2247
key = getter(key)
23-
return frequencies(map(key, seq))
48+
return frequencies(map(key, seq)) # type: ignore
2449

2550

26-
def partitionby(func, seq):
51+
def partitionby(func: Callable[[A], bool],
52+
seq: Sequence[A]) -> Iterable[Tuple[A, ...]]:
2753
""" Partition a sequence according to a function
2854
2955
Partition `s` into a sequence of lists such that, when traversing

0 commit comments

Comments
 (0)