Wrong Signature for Enum with Specific Dataclass Mixin Scenario
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ả
Note: I was thinking about putting this on pyright but it said standard library problems are more suited here, so I'll give it a shot.
I was trying to code an enum with a dataclass mixin where each value is overridden through the first string variable for easier referencing (rather than a dict from str to enum).
from dataclasses import dataclass
from enum import Enum, auto
@dataclass(frozen=True)
class SampleMixin:
a_string: str
a_number: int
class SampleEnum(SampleMixin, Enum):
def __new__(cls, value: str, *_):
obj = object.__new__(cls)
obj._value_ = value
return obj
foo = "foo", 1
bar = "bar", 2
class SampleEnum2(Enum):
foo = auto()
bar = auto()
baz = SampleEnum("foo")
qux = SampleEnum2("foo")
baz still gets SampleEnum.foo right, but the function signature is incorrectly identified. Ideally, it should be closer to SampleEnum2's function signature.
I'm open to workarounds at the meantime. The code runs correctly, it's just that the inferred signature I got was wrong.
Code sample in pyright playground.
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 bản tái hiện bằng Python và pyright playground được liên kết, đồng thời so sánh các chữ ký được suy luận cho SampleEnum và SampleEnum2. Truy vết các định nghĩa typing liên quan của enum và dataclass trong thư viện chuẩn ở typeshed, rồi xác nhận hoàn tất khi trường hợp dataclass-mixin tạo ra chữ ký hàm khởi tạo enum như mong muốn mà không thay đổi hành vi runtime.
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
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 55/100