python / python/cpython

Cannot subclass from Protocol and Enum at the same time.

未關閉
#149,828 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib type-bug
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 Protocol.init_subclass 和 Enum 成員建立開始,使用 issue 中的重現器觀察它們的順序。查看連結的 PR gh-149830 以了解目前的工作。完成的標準是 Protocol 和 Enum 的組合不再引發非預期的具現化錯誤,且相關測試通過。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。