mypy
mypy copied to clipboard
itertools.chain returns object when using iterators of different types
Bug Report
I think this is related to the join-v-union discussion but this case is so easy to reproduce that I thought it's worth the ticket
To Reproduce
from itertools import chain
class A:
def foo(self) -> None: ...
class B:
def foo(self) -> None: ...
a_items = [A(), A()]
b_items = [B(), B()]
for item in chain(a_items, b_items):
item.foo()
A workaround for this is to explicitly have an items: Iterator[A | B] = chain(...)
Expected Behavior
There shouldn't be any errors
Actual Behavior
chain.py:14: error: "object" has no attribute "foo" [attr-defined]
Your Environment
$ mypy --version
mypy 1.11.2 (compiled: yes)
$ python --version
Python 3.12.3