False positive error "incompatible type" on bound TypeVar
未关闭
还没有人认领这个 Issue。
bug
topic-inheritance
topic-type-variables
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- PR 合并指标
- PR 指标待抓取
描述
Originally posted on gitter july 29th, where it was suggested I open a bug report
Bug Report
For this code:
from typing import Generic, TypeVar, Union
ListT = TypeVar("ListT", str, float)
class TestListA(Generic[ListT]):
def __init__(self, device: ListT) -> None:
self._device: ListT = device
class TestListB(TestListA[ListT]):
def __init__(self, device: ListT) -> None:
super().__init__(device)
mypy outputs error:
test.py:13: error: Argument 1 to "__init__" of "TestListA" has incompatible type "str"; expected "ListT" [arg-type]
test.py:13: error: Argument 1 to "__init__" of "TestListA" has incompatible type "float"; expected "ListT" [arg-type]
which is false positive.
Please note that when using a TypeVar with bound=Union, then mypy doesn't complain.
ListT = TypeVar("ListT", bound=Union[str, float])
Your Environment
- Mypy version used:
mypy 0.971 (compiled: yes) - Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used:
Python 3.9.12 - Operating system and version:
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先使用 mypy 0.971 重现所提供的 TestVar 示例,然后将其受约束 TypeVar 的行为与 bound=Union 变体进行比较。定位 TestListB.init 的类型检查路径,并为误报诊断添加回归测试覆盖。完成的标准是受约束示例无错误通过,同时不兼容的调用仍会被诊断出来。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers, devtools
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 38/100