1.TypedDict
TypedDict declares the exact keys a dict should have and the type of each value — for when a dict is really "a record with a fixed shape," not an arbitrary mapping. The result is still a plain dict at runtime.
Key features
- class Movie(TypedDict): title: str; year: int declares a dict shape with two required keys
- A TypedDict value is a genuine, ordinary dict at runtime — isinstance(m, dict) is True
- A type checker verifies the keys and value types match; Python itself does not
- total=False marks all keys optional; NotRequired[T] (3.11+) marks one key optional
- Unlike a dataclass: TypedDict has no __init__, no methods, no defaults — just a shape
Command
from typing import TypedDict
class Movie(TypedDict):
title: str
year: int
m: Movie = {"title": "Arrival", "year": 2016}TypedDict declares a dict's expected keys and value types for a type checker. The result is a plain dict at runtime — nothing is validated when it runs.