[2.0 regression] Bad result from heuristic non-narrowing of type[T]
未关闭
还没有人认领这个 Issue。
bug
topic-type-narrowing
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- 平均合并
- 1 天 18 小时
- 30 天内合并 PR
- 54
描述
Bug Report
(A clear and concise description of what the bug is.)
To Reproduce
from math import isnan
from typing import TypeVar
V = TypeVar("V")
def assert_scalar_equal(s1: V, s2: V, /) -> None:
assert type(s1) is type(s2)
if type(s1) is float and type(s2) is float and isnan(s2):
assert isnan(s1)
else:
assert s1 == s2
Expected Behavior
This snippet should type check.
Actual Behavior
mypy.ini: No [mypy] section in config file
t.py:9: error: Argument 1 to "isnan" has incompatible type "V"; expected "SupportsFloat | SupportsIndex" [arg-type]
t.py:10: error: Argument 1 to "isnan" has incompatible type "V"; expected "SupportsFloat | SupportsIndex" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
Even more minimal reprod:
from math import isnan
from typing import TypeVar
V = TypeVar("V")
def foo(s1: V, /) -> None:
if type(s1) is float:
assert isnan(s1)
mypy.ini: No [mypy] section in config file
t.py:9: error: Argument 1 to "isnan" has incompatible type "V"; expected "SupportsFloat | SupportsIndex" [arg-type]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 2.0.0 or 2.1.0
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: tested 3.11.14 and 3.14.6
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先使用 mypy 2.0.0 或 2.1.0 运行最小 t.py 复现,并检查 type(s1) is float 之后的类型收窄行为。将其与 isnan 的预期收窄进行比较;完成的标准是提供的两个代码片段都能通过类型检查,且不出现报告的 arg-type 错误。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 55/100