Type-narrowing does not apply across 2 branches having the same condition within the same block
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
-
Are you reporting a bug, or opening a feature request?
Feature request -
Please insert below the code you are checking with mypy,
from typing import Optional
def my_func() -> bool:
return True
created: bool = my_func()
a: Optional[int] = None
if created:
a = 1
z = a + 1 # This is OK
if created:
x = a + 1 # Unsupported operand types for + ("None" and "int")
- What is the actual behavior/output?
$ mypy a.py
a.py:13: error: Unsupported operand types for + ("None" and "int")
a.py:13: note: Left operand is of type "Optional[int]"
Found 1 error in 1 file (checked 1 source file)
- What is the behavior/output you expect?
No errors. Mypy should be able to narrow the type of a to just int in the second if block, because it narrowed it to that type already the first if block and they have the exact same condition.
You can argue that it's easy to workaround it be merging the if blocks, but in real-life code, there could easily be intervening code that is not trivial to move around.
-
What are the versions of mypy and Python you are using?
mypy 0.750+dev.5b73e2a5e62d3da723ac517fabacaa2a0c8cdf3c
Python 3.7.4 -
What are the mypy flags you are using? (For example --strict-optional)
--strict-optional
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
a.py の再現プログラムから始め、mypy を --strict-optional 付きで実行して、現在の診断を確認します。繰り返されている 2 つの if created: ブロックについて型の絞り込みを追跡し、その後、a の 2 回目の使用がエラーなしで受け入れられることを示すカバレッジを追加します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers, tooling
- issue の種類
- 機能追加
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 42/100