python / python/mypy

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

未关闭
#20,060 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先,使用 mypy --strict --warn-unreachable 重现提供的示例,并确认涉及 g 的比较不会被报告。跟踪针对与 None 进行比较的 --warn-unreachable 分析;当不必要的比较被诊断出来,同时没有错误地标记涉及 f 的可达比较时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。