python / python/mypy

and expression with bool() does not work with Optionals

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

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

feature priority-2-low topic-strict-optional
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

For the following code, mypy reports an incompatible type error on the function f

from typing import Optional

def f(x:Optional[str]) -> bool:
  return bool(x) and g(x)

def f2(x:Optional[str]) -> bool:
  if x:
    return g(x)
  return False

def g(x: str) -> bool:
  return bool(x)
test.py:4: error: Argument 1 to "g" has incompatible type "Optional[str]"; expected "str"

however the code is equivalent to the one in f2, for which no error is raised.

  • What are the versions of mypy and Python you are using?
    Mypy: 0.620, Python: 3.6.5

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

[mypy]
# This flag should be removed here and enabled per module when we have a
# considerable number of stubs for external libraries.
ignore_missing_imports = True
strict_optional = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
check_untyped_defs = True

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

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

はじめの一歩

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

調査の方向性

まず、strict_optional を有効にして報告された最小再現例を実行し、f と f2 の診断を比較します。mypy による bool(x) と g(x) の式の処理、および Optional[str] の絞り込みを追跡します。対応する f 関数が incompatible-type エラーなしで受け入れられ、既存の f2 の動作が引き続き有効であれば完了です。

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

評価

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

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

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