Skip to content

Unpacking an iterable with an __iter__ method that returns self leads to variable being silently inferred as Any #14819

Closed
@AlexWaygood

Description

@AlexWaygood

This is similar to #14811, but still reproduces on master following bed49ab:

from typing import TypeVar

T = TypeVar("T")

class Foo:
    count: int
    def __init__(self) -> None:
        self.count = 0
    def __iter__(self: T) -> T:
        return self
    def __next__(self) -> int:
        self.count += 1
        if self.count > 3:
            raise StopIteration
        return self.count

a, b, c = Foo()
reveal_type(a)  # note: Revealed type is "Any"

Mypy should have enough information here to infer that the type of a is int.

Mypy versions tested on

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions