python / python/mypy

Non-hashable tuple should not be accepted as `Hashable`

未关闭
#19,232 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 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

把新 issue 发到你的邮箱

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