Skip to content

Typing for enums of primitive Python types #1521

@tokarenko

Description

@tokarenko

It seems that there is no way to create error-free type for Enums of primitive Python types. E.g., the following code leads to the following error: 'Item "str" of "Literal['x32'] | IWordSizeEnum32" has no attribute "value" Mypy[union-attr]'

import enum
from typing import Literal, Protocol

class IWordSizeEnum(Protocol):
    @enum.property
    def x32(self) -> Literal["x32"]:
        ...

    @enum.property
    def x64(self) -> Literal["x64"]:
        ...

    @property
    def name(self) -> str:
        ...

    @property
    def value(self) -> str:
        ...

def type_check_word_size_enum(word_size_enum: IWordSizeEnum):
    word_size_enum.x32.value

Should 'Literal' add '.value' and '.name' for a 'enum.property'?

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions