Skip to content

itertools.chain returns object when using iterators of different types #17915

@blaxter

Description

@blaxter

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-join-v-unionUsing join vs. using unions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions