Set dunder operators too permissive?
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ả
Similar to #1840
With #3181 I made KeysView consistent with Set but I really think the operators are confusingly permissive.
Let's look at them
# difference
{'1'} - {2}
reveal_type({'1'} - {2}) # => Set[str]
This should fail because this is always a null operation and thus
a programmer error.
# intersection
{'1'} & {2} # => Set[str]
reveal_type({'1'} & {2}) # => Set[str]
This really should fail because it is unlikely meaningful; the
return type is always an empty set thus it is also not a Set[str]
# sym difference
{'1'} ^ {2}
reveal_type({'1'} ^ {2}) # => Set[Union[str, int]]
Although the assumed union type is correct, Set[A] & Set[B] => Set[A|B] just doesn't make sense. The operator can only meaningful be applied to same type sets. What we do here is just a form of plain concat or union.
# union
{'1'} | {2} # MAYBE OK
reveal_type({'1'} | {2}) # => Set[Union[str, int]]
If we look at Sets as just containers without some
properties, we could accept it to construct mixed
type Sets using 'union'. I think it is okay for Python,
the name union makes it meaningful, but I maybe never did
that in practice.
Since it adds to the type it is also likely that
the following part of the program catches mistakes.
(But lists don't allow that [1] + ['2'] although they're really
arbitrary containers. Except my PR #3183.)
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 với các ví dụ của issue này và so sánh các cuộc thảo luận liên quan trong #1840, #3181 và #3183. Xác định những toán tử Set và KeysView nào nên từ chối các kiểu phần tử hỗn hợp, bao gồm cả việc union có tiếp tục được phép hay không. Công việc được hoàn thành khi dự án có một quy tắc đã thống nhất cho từng toán tử và hành vi typing liên quan phản ánh quyết định đó.
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
- tooling
- Loại issue
- Tính năng
- Độ 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
- Khá rõ ràng
- Mức phù hợp với người mới
- 25/100