Код: Выделить всё
from collections.abc import Generator, Iterable
from itertools import chain, tee
from typing import TypeVar
_T1 = TypeVar('_T1')
_MISSING = object()
def pairwise(iterable: Iterable[_T1]) -> Iterable[tuple[_T1, _T1]]:
# See https://docs.python.org/3.9/library/itertools.html#itertools-recipes
a, b = tee(iterable)
next(b, None)
return zip(a, b)
def annotated_last(sequence: Iterable[_T1]) -> Generator[tuple[_T1, bool], Any, None]:
for current_item, next_item in pairwise(chain(sequence, [_MISSING])):
is_last = next_item is _MISSING
yield current_item, is_last #
Подробнее здесь: [url]https://stackoverflow.com/questions/78645653/typing-with-typevar-converts-a-type-to-an-object[/url]