python / python/cpython

Inconsistent behaviors of `dict.keys()`, `dict.values()` and `dict.items()` with `==`

オープン
#140,866 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

docs type-bug
主要言語
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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

レポートでは dict.keys()、dict.values()、dict.items() を取り上げ、実行可能な等価性の例を示しています。まず CPython 3.12 で実行してください。文書化された等価性の動作と実装された等価性の動作を比較し、その後、リンクされた PR gh-155858 を現在の作業の参照として使用してください。完了の条件は、合意された動作と、それに対応するテストです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
20/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。