python / python/cpython

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

Offen
#140,866 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

docs type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Der Bericht nennt dict.keys(), dict.values() und dict.items() und enthält ausführbare Gleichheitsbeispiele; führe sie zunächst auf CPython 3.12 aus. Vergleiche das dokumentierte und das implementierte Gleichheitsverhalten und verwende dann den verknüpften PR gh-155858 als aktuelle Arbeitsreferenz; abgeschlossen ist die Arbeit, wenn ein abgestimmtes Verhalten und passende Tests vorliegen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
20/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.