python / python/cpython

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

Aperta
#140,866 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

docs type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Il report cita dict.keys(), dict.values() e dict.items() e fornisce esempi eseguibili di uguaglianza; inizia eseguendoli su CPython 3.12. Confronta il comportamento di uguaglianza documentato con quello implementato, quindi usa la PR collegata gh-155858 come riferimento di lavoro corrente; per considerare il lavoro completato sono necessari un comportamento concordato e test corrispondenti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
20/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.