python / python/cpython

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

Đang mở
#140,866 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

docs type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Báo cáo đề cập đến dict.keys(), dict.values() và dict.items(), đồng thời cung cấp các ví dụ có thể thực thi về phép so sánh bằng; trước tiên hãy chạy chúng trên CPython 3.12. So sánh hành vi so sánh bằng được ghi lại trong tài liệu với hành vi đã được triển khai, sau đó sử dụng PR được liên kết gh-155858 làm tham chiếu công việc hiện tại; để hoàn thành cần có hành vi được thống nhất và các bài kiểm thử tương ứng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
20/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.