-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
mypy does not recognise from ... import inside an IntEnum class body as producing enum members. #21146
Copy link
Copy link
Open
Labels
featuretopic-enumtopic-runtime-semanticsmypy doesn't model runtime semantics correctlymypy doesn't model runtime semantics correctly
Description
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)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
featuretopic-enumtopic-runtime-semanticsmypy doesn't model runtime semantics correctlymypy doesn't model runtime semantics correctly