Type hinting the Color class should work:

def get_color_return_something(some_color: Color):
    print(some_color.value)
Answer from ibarrond on Stack Overflow
🌐
Python
typing.python.org › en › latest › spec › enums.html
Enumerations — typing documentation
Within a type stub, members can ... be used: class Pet(Enum): genus: str # Non-member attribute species: str # Non-member attribute CAT = 1 # Member attribute with known value and type DOG = cast(int, ...) # Member attribute with unknown value and known type BIRD = ......
Top answer
1 of 6
129

Type hinting the Color class should work:

def get_color_return_something(some_color: Color):
    print(some_color.value)
2 of 6
24

You can try to use an option with type hint Literal.

From official PEP8 documentation we know that:

Literal it's type that can be used to indicate to type checkers that the corresponding variable or function parameter has a value equivalent to the provided literal (or one of several literals)

So in case if you need to use some specific values for a function argument it will be one of the best options. But this approach will not work fully as we expected, because of the type of the Enum values. Each value will have a type of Enum class. This means that for the code example below we will be able to put Color.GREEN as the function argument. So such a solution will be just information for developers, but not a mandatory rule for a function argument.

class Color(enum.Enum):
    RED = '1'
    BLUE = '2'
    GREEN = '3'

print(type(Color.RED)  # will return <enum 'Color'>

Code example:

from enum import Enum
from typing import Literal


class Color(Enum):
    RED = '1'
    BLUE = '2'
    GREEN = '3'

def some_function(some_color: Literal[Color.RED, Color.BLUE]) -> None:
    pass

The second option it's fully correct solution provided by @ibarrond from the post above with just a class type hint.

some_color: Color

So here you can choose the option to work with depending on your needs.

From my point of view we can try to specify possible Enum values for developers, to be more clear in our requirements for a function.

Discussions

Generic versions of enum.Enum?
(First of all, apologies if this is not the proper place for suggesting this, or if this has already been discussed, I couldn't find anything related to this so I opened an issue here.) So I... More on github.com
🌐 github.com
15
January 28, 2018
How to replicate the enum type hint?
Dumb question, but why not inherit from enum.Enum or its variants in either your base class or here? You can customise the functionality with the methods shown in the documentation. That would enable using the class itself in type annotations. More on reddit.com
🌐 r/learnpython
5
3
July 7, 2024
Draft of typing spec chapter for enums - Typing - Discussions on Python.org
I’ve written a first draft of a new chapter for the typing spec that focuses on type checker behaviors for enumerations. The Enum class has many atypical behaviors, and type checkers need to include special-case logic to handle these behaviors. Not surprisingly, there are a number of differences ... More on discuss.python.org
🌐 discuss.python.org
3
January 18, 2024
Convert enum to Literal type alias in Python typing - Stack Overflow
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 Lit... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python
docs.python.org › 3 › library › enum.html
enum — Support for enumerations
February 23, 2026 - If you do not need/want those limitations, you can either create your own base class by mixing in the int or str type yourself: >>> from enum import Enum >>> class MyIntEnum(int, Enum): ... pass · or you can reassign the appropriate str(), etc., in your enum: >>> from enum import Enum, IntEnum >>> class MyIntEnum(IntEnum): ... __str__ = Enum.__str__ ... © Copyright 2001 Python Software Foundation.
🌐
GeeksforGeeks
geeksforgeeks.org › python › type-hint-enum-in-python
Type Hint Enum in Python - GeeksforGeeks
July 23, 2025 - Type hinting in Python has become a crucial aspect of writing clean and maintainable code. Enums, short for enumerations, are a convenient way to represent a set of named values. Combining type hinting with enums enhances code readability and ...
🌐
GitHub
github.com › python › typing › issues › 535
Generic versions of enum.Enum? · Issue #535 · python/typing
January 28, 2018 - Analyzing this with mypy highlights the value of MyEnum.a.value as Any. That makes sense, as enum member values could be of any type. However, in this case I know that all members of my enum should have int values, and no other type. In general, in most cases where I have used an enum the values have all been of a single type.
Author   bugreport1234
🌐
Medium
medium.com › @jaberi.mohamedhabib › understanding-enums-and-classes-in-python-db4a7dbf32e0
Understanding Enums and Classes in Python | by JABERI Mohamed Habib | Medium
March 3, 2024 - ... Readability: Enums improve code readability by replacing magic numbers or strings with meaningful names. Type Safety: Enums provide type safety, helping to catch potential errors early in development.
Find elsewhere
🌐
Andrewfavia
andrewfavia.dev › posts › type-hinting-and-enums
Andrew Favia | Type Hinting Generic Enum Factories
We get a little complain on the double assignment in the function. We can # type: ignore that, or, we can opt to raise an exception instead of returning a 0 (1 lol) value: def to_mtg(e: Enum) -> MagicCardType: if isinstance(e, MagicCardType): a: MagicCardType = e else: raise TypeError("Wrong Enum Passed!
🌐
Python documentation
docs.python.org › 3 › howto › enum.html
Enum HOWTO — Python 3.14.3 documentation
An Enum is a set of symbolic names bound to unique values. They are similar to global variables, but they offer a more useful repr(), grouping, type-safety, and a few other features. They are most ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › enum-in-python
enum in Python - GeeksforGeeks
December 11, 2025 - Enums are created by defining a class that inherits from the Enum class. Each attribute inside the class represents a unique, named constant with a specific value.
🌐
Reddit
reddit.com › r/learnpython › how to replicate the enum type hint?
r/learnpython on Reddit: How to replicate the enum type hint?
July 7, 2024 -

I made a Custom enum Base class that fixes some issues I had with the normal enum, mainly the nesting of enums to represent more complex strucures. It also makes the code a lot more maintainable.

What I wanted to know is how I can replicate the Enum type hint so that instead of security_level: Security.BASIC | Security.AVERAGE | ... I can just do security_level: Security. Aswell as an easier way to check compliance like isinstance (of course you can just check the base class but I want to know if this can be solved with proper type hinting too).

Example:

from ..data import EANEnum as _EANEnum


class Security(_EANEnum):  # Changed to indices for easy selection from iterables
    """Baseclass for different security levels"""
    BASIC = 0, "An attacker can reverse whatever if they have enough info on you pretty easily"
    AVERAGE = 1, "A lot better than basic"
    STRONG = 2, "Practically impossible to reverse or crack"
    SUPER_STRONG = 3, "Great security, but at the cost of comfort features like readability and efficiency"

Implementation: https://pastebin.com/p2CVSNJP

🌐
Mypy
mypy.readthedocs.io › en › stable › literal_types.html
Literal types and Enums - mypy 1.20.0 documentation
def choose_direction(direction: Direction) -> None: if direction == Direction.up: print('Going up!') return assert_never(direction) # E: Argument 1 to "assert_never" has incompatible type "Direction"; expected "NoReturn" Exhaustiveness checking is also supported for match statements (Python 3.10 and later). For match statements specifically, inexhaustive matches can be caught without needing to use assert_never by using --enable-error-code exhaustive-match. Mypy also tries to support special features of Enum the same way Python’s runtime does:
🌐
Medium
medium.com › @glasshost › how-to-type-hint-enums-in-python-18b45d23d67b
How to Type Hint Enums in Python | by Glasshost | Medium
April 12, 2023 - To use Enums in Python, you need to import the Enum class from the enum library. You can also import the IntEnum class if you want to use integer-based Enums. ... To define an Enum, you can create a class that inherits from the Enum class.
🌐
Python.org
discuss.python.org › typing
Draft of typing spec chapter for enums - Typing - Discussions on Python.org
January 18, 2024 - I’ve written a first draft of a new chapter for the typing spec that focuses on type checker behaviors for enumerations. The Enum class has many atypical behaviors, and type checkers need to include special-case logic t…
Top answer
1 of 2
16

I'm afraid there's no such way. The first thing that comes to mind is iterating over enum's values to build a Literal type won't work, because Literal cannot contain arbitrary expressions. So, you cannot specify it explicitly:

# THIS DOES NOT WORK
def is_enabled(state: State | Literal[State.ENABLED.value, State.DISABLED.value]) -> bool:
    ...

There's an open issue on GitHub with the related discussion. Basically, you could have hardcoded literals for every Enum, but in that case, you need to update it in accordance with Enum updates, and one day it will be messy. So, I would either stick with State | str annotation or just State and expect your function to accept only enums.

Also, take into account that you do not need to explicitly create an Enum object to test its value, you can just write "enabled" == State.ENABLED as I mentioned in the comments.

2 of 2
1

It's very annoying that you can't easily create a Literal type hint for all the members of an enum, but one potential workaround if you have control of the enum (and can programmatically determine what the member name should be from the value) is to reverse the direction

Instead of creating a Literal type hint from the enum members, you can create an enum from the values in a Literal type hint using the functional API for Enum and typing.get_args

Your example would look like:

from enum import Enum
from typing import Literal, get_args

# variable naming might not be great here,
# but I figure it's good enough for the short example
StateValues = Literal["enabled", "disabled"]
State = Enum(
    "State",
    ((state.upper(), state) for state in get_args(StateValues))
    module="...",  # fill in module and qualname to help with pickling
    qualname="...",
)

def is_enabled(state: State | StateValues) -> bool:
    return state is State.ENABLED or state == State.ENABLED.value

Huge downside to this approach is that now static analysis tools will flag State.ENABLED as an unknown attribute (and you lose the ability to put individual enum members in type hints or do exhaustiveness checking on enum members)

The functional API won't let you do everything that the class API does, but if all you need are the members to be defined, then this should work


Overall, this approach probably isn't worth it

🌐
Typer
typer.tiangolo.com › tutorial › parameter-types › enum
Enum - Choices - Typer
To define a CLI parameter that can take a value from a predefined set of values you can use a standard Python enum.Enum: ... from enum import Enum import typer class NeuralNetwork(str, Enum): simple = "simple" conv = "conv" lstm = "lstm" app = typer.Typer() @app.command() def main(network: NeuralNetwork = NeuralNetwork.simple): print(f"Training neural network of type: {network.value}") if __name__ == "__main__": app()
🌐
Python.org
discuss.python.org › python help
How do I get static code analysis type support for enums (not just runtime support)? - Python Help - Discussions on Python.org
July 23, 2022 - I’m going through the FastAPI documentation tutorial and there’s a section where I create an enum for a path parameter (Path Parameters - FastAPI). My code ends up looking like this when I copy and paste it from the page: from enum import Enum from fastapi import FastAPI class ModelName(str, Enum): alexnet = "alexnet" resnet = "resnet" lenet = "lenet" app = FastAPI() @app.get("/models/{model_name}") async def get_model(model_name: ModelName): if model_name == ModelName.al...
🌐
3D Bay
clouddevs.com › home › python guides › enumeration definition with python enum type
Python Enums - Enumeration Types
October 26, 2023 - Python’s Enumerations, or Enums for short, provide a way to define a fixed set of named values, also known as symbolic constants. Enums have been available since Python 3.4 and are widely used in Python codebases to make code more readable and maintainable.