Cannot subclass from Protocol and Enum at the same time.
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description:
x-ref: https://github.com/python/cpython/issues/119946#issuecomment-4418882668
When trying to create an Enum of classes that implement a certain Protocol, an unexpected 'Protocols cannot be instantiated' error is raised.
from enum import Enum, EnumType
from typing import Protocol
class Example(Protocol):
def method(self) -> None: ...
class Impl(Example):
def method(self) -> None: ...
Impl() # OK
class CompatEnumType(type(Protocol), EnumType): ...
class ProtoEnum(Example, Enum, metaclass=CompatEnumType):
impl = Impl() # ERROR 'Protocols cannot be instantiated'
I have debugged this a bit and the issue is that _is_protocol is only set during Protocol.__init_subclass__, but Enum instantiates its members before this method is called.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-149830
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 Protocol.init_subclass và việc tạo các thành viên Enum, sử dụng reproducer trong issue để quan sát thứ tự của chúng. Xem xét PR gh-149830 được liên kết để biết công việc hiện tại. Được xem là hoàn tất khi sự kết hợp giữa Protocol và Enum không còn gây ra lỗi khởi tạo không mong muốn và các bài kiểm thử liên quan đều đạ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
- backend
- 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
- 25/100