Proposal: LiteralEnum — runtime literals with static exhaustiveness

I feel like it would be nice to be able to just Unpack an into a Literal. Where the unpacked values must be marked as Final

Methods = ('GET', 'POST', 'PATCH', 'DELETE')
Method = Literal[*Methods] # Type Error

Methods: Final = ('GET', 'POST', 'PATCH', 'DELETE')
Method = Literal[*Methods] # Ok

I don’t really see a clean way of combining Literals and Enums though, since an Enum requires the members to be valid variable names like you said.

You could also just have a new LiteralEnum abstract that enforces that all literal values are mapped to enum names:

class MethodEnum(LiteralEnum[Method]): # Type Error (Missing 'DELETE')
    get='GET'
    post='POST'
    patch='PATCH'
1 Like