Inconsistent behaviors of `dict.keys()`, `dict.values()` and `dict.items()` with `==`
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 77.2k
- Forks
- 35.9k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
El informe menciona dict.keys(), dict.values() y dict.items(), y proporciona ejemplos ejecutables de igualdad; empieza ejecutándolos en CPython 3.12. Compara el comportamiento de igualdad documentado con el implementado y, después, usa el PR vinculado gh-155858 como referencia de trabajo actual; para darlo por terminado se requiere un comportamiento acordado y pruebas correspondientes.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- backend
- Tipo de issue
- Error
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 20/100