Inconsistent behaviors of `dict.keys()`, `dict.values()` and `dict.items()` with `==`
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
dict.keys() and dict.keys() are True because they're the same type and dict.keys() and a list or tuple are False because they're different types but dict.keys() and a set or frozenset are True even though their types are different and it seems like the elements in dict.keys() are ordered but not unordered as shown below:
v = {'A':'B', 'C':'D', 'E':'F', 'G':'H', 'I':'J'}.keys()
print(v)
# dict_keys(['A', 'C', 'E', 'G', 'I'])
print(v == {'A':'B', 'C':'D', 'E':'F', 'G':'H', 'I':'J'}.keys())
# True
print(v == ['A', 'C', 'E', 'G', 'I']) # list
print(v == ('A', 'C', 'E', 'G', 'I')) # tuple
# False
print(v == {'A', 'C', 'E', 'G', 'I'}) # set
print(v == frozenset(['A', 'C', 'E', 'G', 'I'])) # frozenset
# True
for x in v:
print(x)
# A
# C
# E
# G
# I
And, dict.items() and dict.items() are True because they're the same type and dict.items() and a list or tuple are False because they're different types but dict.items() and a set or frozenset are True even though their types are different and it seems like the elements in dict.items() are ordered but not unordered as shown below:
v = {'A':'B', 'C':'D', 'E':'F', 'G':'H', 'I':'J'}.items()
print(v)
# dict_items([('A', 'B'), ('C', 'D'), ('E', 'F'), ('G', 'H'), ('I', 'J')])
print(v == {'A':'B', 'C':'D', 'E':'F', 'G':'H', 'I':'J'}.items())
# True
print(v == [('A', 'B'), ('C', 'D'), ('E', 'F'), ('G', 'H'), ('I', 'J')]) # list
print(v == (('A', 'B'), ('C', 'D'), ('E', 'F'), ('G', 'H'), ('I', 'J'))) # tuple
# False
print(v == {('A', 'B'), ('C', 'D'), ('E', 'F'), ('G', 'H'), ('I', 'J')}) # set
print(v == frozenset([('A', 'B'), ('C', 'D'), ('E', 'F'), ('G', 'H'), ('I', 'J')])) # frozenset
# True
for x in v:
print(x)
# ('A', 'B')
# ('C', 'D')
# ('E', 'F')
# ('G', 'H')
# ('I', 'J')
Finally, dict.values() and dict.values(), a list, tuple, set or frozenset are all False whether their types are the same or different and whether their elements are ordered or unordered as shown below:
v = {'A':'B', 'C':'D', 'E':'F', 'G':'H', 'I':'J'}.values()
print(v)
# dict_values(['B', 'D', 'F', 'H', 'J'])
print(v == {'A':'B', 'C':'D', 'E':'F', 'G':'H', 'I':'J'}.values()) # dict.values()
print(v == ['B', 'D', 'F', 'H', 'J']) # list
print(v == ('B', 'D', 'F', 'H', 'J')) # tuple
print(v == {'B', 'D', 'F', 'H', 'J'}) # set
print(v == frozenset(['B', 'D', 'F', 'H', 'J'])) # frozenset
# False
for x in v:
print(x)
# B
# D
# F
# H
# J
CPython versions tested on:
3.12
Operating systems tested on:
No response
Linked PRs
- gh-155858
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
报告提到了 dict.keys()、dict.values() 和 dict.items(),并提供了可执行的相等性示例;首先在 CPython 3.12 上运行这些示例。比较文档所述的相等性行为与已实现的相等性行为,然后使用链接的 PR gh-155858 作为当前工作参考;完成的要求是行为达成一致,并且测试相匹配。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 20/100