Skip to main content

How do I type hint for Enums in Python?

I have a python function for which I want to use type hinting. There are two arguments. The first is any Enum class, the second optional arg is an element of that Enum.

For example, say I have:

class Foo(Enum):
    ALPHA = 1
    BETA = 2
    GAMMA = 3

The first arg would be, e.g. Foo, the second would be e.g. Foo.ALPHA

What would be the correct way of type hinting this? What I have so far is:

def switch(options: Enum, selected: Optional[Enum] = None) -> Enum:
   # Rest of fn...

but that doesn't seem right.



source https://stackoverflow.com/questions/75477485/how-do-i-type-hint-for-enums-in-python

Comments