Skip to content

mypy does not recognise from ... import inside an IntEnum class body as producing enum members. #21146

@peturingi

Description

@peturingi

mypy does not recognise from ... import inside an IntEnum class body as producing enum members. It types the members as int instead of the enum type, causing a spurious "int" has no attribute "value" error.
The same code runs correctly at runtime — Python's EnumMeta picks up the imported names as enum members.

from enum import IntEnum

class LogLevel(IntEnum):
    from syslog import (
        LOG_CRIT as CRITICAL,
        LOG_EMERG as EMERGENCY,
        LOG_ERR as ERROR,
        LOG_WARNING as WARNING,
        LOG_INFO as INFO,
        LOG_DEBUG as DEBUG,
    )

print(LogLevel.EMERGENCY.value)

Expected Behavior

No errors. LogLevel.EMERGENCY should be typed as LogLevel (or at minimum as int with .value accessible), consistent with how EnumMeta processes the class body at runtime.

Actual Behavior

demo.py:15: error: "int" has no attribute "value"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions