Skip to content

Commit

Permalink
feature: add type hints for recipes (pytoolz#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiehong committed Oct 9, 2020
1 parent e9bc1e1 commit 0f3cc15
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ dist/
*.egg-info/
bench/shakespeare.txt
.coverage
.idea

\.tox/
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ env:

# command to install dependencies
install:
- pip install coverage pep8 pytest
- pip install coverage pep8 pytest 'mypy==0.782'

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

# load coverage status to https://coveralls.io
after_success:
Expand Down
2 changes: 1 addition & 1 deletion toolz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from . import curried, sandbox

functoolz._sigs.create_signature_registry()
functoolz._sigs.create_signature_registry() # type: ignore

from ._version import get_versions
__version__ = get_versions()['version']
Expand Down
6 changes: 3 additions & 3 deletions toolz/_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@
lambda a, b: None],
)

module_info['toolz'] = dict(
module_info['toolz'] = dict( # type: ignore
curry=[
(0, lambda *args, **kwargs: None)],
excepts=[
Expand All @@ -584,7 +584,7 @@
(0, lambda func=None, cache=None, key=None: None)],
)

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


signatures = {}
signatures = {} # type: ignore


def create_signature_registry(module_info=module_info, signatures=signatures):
Expand Down
2 changes: 1 addition & 1 deletion toolz/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class NotThisMethod(Exception):
"""Exception raised if a method is not valid for the current scenario."""


LONG_VERSION_PY = {}
LONG_VERSION_PY = {} # type: ignore
HANDLERS = {}


Expand Down
2 changes: 1 addition & 1 deletion toolz/curried/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@
valfilter = toolz.curry(toolz.valfilter)
valmap = toolz.curry(toolz.valmap)

del exceptions
del exceptions # type: ignore
del toolz
14 changes: 10 additions & 4 deletions toolz/recipes.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import itertools
from .itertoolz import frequencies, pluck, getter
from typing import TypeVar, Dict, Tuple, Callable, Any, Iterable, Sequence

from .itertoolz import frequencies, pluck, getter

__all__ = ('countby', 'partitionby')

A = TypeVar('A')
B = TypeVar('B')
KeyLike = TypeVar('KeyLike', int, Iterable, Callable, Tuple)


def countby(key, seq):
def countby(key: KeyLike, seq: Iterable[Any]) -> Dict[Any, int]:
""" Count elements of a collection by a key function
>>> countby(len, ['cat', 'mouse', 'dog'])
Expand All @@ -20,10 +25,11 @@ def countby(key, seq):
"""
if not callable(key):
key = getter(key)
return frequencies(map(key, seq))
return frequencies(map(key, seq)) # type: ignore


def partitionby(func, seq):
def partitionby(func: Callable[[A], bool],
seq: Sequence[A]) -> Iterable[Tuple[A, ...]]:
""" Partition a sequence according to a function
Partition `s` into a sequence of lists such that, when traversing
Expand Down

0 comments on commit 0f3cc15

Please sign in to comment.