Add bound Hashable for types for keys of type Mapping and values of type Set
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 5.1k
- Fork
- 2.1k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 82
Mô tả
Problem
Types used for keys in mappings and values in sets are too broad.
How to reproduce
d: [list, int] = {} # Tools that use typeshed see this code as valid.
d[[1]] = 2 # TypeError: unhashable type: 'list'
Solution
Change type of keys in mappings: _KT = TypeVar("_KT") to _KT = TypeVar("_KT", bound=Hashable)
- Added PR with fixes to stdlib
But I still have questions about how to fix the types in set and frozenset.
-
For set, we using
_T, which is also used for values in the list. Can I change it to_KT? The requirements for the type of values in the set are the same as for the keys in the dictionary. I understand that it is a value, not a key, and the name can be confusing. Do you think of a better option? -
For frozenset, we using covariant
_T_co, which is also used for values in the tuple. It's the same problem here. We need a new type that is covariant and bound to Hashable. -
set
-
frozenset
And once we figure out what to do with types in stdlib, I will be happy to add the same changes to third-party libraries.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách xem xét stdlib/builtins.pyi tại các định nghĩa set và frozenset được issue tham chiếu, cùng với các thay đổi mapping liên quan trong PR 9570. Quyết định cách biểu diễn biến kiểu của set và một biến frozenset đồng biến bị giới hạn bởi Hashable, sau đó cập nhật các stub của stdlib và xác minh rằng các khóa không thể băm hoặc các giá trị set bị các công cụ kiểm tra kiểu liên quan từ chối.
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
- developer-experience
- Loại issue
- Tính năng
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100