python / python/mypy

Overridden `__iter__` with more specific item type is only respected in some contexts

Open
#16,492 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Given a subclass C of Iterable[int] that more specifically implements def __iter__(self) -> Iterator[bool] (which is fine, since bool is a subclass of int), I would expect mypy to understand that iterating C() yields bools. And indeed it does understand this in some contexts, such as iter(C()) and for item in C(). But in other contexts, such as list(C()) and unpacking, it relies only on the base class Iterable[int] and discards the more specific information from __iter__.

To Reproduce

from typing import Iterable, Iterator

class C(Iterable[int]):
    def __iter__(self) -> Iterator[bool]:
        yield True

# these give bool, as expected
reveal_type(C().__iter__())  # Iterator[bool]
reveal_type(iter(C()))  # Iterator[bool]
reveal_type([comp_item for comp_item in C()])  # list[bool]
for loop_item in C(): reveal_type(loop_item)  # bool

# these unexpectedly give int
reveal_type(list(C()))  # list[int]
[unpack_item] = C(); reveal_type(unpack_item)  # int
[*unpack_items] = C(); reveal_type(unpack_items)  # list[int]

# these unexpectedly give Any
reveal_type([*C()])  # list[Any] (#15747)
reveal_type(max(*C()))  # Any

Playground link

Expected Behavior

All items should be inferred as bool.

main.py:8: note: Revealed type is "typing.Iterator[builtins.bool]"
main.py:9: note: Revealed type is "typing.Iterator[builtins.bool]"
main.py:10: note: Revealed type is "builtins.list[builtins.bool]"
main.py:11: note: Revealed type is "builtins.bool"
main.py:14: note: Revealed type is "builtins.list[builtins.bool]"
main.py:15: note: Revealed type is "builtins.bool"
main.py:16: note: Revealed type is "builtins.list[builtins.bool]"
main.py:19: note: Revealed type is "builtins.list[builtins.bool]"
main.py:20: note: Revealed type is "builtins.bool"

Actual Behavior

The items are inconsistently inferred as bool, int, or Any, depending on the context.

main.py:8: note: Revealed type is "typing.Iterator[builtins.bool]"
main.py:9: note: Revealed type is "typing.Iterator[builtins.bool]"
main.py:10: note: Revealed type is "builtins.list[builtins.bool]"
main.py:11: note: Revealed type is "builtins.bool"
main.py:14: note: Revealed type is "builtins.list[builtins.int]"
main.py:15: note: Revealed type is "builtins.int"
main.py:16: note: Revealed type is "builtins.list[builtins.int]"
main.py:19: note: Revealed type is "builtins.list[Any]"
main.py:20: note: Revealed type is "Any"

One of the Any cases was previously reported:

  • #14470
  • #15747

Your Environment

  • Mypy version used: 1.7.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the issue's reproduction with mypy and compare the reveal_type results for iter(C()), list(C()), unpacking, and starred arguments. Trace the type-checking entry points for list construction, unpacking, and starred calls; done means all listed item types are consistently inferred as bool, including the previously reported Any cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.