Type detection broken on attribute override and not-equal-assertion
オープン
まだ誰も着手していません。
bug
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- 平均マージ
- 1日 18時間
- マージ済み PR(30日)
- 54
説明
Bug Report
I commonly find myself writing simplified list init parameters. So, if the user provides only a type X, I automatically convert that to list type [X]. In doing so, I found two issue that prevent a proper type detection. See the example code below.
To Reproduce
#!/usr/bin/env python3
from typing import Union, List, Optional
def test(a: Union[List[str], str, None] = None) -> Optional[List[str]]:
if type(a) == str:
a = [a]
assert(type(a) != str)
return a
Expected Behavior
- First, the
type(a) == strcheck and consequent list assignment should remove the str type from the Union choices. - Second, the
assert(!=)check should also remove the str type from the Union options.
Actual Behavior
Both checks do not work and the type is still guessed incorrectly:

Your Environment
- Mypy version used: 0.910
- Mypy command-line flags: pip install
- Mypy configuration options from
mypy.ini(and other config files): - - Python version used: 3.8
- Operating system and version: macOS 10.15
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供されている Python 再現コードを mypy に対して実行し、type(a) == str の代入と assert(type(a) != str) のチェックにおける型の絞り込みの動作を調べます。両方の分岐で推論された Union から str が除外され、関数が期待される Optional[List[str]] の戻り値型で受け入れられれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100