Ignore incompatible type error when variable is checked in if statement
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
I'm using mypy for type checking in my Python projects and I've come across a situation where I believe mypy could be improved.
Consider the following code:
def foo1(a: str): ...
def foo2(a: str | None, b: str | None):
if not a and not b:
return None
if not a:
return foo1(b)
return None
In this case, mypy raises an error:
Argument 1 to "foo1" has incompatible type "str | None"; expected "str" [arg-type].
However, the function foo1 is only called when b
is not None, so in practice, there is no type error.
The logic of the function ensures that b cannot be None when foo1(b) is called.
This is because in the first if statement, we check if both a and b are None.
If they are not, we proceed to the next if statement where we check if a is None.
If a is None, then b must not be None because of the previous check.
Therefore, foo1(b) is only called when b is not None.
Versions
Python: 3.10.10
MyPy: 1.7.1
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供された Python 再現コードに対して mypy を実行し、2 つの if 文が型の絞り込みにどのように影響するかを追跡します。関連する型チェックのテストまたはエントリーポイントを特定し、その後、この制御フローの回帰ケースを追加します。mypy が arg-type エラーなしで foo1(b) を受け入れ、既存のチェックを維持できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100