The beauty of Python type hints

Here’s an example of a Python code properly using type hints.

import itertools
from typing import Callable, Iterable, List, Tuple, TypeVar


A = TypeVar('A')
B = TypeVar('B')


def groupby_eager(iterable: Iterable[A],
                  key: Callable[[A], B]) -> List[Tuple[B, List[A]]]:
    groups = itertools.groupby(sorted(iterable, key=key), key=key)
    return [
        (group_name, list(group))
        for group_name, group in groups
    ]

I think type hints help readability a lot. You don’t need to read the implementation, simply look at the signature. Scala/Haskell level of magic.