(🐞) `overload`s that exhaust finite `Literal`s(`bool`/`Enum`) not treated as exhaustive
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 20.6k
- Fork
- 3.3k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
@overload
def foo(a: Literal[True]) -> int: ...
@overload
def foo(a: Literal[False]) -> str: ...
def foo(a: bool) -> object: ...
a: bool
reveal_type(foo(a)) # error: No overload variant of "foo" matches argument type "bool"
@overload
def foo(a: Literal[True]) -> int: ...
@overload
def foo(a: Literal[False]) -> str: ...
@overload
def foo(a: bool) -> object: ... # no error regarding impossible to match overload
a: bool
reveal_type(foo(a)) # object
@overload
def foo(a: Literal[True, False]) -> int | str: ...
@overload
def foo(a: bool) -> object: ... # error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader
def foo(a: bool) -> object: ...
a: bool
reveal_type(foo(a)) # int | str
Here mypy incorrectly forces us to implement a completely redundant overload for the non Literal case when it is already exhaustively covered by both literals. It is only when the literals are in the same overload (useless in practice, but just for demonstration) that mypy correctly handles this case.
Mypy should be doing 'union math' (or what ever it's called) to apply both literal overloads at once.
This also affects all other exhaustible Literals such as Enums.
This example is pulled directly from the docs, so I think they should be updated as well to an example that doesn't contain this confusing defect.
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 tái hiện các ví dụ overload trong issue và đọc tài liệu Literal types được liên kết. Theo dõi quá trình matching overload và cách mypy xử lý các giá trị Literal và Enum hữu hạn. Được xem là hoàn tất khi các overload Literal riêng biệt bao phủ đầy đủ các trường hợp không còn yêu cầu một overload không phải Literal dư thừa, và ví dụ tài liệu bị ảnh hưởng đã được cập nhật.
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
- devtools
- Loại issue
- Lỗi
- Độ 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
- 38/100