python / python/cpython

Alias that is generic over a ParamSpec is not hashable when using a list as parameters_expression

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

还没有人认领这个 Issue。

topic-typing type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

This is slightly related to #124445, but a different issue, maybe it can be solved at the same time?

Consider the following code where [T] is a parameters_expression according to PEP 612. Similar to using Callable or using Generic[P] or Protocol[P] I would expect the following code to work and receive a hashable variable:

from typing import TypeVar
from collections.abc import Callable
type X[**P] = Callable[P, int]
T = TypeVar("T")
hash(X[[T]])
# ~~~~^^^^^^^^
# TypeError: unhashable type: 'list'

Cause:

The problem is that X[[T]].__args__ == ([T],) which cannot be hashed. I think the list should either be turned into a tuple. Either a nested one as its done for Generic and Protocol or joined like for Callable[P, str][[int]].__args__.

Discussion:

I've opened a parallel topic on discuss

CPython versions tested on:

3.13.0.rc2

Operating systems tested on:

Linux

EDIT: A thought, should this be changed for whole class in general?

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 issue 中的复现用例开始,将 ParamSpec 别名的行为与引用的 Generic、Protocol 和 Callable 案例进行比较。阅读 PEP 612、issue #124445 以及链接的讨论,以确定列表参数应变为嵌套元组还是应进行合并。完成的标准是 hash(X[[T]]) 能够在没有不可哈希列表的情况下工作,同时保留预期的参数。

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

评估

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

把新 issue 发到你的邮箱

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