[Python] How to define a immutable ClassVar in a python protocol?

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I have an Enum and several classes using that Enum in an immutable class variable.

    from typing import ClassVar, Protocol, Final, Literal
    from enum import Enum

    class MyEnum(Enum):
    A = 0
    B = 1

    class MyClass1:
    type: Final = MyEnum.A

    class MyClass2:
    type: Final = MyEnum.B


    This works fine but I fail to define a protocol encompassing the two classes. Both:

    class MyProtocol1(Protocol):
    @property
    def type(self) -> MyEnum: ...

    class MyProtocol2(Protocol):
    type: Final[MyEnum]


    do not work for obvious reasons.

    class MyProtocol3(Protocol):
    @property
    def type(self) -> ClassVar[MyEnum]: ...

    class MyProtocol4(Protocol):
    @property
    @classmethod
    def type(cls) -> MyEnum: ...


    might achieve what I want but are not allowed. I thought about using a Generic with Literal, which also led nowhere. I am running out of ideas.

    Continue reading...

Compartilhe esta Página