Skip to content

Instantly share code, notes, and snippets.

@technillogue
Last active July 5, 2019 15:48
Show Gist options
  • Save technillogue/1ca97d924c55b849405e036c95cc774a to your computer and use it in GitHub Desktop.
Save technillogue/1ca97d924c55b849405e036c95cc774a to your computer and use it in GitHub Desktop.
mypy
$ mypy test.py
test.py:71: error: List comprehension has incompatible type List[Callable[[List[str]], List[str]]]; expected List[Callable[[Arg(List[str], 'names')], List[str]]]
from functools import reduce
from itertools import combinations
from typing import List, Callable, Sequence, TypeVar
X = TypeVar("X")
Y = TypeVar("Y")
Z = TypeVar("Z")
def compose(f: Callable[[Y], Z], g: Callable[[X], Y]) -> Callable[[X], Z]:
def composed_function(arg: X) -> Z:
return f(g(arg))
composed_function.__name__ = composed_function.__qualname__ = "_".join(
(f.__name__, g.__name__)
)
return composed_function
Names = List[str]
def remove_none(names: Names) -> Names:
return names
def remove_nonblank(names: Names) -> Names:
return list(filter(None, names))
def remove_short(names: Names) -> Names:
return [name for name in names if len(name) > 2]
Refiners = Sequence[Callable[[Names], Names]]
UNIQUE_REFINERS: Refiners = [remove_short, remove_nonblank]
REFINERS: Refiners = [remove_none] + [
reduce(compose, combination)
for i in range(1, len(UNIQUE_REFINERS))
for combination in combinations(UNIQUE_REFINERS, i)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment