Add bound Hashable for types for keys of type Mapping and values of type Set
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 5.1k
- 派生
- 2.1k
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 82
描述
Problem
Types used for keys in mappings and values in sets are too broad.
How to reproduce
d: [list, int] = {} # Tools that use typeshed see this code as valid.
d[[1]] = 2 # TypeError: unhashable type: 'list'
Solution
Change type of keys in mappings: _KT = TypeVar("_KT") to _KT = TypeVar("_KT", bound=Hashable)
- Added PR with fixes to stdlib
But I still have questions about how to fix the types in set and frozenset.
-
For set, we using
_T, which is also used for values in the list. Can I change it to_KT? The requirements for the type of values in the set are the same as for the keys in the dictionary. I understand that it is a value, not a key, and the name can be confusing. Do you think of a better option? -
For frozenset, we using covariant
_T_co, which is also used for values in the tuple. It's the same problem here. We need a new type that is covariant and bound to Hashable. -
set
-
frozenset
And once we figure out what to do with types in stdlib, I will be happy to add the same changes to third-party libraries.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先检查 stdlib/builtins.pyi 中 issue 所引用的 set 和 frozenset 定义,以及 PR 9570 中相关的 mapping 更改。确定应如何表示 set 类型变量和一个以 Hashable 为界的协变 frozenset 变量,然后更新 stdlib 存根,并验证相关类型检查工具会拒绝不可哈希的键或 set 值。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- developer-experience
- Issue 类型
- 功能
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100