Closed as not planned
Description
Real example:
from typing import Any, Collection, Generator, Iterable, TypeVar, cast
from itertools import chain, islice
T = TypeVar('T')
def _split_on_batch_gen(items: Iterable[T]) -> Generator[Iterable[T], Any, None]:
items = iter(items)
stopper = object()
while True:
first_item = next(items, stopper)
if first_item is stopper:
break
batch: chain[T] = chain((first_item,), islice(items, 0, 99))
yield batch
or, reduced:
from typing import Any, Collection, Generator, Iterable, TypeVar, cast
from itertools import chain, islice
items = iter(range(1000))
stopper = object()
first_item = next(items, stopper)
if first_item is not stopper:
batch: Iterable[int] = chain((first_item,), islice(items, 0, 99))
check here: https://mypy-play.net/?mypy=latest&python=3.11
gives:
main.py:8: error: Argument 1 to "chain" has incompatible type "tuple[object]"; expected "Iterable[int]" [arg-type]
Which is obviously incorrect, since we checked that first_item
is not an object(). I can not check for isinstance(fist_item, object)
because every object is object :). Seems, MyPy understands isinstance()
assertions, but does not understand is
.
Seems, type of next(xxx, yyy)
should be xxx[0] | Literal[yyy]
instead of xxx[0] | yyy
(pseudocode, I don't know how to express correctly)