Skip to content

Commit f1d10fd

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

File tree

7 files changed

+21
-11
lines changed

7 files changed

+21
-11
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ env:
1717

1818
# command to install dependencies
1919
install:
20-
- pip install coverage pep8 pytest
20+
- pip install coverage pep8 pytest 'mypy==0.782'
2121

2222
# command to run tests
2323
# require 100% coverage (not including test files) to pass Travis CI test
@@ -32,6 +32,7 @@ script:
3232
- python setup.py develop
3333
- py.test
3434
- nosetests
35+
- mypy toolz/recipes.py
3536

3637
# load coverage status to https://coveralls.io
3738
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import itertools
1818
import operator
1919
from importlib import import_module
20+
from typing import Dict
2021

2122
from .functoolz import (is_partial_args, is_arity, has_varargs,
2223
has_keywords, num_required_args)
@@ -571,7 +572,7 @@
571572
lambda a, b: None],
572573
)
573574

574-
module_info['toolz'] = dict(
575+
module_info['toolz'] = dict( # type: ignore
575576
curry=[
576577
(0, lambda *args, **kwargs: None)],
577578
excepts=[
@@ -584,7 +585,7 @@
584585
(0, lambda func=None, cache=None, key=None: None)],
585586
)
586587

587-
module_info['toolz.functoolz'] = dict(
588+
module_info['toolz.functoolz'] = dict( # type: ignore
588589
Compose=[
589590
(0, lambda funcs: None)],
590591
InstanceProperty=[
@@ -653,7 +654,7 @@ def expand_sig(sig):
653654
return num_pos_only, func, keyword_only + keyword_exclude, sigspec
654655

655656

656-
signatures = {}
657+
signatures: Dict = {}
657658

658659

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

toolz/_version.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import re
1616
import subprocess
1717
import sys
18+
from typing import Dict
1819

1920

2021
def get_keywords():
@@ -52,7 +53,7 @@ class NotThisMethod(Exception):
5253
"""Exception raised if a method is not valid for the current scenario."""
5354

5455

55-
LONG_VERSION_PY = {}
56+
LONG_VERSION_PY: Dict = {}
5657
HANDLERS = {}
5758

5859

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: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import itertools
2-
from .itertoolz import frequencies, pluck, getter
2+
from typing import TypeVar, Dict, Tuple, Callable, Any, Iterable, Sequence
33

4+
from .itertoolz import frequencies, pluck, getter
45

56
__all__ = ('countby', 'partitionby')
67

8+
A = TypeVar('A')
9+
B = TypeVar('B')
10+
KeyLike = TypeVar('KeyLike', int, Iterable, Callable, Tuple)
11+
712

8-
def countby(key, seq):
13+
def countby(key: KeyLike, seq: Iterable[Any]) -> Dict[Any, int]:
914
""" Count elements of a collection by a key function
1015
1116
>>> countby(len, ['cat', 'mouse', 'dog'])
@@ -20,10 +25,11 @@ def countby(key, seq):
2025
"""
2126
if not callable(key):
2227
key = getter(key)
23-
return frequencies(map(key, seq))
28+
return frequencies(map(key, seq)) # type: ignore
2429

2530

26-
def partitionby(func, seq):
31+
def partitionby(func: Callable[[A], bool],
32+
seq: Sequence[A]) -> Iterable[Tuple[A, ...]]:
2733
""" Partition a sequence according to a function
2834
2935
Partition `s` into a sequence of lists such that, when traversing

0 commit comments

Comments
 (0)