Failure to narrow down type of elements of Tuple
オープン
まだ誰も着手していません。
false-positive
feature
priority-2-low
topic-type-narrowing
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
-
Are you reporting a bug, or opening a feature request?
kind of both -
Please insert below the code you are checking with mypy,
from typing import Optional, Tuple, TypeVar
_T = TypeVar("_T")
_U = TypeVar("_U")
def promote_optional(tup: Tuple[Optional[_T], Optional[_U]]) -> Optional[Tuple[_T, _U]]:
for val in tup:
if val is None:
return None
return tup
def promote_optional2(tup: Tuple[Optional[_T], Optional[_U]]) -> Optional[Tuple[_T, _U]]:
if tup[0] is None:
return None
if tup[1] is None:
return None
return tup
- What is the actual behavior/output?
/tmp/mwe.py:11: error: Incompatible return value type (got "Tuple[Optional[_T], Optional[_U]]", expected "Optional[Tuple[_T, _U]]")
/tmp/mwe.py:19: error: Incompatible return value type (got "Tuple[Optional[_T], Optional[_U]]", expected "Optional[Tuple[_T, _U]]")
Found 2 errors in 1 file (checked 1 source file)
-
What is the behavior/output you expect?
Mypy should accept the code. -
What are the versions of mypy and Python you are using?
mypy 0.782
I want to turn a Tuple[Optional...] to an Optional[Tuple], because the whole tuple is worthless if it contains any None. Unfortunately Mypy seems to be unable to detect that I checked all elements of the tuple for being none.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
/tmp/mwe.py の最小再現から始め、mypy に対して実行して現在のエラーを確認します。None チェック後に mypy がタプル要素の型をどのように絞り込むかを追跡します; 両方の promote_optional 関数が、互換性のない戻り値型のエラーなしで受け入れられれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100