Sub topic 9
Extending a TypedDict through inheritance
This chapter assumes you can define a basic TypedDict. You should also know how NotRequired and total=False mark optional keys, and that a type checker such as mypy or pyright checks these annotations separately from running the code.
Why inherit
Records often share fields. Users, articles and orders might all carry an id and a name. Repeating those keys in every TypedDict invites drift. Inheritance lets you declare the shared keys once and add the rest in subclasses.
Basic inheritance
A TypedDict subclass lists another TypedDict as its base. It gets every key of the base, plus any keys you add.
from typing import NotRequired, TypedDict
class Entity(TypedDict):
id: int
name: str
class User(Entity):
email: NotRequired[str]
def greet(user: User) -> str:
return f"Hello, {user['name']} (#{user['id']})"
alice: User = {"id": 1, "name": "Alice"}
print(greet(alice))
print(sorted(User.__required_keys__))
print(sorted(User.__optional_keys__))User requires id and name and allows email. The last two lines show that inherited keys appear in the runtime metadata too.
Multiple bases
A subclass can combine several TypedDict bases. This works well for mixins that hold a group of related keys.
from typing import TypedDict
class Entity(TypedDict):
id: int
name: str
class Timestamps(TypedDict):
created_at: str
updated_at: str
class Article(Entity, Timestamps):
body: str
post: Article = {
"id": 7,
"name": "Intro",
"created_at": "2026-11-26",
"updated_at": "2026-11-26",
"body": "Hello",
}
print(sorted(Article.__annotations__))Inheritance and optional keys
The total argument applies only to the keys declared in the class body where you write it. Inherited keys keep the requirement they had in their own class.
from typing import TypedDict
class Base(TypedDict):
id: int
class Options(Base, total=False):
color: str
a: Options = {"id": 1}
b: Options = {"id": 2, "color": "red"}
print(sorted(Options.__required_keys__), sorted(Options.__optional_keys__))Here id stays required and color is optional. This pattern, a required base plus an optional subclass, is the older way to mix both kinds. NotRequired in a single class is usually clearer today.
Rules to remember
- A subclass may add keys, but it should not redeclare an inherited key with a different type. Checkers report this as an incompatible override.
- Every base must be a
TypedDict(orGeneric). Mixing in an ordinary class, such as a plain class or a dataclass, is an error. - Inheritance is a convenience for declaring keys. Because compatibility is structural, a function that accepts
Entityalso accepts aUservalue. - At runtime a subclass is still a plain
dicttype. There is nosuper()call and no method resolution to reason about, because aTypedDictcannot define methods.
| Mistake | What the checker reports | Fix |
|---|---|---|
| `class Bad(Entity): id: str` | Incompatible override of `id` | Keep `id: int`, or use a different key name |
| `class Bad(Entity, SomeDataclass)` | A TypedDict can only inherit from TypedDicts | Make every base a `TypedDict` |
| `class Opt(Base, total=False)` expected to loosen `id` | Nothing, but `id` is still required | Redeclare nothing; move `id` into an optional-only base |
Exercise
Given class Base(TypedDict): id: int and class Child(Base, total=False): note: str, which keys must a Child value contain, and what does Child.__optional_keys__ hold?
Show answer
A Child value must contain id, because total=False affects only keys declared in Child itself. Child.__optional_keys__ is frozenset({'note'}).