Skip to content

iter(...) is not revealed the same as __iter__ method #21027

@loicdiridollou

Description

@loicdiridollou

Bug Report

We are having issues with the pandas-stubs repo where the inference for __iter__ is different with iter(...).
With __iter__ we are seeing the right inference while with iter(...)

To Reproduce

from typing import Any, Generic, Never, TypeVar, reveal_type

_T_EXPANDING = TypeVar("_T_EXPANDING")
_T_STR = TypeVar("_T_STR", bound=Series)


class StringMethods(Generic[_T_EXPANDING, _T_STR]):
    def __getitem__(self, key: int) -> _T_STR: ...
    def __iter__(self) -> Never: ...

class Series:
    @property
    def str(self) -> StringMethods[Any, Series]: ...


def _0() -> None:
    reveal_type(StringMethods().__iter__())  # all 4 type checkers give Never

def _1() -> None:
    reveal_type(iter(Series().str))  # mypy: Any, pyright, pyrefly: Never, ty: Unknown

Expected Behavior

It is identifying it as Any.

Actual Behavior

We expect Never.

Your Environment

  • Mypy version used: mypy 1.19.1 (compiled: yes)
  • Mypy command-line flags: mypy test_file.py
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.14

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions