python / python/mypy

FOO and next(iter(FOO))

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

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

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

説明

Bug Report

Python code

FOO = frozenset("abc")
BAR = FOO and next(iter(FOO))

Mypy reports and error

wtf.py:2: error: Argument 1 to "iter" has incompatible type "FrozenSet[str]"; expected "Iterable[FrozenSet[str]]"

Expected Behavior

Ideally, mypy should infer that type of BAR is "falsey or string".
If not that, then "empty set or string".
if not that, then "set or string".

Actual Behavior

mypy seems to be tripped by X op func(X), gets confused 😖
mypy complains about the argument to iter 🤷🏿

Note that expression like BAR = next(iter(FOO)) is understood correctly.

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.5
  • Operating system and version: macos, universal2

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

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

はじめの一歩

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

調査の方向性

まず、mypy 0.910 を使って最小限の Python 例を再現し、型チェッカーが iter(FOO) 周辺のブール値 and 式をどのように処理するかを調べます。例で互換性のない引数エラーが報告されなくなり、BAR の推論された型が期待される選択肢のいずれかと一致すれば完了です。

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

評価

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

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

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