Add safer types of casts
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 1.8k
- Fork
- 302
- Merge trung bình
- 23 giờ
- Pull request đã merge (30 ngày)
- 8
Mô tả
The only form of cast expression we have is intended to completely bypass the type system. This is like most forms of casting in C (not counting conversions) -- it never fails either at runtime or at compile time.
Maybe we can add two new types of casts:
Downcast
downcast(T, E) checks at compile time that the type of expression E is a supertype of T, and checks at runtime that E is in fact an instance of T. As a special case, if the type of E is Any, the compile time check always succeeds, but the runtime check is still performed.
A possible implementation:
def downcast(type, expr):
assert isinstance(expr, type)
return expr
It's intentional that this uses assert (though debatable): the intended use case is currently handled by inserting the same assert manually. IOW:
x = downcast(type, expr)
is roughly equivalent to:
assert isinstance(expr, type)
x = expr
Upcast
Probably much less needed, but proposed for symmetry and because occasionally it's useful. upcast(T, E) should check at compile time that T is a supertype of the type of E, and at runtime it's a no-op.
A possible implementation:
def upcast(type, expr):
return expr
This fragment:
x = upcast(type, expr)
is roughly equivalent to:
x: type = expr
except that it works even if the type of x has already been declared.
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
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 việc xem xét ngữ nghĩa downcast và upcast được đề xuất trong phần nội dung của issue. Không có tệp, bài kiểm thử hoặc entry point nào được nêu tên; bước đầu tiên là thống nhất một thiết kế, sau đó sẽ cần triển khai và viết các bài kiểm thử.
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
- Cần làm rõ
- Mức phù hợp với người mới
- 30/100