Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 10 · Production idioms (modern Python)typing.Never + assert_never — exhaustiveness checks the compiler enforcespredict147 / 161
+150 XP
Task
📝 **Task:** Predict the 5-line output. A Color StrEnum has 3 variants. `describe_all` is exhaustive (every variant handled); `describe_partial` only handles RED. The trace: describe_all on all 3 variants succeeds and prints 3 lines; describe_partial on RED prints `warm`; describe_partial on GREEN reaches the assert_never branch and raises AssertionError, which the outer try catches. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

from typing import assert_never
from enum import StrEnum


class Color(StrEnum):
    RED = "red"
    GREEN = "green"
    BLUE = "blue"


def describe_all(c: Color) -> str:
    match c:
        case Color.RED: return "warm"
        case Color.GREEN: return "fresh"
        case Color.BLUE: return "cool"
        case _: assert_never(c)


def describe_partial(c: Color) -> str:
    match c:
        case Color.RED: return "warm"
        case _: assert_never(c)


for c in Color:
    print(describe_all(c))

print(describe_partial(Color.RED))

try:
    describe_partial(Color.GREEN)
except Exception as e:
    print(f"{type(e).__name__} raised")

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…