0

プロジェクトでいくつかの utils 関数に対して mypy を試していますが、groupby と next を組み合わせたこの関数に問題があります。

これは関数コードです:

from itertools import groupby
from typing import Iterable, Any


def all_same(iterable: Iterable[Any]) -> bool:
    """Return True if all elements in iterable are equal
    >>> all_same([3, 3, 3])
    True
    >>> all_same([3, 3, 1])
    False
    >>> all_same([])
    True
    >>> all_same(['a', 'a'])
    True
    """
    g = groupby(iterable)
    return bool(next(g, True)) and not bool(next(g, False))

を推測できないというエラーが表示され続けますtype argument 1 of "next"

$ mypy testing.py 
testing.py: note: In function "all_same":
testing.py:17: error: Cannot infer type argument 1 of "next"

ここの型を推測できないという意味だと思いgますよね?

これが私の型注釈または の型注釈の問題であるかどうかを理解するのに苦労していgroupbyます。

参考までに、これはの型注釈ですgroupby

@overload
def groupby(iterable: Iterable[_T]) -> Iterator[Tuple[_T, Iterator[_T]]]: ...

したがって、これは、「groupby は T 型の iterable を受け取り、2 つの項目を含むタプルのイテレーターを返す (T 型の 1 つの項目、T 型のオブジェクトのイテレーター)」ことを意味します。next私には良さそうに見えますが、 mypy はasの最初の引数を推測できるはずIterator[Tuple[Any, Iterator[Any]]]ですよね?

私は何が欠けていますか?

4

1 に答える 1