Skip to content

Typing for enums of primitive Python types #1521

Closed

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'?

Activity

  1. added
    topic: featureDiscussions about new features for Python's type annotations
    on Dec 4, 2023
  2. locked and limited conversation to collaborators on Dec 4, 2023
  3. converted this issue into a discussion #1522 on Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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