python / python/mypy

`--warn-unreachable` doesn't flag unnecessary comparison to `None`

オープン
#20,060 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る

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

bug topic-reachability
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Here is a full example:


from random import randrange



def f(x: int) -> int | None:

    return None if x == 0 else 2 * x



def g(x: int) -> int:

    return 2 * x



if __name__ == '__main__':

    if f(randrange(10)) is None:

        print('f: got 0')



    if g(randrange(10)) is None:  # <-- unreachable but not flagged

        print('g: got 0')

mypy --strict --warn-unreachable does not raise any warnings, but it should. So this is a FALSE NEGATIVE bug. Or a new feature, whatever, but in any case this situation should be flagged.

Originally posted by @nickdrozd in #18386

This might be intentional, but I think it's counterintuitive and I can't really see a benefit. Making an issue to make sure I get around to investigating this.

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

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

はじめの一歩

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

調査の方向性

まず、提供された例を mypy --strict --warn-unreachable で再現し、g を含む比較が報告されないことを確認します。None との比較に対する --warn-unreachable の解析を追跡します。不要な比較が診断され、f を含む到達可能な比較が誤って指摘されなければ完了です。

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

評価

技術スタック
python
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

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

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