Unpacking an iterable in a list comprehension leads to the type inferred as list[Any]
オープン
まだ誰も着手していません。
bug
good-second-issue
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
When a value of a user-defined class with __iter__ is converted into a list using [*value], the type of the list is inferred as list[Any].
To Reproduce
import typing
class Spam:
def __iter__(self, /) -> typing.Iterator[int]:
yield 1
a = Spam()
# list[int]
reveal_type(list(a))
# list[int]
reveal_type([i for i in a])
# list[int]
reveal_type([*(i for i in a)])
# list[int]
reveal_type([*a.__iter__()])
# list[Any] ???
reveal_type([*a])
b, = a
# int
reveal_type(b)
Expected Behavior
The type of [*a] is list[int]
Actual Behavior
example.py:22: note: Revealed type is "builtins.list[Any]"
Your Environment
- Mypy version used: 1.4.1
- Mypy command-line flags:
--strict - Mypy configuration options from
mypy.ini(and other config files): - Python version used:
3.11.4
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Issue の再現コードから始め、リスト表示がスター付き式と iterable 型をどのように処理するかを追跡します。Iterator[int] を返すユーザー定義の iter に対する回帰テストを追加し、そのうえで [*a] が list[int] と推論され、他の表示される型が壊れていないことを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100