Open
Description
The following function is given as a recipe in the itertools
docs, indicating that it's an idiomatic usage of groupby
:
from itertools import groupby
def all_equal(iterable):
g = groupby(iterable)
return next(g, True) and not next(g, False)
I think the correct way of adding type annotations to this function would be as follows, since it will work on arbitrary iterables:
from collections.abc import Iterable
from itertools import groupby
def all_equal(iterable: Iterable[object]) -> bool:
g = groupby(iterable)
return next(g, True) and not next(g, False)
Unfortunately, however, mypy complains about this function:
error: Argument 1 to "next" has incompatible type "groupby[object, object]"; expected "SupportsNext[bool]" [arg-type]
(Mypy gives a similar error if I use Iterable[Any]
instead of Iterable[object]
for the argument annotation.)
Perhaps we should consider copy-and-pasting all the itertools recipes into our test_cases
directory. They're all meant to be idiomatic uses of itertools, so if any of them fail to type check, there's probably a problem somewhere.