Skip to content

4 - Exhaustiveness checking with mypy

This technique is handy if there’s logic which should explicitly handle all possible values of an enumeration. With the example, mypy would kindly hint that BLUE is unhandled: 17: error: Argument 1 to "assert_never" has incompatible type "Literal[Color.BLUE]"; expected "NoReturn"

In addition to Enums, works great with e.g. Unions and Literals!

Exhaustiveness checking

Read more

This is a widely known trick but I assume the original credits belong to

More on the topic in the mypy docs:

The code
from enum import Enum
from typing import NoReturn

class Color(Enum):
    RED = "RED"
    BLUE = "BLUE"  # I just added this

def handle_color(color: Color) -> None:
    if color is Color.RED:
    elif color is Color.GREEN:

def assert_never(value: NoReturn) -> NoReturn:
    assert False, f"Unknown value: {value}"