Support enum member `value` types
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ả
Not sure if this would be in mypy or the typeshed, though it may need support in both.
Currently enum.IntEnum provides a way to have an enum whose value is an int, however all other enum.Enums have a .value of Any.
While this can be worked around in more recent versions of Python by adding a member without a value within the enum:
class MyEnum(str, enum.Enum):
value: str
FIRST = 'first'
This isn't ideal as it means that there are two places where this needs to be specified -- as an inherited type and within the class body.
It feels like it would be better if mypy was able to infer the member value type from the inheritance.
Aside: had generics existed before Enum, it feels like that might alternatively solved this, something like:
class MyEnum(enum.Enum[str]):
FIRST = 'first'
Edit:
Note that this issue is about the general use-case of variables typed for the enum (such as x: MyEnum) -- mypy has long supported determining the type of specific members and their .values (e.g: reveal_type(MyEnum.FIRST.value) is already str or more recently Literal['first']).
I've put a more complete demo of this at https://gist.github.com/PeterJCLaw/d4334e498ab1391ee306886748e675f1.
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 bản demo đầy đủ của issue và lần theo cách xử lý enum trong mypy và typeshed; issue không xác định các tệp hoặc bài kiểm thử cụ thể. Xác định cách suy luận kiểu giá trị của một biến enum từ tính kế thừa, sau đó bổ sung độ bao phủ cho thấy các instance enum tổng quát cung cấp kiểu giá trị mong muốn mà không cần chú thích riêng trong phần thân lớp.
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
- 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
- 30/100