python / python/mypy

False positive Argument 2 to "get" of "Message" has incompatible type "str"; expected "bool"

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

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

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

説明

Note: I wasn't sure what a proper title was for this issue, so I chose one that is specific to the issue at hand. I checked typeshed, it looks like the issue is about mypy, not typeshed. I wouldn't be surprised if an issue already existed for this false positive.

Minimal repro:

assert isinstance(email, EmailMessage)

# Fine
r1 = email.get("List-Id", "")
# Fine
r2 = email.get("List-Id", "") or email.get("List-Unsubscribe", "")
# Fine
r3 = bool(email.get("List-Id", "") or email.get("List-Unsubscribe", ""))
# FAILS: Argument 2 to "get" of "Message" has incompatible type "str"; expected "bool"
r4 = bool(
    email.get("Auto-Submitted", "") in ("no", "")
    or email.get("List-Unsubscribe", "")
)
# FAILS: Argument 2 to "get" of "Message" has incompatible type "str"; expected "bool"
r5 = email.get("Auto-Submitted", "") in ("no", "") or email.get(
    "List-Unsubscribe", ""
)

Both are false positives.

Typeshed:

_T = TypeVar('_T')

class Message:
    ...
    def get(self, name: str, failobj: _T = ...) -> Union[_HeaderType, _T]: ...
  • What is the actual behavior/output? Argument 2 to "get" of "Message" has incompatible type "str"; expected "bool"

  • What is the behavior/output you expect? No error.

  • What are the versions of mypy and Python you are using? mypy 0.711, Python 3.6.7
    Do you see the same issue after installing mypy from Git master? haven't tried it but since 0.711 is fairly recent...

  • What are the mypy flags you are using? (For example --strict-optional)

[mypy]
ignore_missing_imports = True
follow_imports = skip
check_untyped_defs = False
strict_equality = True
disallow_incomplete_defs = True

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

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

はじめの一歩

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

調査の方向性

最小限の Python 再現コードと、示されている typeshed の Message.get シグネチャから始め、mypy 0.711、Python 3.6.7、および一覧にある mypy フラグを使用します。報告された 2 つの誤検知を、エラーがないことが期待される挙動と比較し、その後、get 呼び出しとブール式を処理する型チェックの経路を追跡します。

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

評価

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

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

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