Is there a way to type annotate a function or variable in Python in such a way it allows both an enum or Literal formed form the attributes of the enum?
from enum import Enum
from typing import Literal
class State(str, Enum):
ENABLED = "enabled"
DISABLED = "disabled"
def is_enabled(state: State | Literal["enabled", "disabled"]) -> bool:
if isinstance(state, str):
state = State(state)
return state == State.ENABLED
In other words, is there a way to obtain the alias for Literal["enabled", "disabled"] without having to rewrite all the keys of the enum?