python / python/mypy

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

オープン
#16,492 コメント 2 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず issue の再現を mypy で実行し、iter(C())、list(C())、アンパック、スター付き引数について reveal_type の結果を比較します。リスト構築、アンパック、スター付き呼び出しに対する型チェックのエントリーポイントを追跡します。完了の条件は、以前に報告された Any のケースも含め、一覧に挙げたすべての要素型が一貫して bool と推論されることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。