Fill in

Typed Quest Scrolls

JSON-style data often arrives as dicts with fixed keys. A TypedDict lists those keys and the type of each value:

from typing import TypedDict

class Lantern(TypedDict):
    island: str
    fuel: int

lamp: Lantern = {"island": "Reef", "fuel": 3}

At runtime lamp is just a normal dict, the class only teaches type checkers the shape, so they can flag lamp["fule"] typos.

Literal

Literal limits a value to a few exact options:

from typing import Literal
Direction = Literal["north", "south"]   # a type alias

A checker will now reject "nrth". Way better than "any string"!

Assignment

Replace each ___:

  1. A Quest has a name (str), a reward (int) and a status that is the Status literal.
  2. complete returns a copy of the quest with status "done".
  3. open_rewards adds up the rewards of quests whose status is "open".