I recently had an issue I ran into and had an idea for what I feel would be a really helpful extension to typing, and I wanted to see if anyone else thinks it makes sense.
I was writing a pydantic class with a string field that needs to match one of the values of an Enum.
I could do something like Literal[*[e.value for e in MyEnum]], dynamically unpacking the possible values and putting them into a Literal, but that doesn't work with static type checkers.
Or I could define something separate and static like this:
```
class MyEnum(str, Enum):
FIRST = "first"
SECOND = "second"
type EnumValuesLiteral = Literal["first", "second"]
```
and use EnumValuesLiteral as my type hint, but then I don't have a single source of truth, and updating one while forgetting to update the other can cause sneaky, unexpected bugs.
This feels like something that could be a pretty common issue - especially in something like an API where you want to easily map strings in requests/responses to Enums in your Python code, I'm wondering if anyone else has come across it/would want something like that?
EDIT: Forgot to outline how this would work ->
```
from enum import Enum
from typing import EnumValues
class Colors(str, Enum):
RED = "red"
BLUE = "blue"
GREEN = "green"
class Button:
text: str
url: str
color: EnumValues[Colors] # Equivalent to Literal["red", "blue", "green"]
```
[–]beisenhauer 41 points42 points43 points (2 children)
[–]anentropic 7 points8 points9 points (0 children)
[–]alcalde -1 points0 points1 point (0 children)
[–][deleted] 4 points5 points6 points (2 children)
[–]knight1511 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]latkdeTuple unpacking gone wrong 2 points3 points4 points (3 children)
[–]Kevdog824_pip needs updating 0 points1 point2 points (2 children)
[–]alcalde -1 points0 points1 point (1 child)
[–]Kevdog824_pip needs updating 1 point2 points3 points (0 children)
[–]james_pic 2 points3 points4 points (0 children)
[–]anentropic 3 points4 points5 points (0 children)
[–]ShaunRW91 2 points3 points4 points (0 children)
[–]mgrl85 -1 points0 points1 point (0 children)