Non-hashable tuple should not be accepted as `Hashable`
未关闭
还没有人认领这个 Issue。
bug
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- 平均合并
- 1 天 18 小时
- 30 天内合并 PR
- 54
描述
Bug Report
I'd expect to get a type error when trying to pass a tuple to a Hashable bound if that tuple contains any non-hashable element type.
To Reproduce
from typing import Hashable, TypeVar
T = TypeVar("T", bound=Hashable)
def f(x: T) -> T:
print(hash(x))
return x
def usage1(x: tuple[dict[str, str], ...]):
f(x)
def usage2(x: tuple[dict[str, str], int, int]):
f(x)
def usage3(x: tuple[int, int, dict[str, str]]):
f(x)
Expected Behavior
Type error in all f(x) lines, rejecting the non-hashable type.
Actual Behavior
The type check passes despite the unsound typing.
Your Environment
- Mypy version used: 1.16.0
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.12
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
先使用提供的 Python 3.12 和 mypy 1.16.0 重现问题,然后检查针对 Hashable 边界和 tuple 元素类型的类型检查路径。为 usage1、usage2 和 usage3 添加回归覆盖;当三个调用都被拒绝时,工作即告完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- tooling
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100