Sub topic 9
Extending a TypedDict through inheritance
This chapter covers how one TypedDict extends another with class inheritance. It also covers how inheritance interacts with total, why overriding a key usually fails, and what inheritance does at runtime. It assumes you can define a basic TypedDict, you know total=False, Required and NotRequired, and you know a static checker such as mypy or pyright reads the annotations separately from running the code.
Why extend a TypedDict
Dictionaries with related shapes often share keys. An API might return id and name for every resource, and then add price for products and author for books. Copying the shared keys into each TypedDict means a change to id has to be made in several places, and the copies can drift apart.
Inheritance lets you declare the shared keys once. The child TypedDict contains every key of its parent plus the keys it declares itself.
from typing import TypedDict
class Entity(TypedDict):
id: int
name: str
class Product(Entity):
price: float
class Book(Product):
author: str
book: Book = {"id": 1, "name": "Dune", "price": 9.99, "author": "Frank Herbert"}
print(sorted(book))A Book needs all four keys. Leaving out id is an error even though id is declared in Entity and not in Book.
Inheritance and subtyping
A child TypedDict is a subtype of its parent. A function that accepts an Entity accepts a Product or a Book too, because those values have every key the function might read.
from typing import TypedDict
class Entity(TypedDict):
id: int
name: str
class Product(Entity):
price: float
def label(entity: Entity) -> str:
return f"#{entity['id']} {entity['name']}"
product: Product = {"id": 7, "name": "Lamp", "price": 24.5}
print(label(product))The reverse is rejected. A function that needs a Product cannot take an Entity, because price might be missing. This direction is useful for writing helpers against the smallest shape they need.
Inheritance is not the only way to get this behavior. A TypedDict is checked structurally, so a separate TypedDict with the same keys is also compatible. A later chapter covers those rules. Inheritance mainly saves you from repeating keys and documents the relationship between shapes.
Inheritance combined with total
The total argument applies only to the keys declared in the class body where you write it. Keys inherited from a parent keep whatever requiredness the parent gave them.
This gives a common pattern for mixing required and optional keys. Put the required keys in a base class, and the optional keys in a child with total=False.
from typing import TypedDict
class UserBase(TypedDict):
id: int
name: str
class User(UserBase, total=False):
email: str
bio: str
print(sorted(User.__required_keys__))
print(sorted(User.__optional_keys__))This prints ['id', 'name'] and then ['bio', 'email']. The child's total=False makes email and bio optional and leaves id and name required.
A frequent mistake is expecting total=False on a child to loosen the parent's keys.
from typing import TypedDict
class Base(TypedDict):
id: int
class Child(Base, total=False):
note: str
child: Child = {"note": "hi"} # error: missing required key "id"The checker reports the missing id. To make an inherited key optional, you have to redeclare it, which runs into the override rules below. It is usually cleaner to split the shape into a required base and an optional child, or to use NotRequired on individual keys in the class where they first appear.
from typing import NotRequired, TypedDict
class Entity(TypedDict):
id: int
name: str
class Contact(Entity):
email: NotRequired[str]
phone: NotRequired[str]
contact: Contact = {"id": 3, "name": "Ana", "phone": "555-0100"}
print(contact)This version needs only one child class, and the requiredness of each key is visible on the line that declares it. The explicit markers also work when the child sets total=False and you want one key to stay required: use Required[...] on that key.
Multiple inheritance
A TypedDict can inherit from several TypedDicts. The result contains the keys of all its parents. This works well for small mixins, each describing one concern.
from typing import TypedDict
class Named(TypedDict):
name: str
class Timestamped(TypedDict):
created_at: str
updated_at: str
class Article(Named, Timestamped):
body: str
article: Article = {
"name": "Intro",
"created_at": "2026-01-01",
"updated_at": "2026-01-02",
"body": "Hello",
}
def show_name(item: Named) -> str:
return item["name"]
print(show_name(article))Article is a subtype of both Named and Timestamped, so either helper function accepts it.
If two parents declare the same key, the declarations must agree. Checkers accept identical types and reject conflicting ones. Mypy reports a conflicting case as an error on the child class.
Restrictions on bases
The base classes of a TypedDict have to be TypedDicts. You cannot mix in an ordinary class, and you cannot add methods to the body. The class body holds only annotations and an optional docstring. If you need behavior, write functions that take the TypedDict as an argument, or choose a dataclass (see the last chapter).
There is one exception: a TypedDict can also be generic. A generic TypedDict lists Generic[T] among its bases (or uses the Python 3.12 class Box[T](TypedDict) syntax). That is a way to parameterize the shape, not a way to mix in behavior.
Overriding keys
Redeclaring an inherited key with a different type is rejected by type checkers.
from typing import TypedDict
class Base(TypedDict):
id: int
kind: str
class Child(Base):
kind: int # error: incompatible override of "kind"The reason is substitution. A Child must work anywhere a Base is expected, and code that holds a Base may assign any str to kind. If Child narrowed kind to a smaller type, that assignment could break the child.
This matters when you want a shared base with a generic kind: str and children that narrow it to Literal["circle"] and so on. A mutable key cannot be narrowed that way. Since Python 3.13 (or with typing_extensions on older versions), you can declare the base key as ReadOnly[str]. A read-only key promises that nobody will assign to it through the base type, so a child may narrow it.
from typing import Literal, ReadOnly, TypedDict
class Shape(TypedDict):
kind: ReadOnly[str]
class Circle(Shape):
kind: ReadOnly[Literal["circle"]]
radius: floatFor most code the simpler choice is to skip the shared kind in the base. Declare kind separately in each member of the tagged union, as in the earlier chapter on tagged unions, and put only the genuinely shared keys in the base.
What happens at runtime
At runtime a TypedDict class is a special object, and its instances are ordinary dicts. Inheritance does not change that. The class records the merged annotations and the key sets, which you can inspect.
from typing import TypedDict, get_type_hints
class Entity(TypedDict):
id: int
name: str
class Product(Entity):
price: float
p = Product(id=1, name="Pen", price=1.5)
print(type(p))
print(sorted(get_type_hints(Product)))
print(sorted(Product.__required_keys__))
try:
isinstance(p, Entity)
except TypeError as error:
print(error)The hints include the inherited keys, and type(p) is plain dict. The isinstance call raises a TypeError, because a dict value does not remember which TypedDict it was meant to be. The same applies to issubclass. Child classes also do not validate anything. Product(id="x", name=1, price="y") builds a dict without complaint, and only a type checker flags it.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Checker says an inherited key is missing | `total=False` on the child only affects the child's own keys | Put optional keys in the child and required keys in the parent, or use `NotRequired` |
| Error on redeclared key | A child changed the type of an inherited key | Remove the redeclaration, restructure the base, or use `ReadOnly` in the base |
| Error on the class definition | A base is an ordinary class, or the body has a method | Use only TypedDict bases and annotations, and write separate functions |
| `TypeError` from `isinstance` | TypedDict classes do not support runtime class checks | Test for keys or a `Literal` tag instead |
Exercise
Given class Base(TypedDict): id: int and class Child(Base, total=False): note: str, which keys does a Child value have to contain, and how would you make id optional without redefining Base?
Show answer
A Child must contain id, because inherited keys keep the requiredness from Base, and total=False only affects note. To make id optional, Base itself would have to declare it with NotRequired[int], or you would define a separate TypedDict that does not inherit from Base. Redeclaring id in Child is not the answer. That is an override, and checkers restrict it.
Exercise
A function takes Named (a TypedDict with name: str). You pass it an Article that inherits from Named and adds body: str. Does the checker accept the call, and does the reverse (passing a Named to a function that needs an Article) work?
Show answer
The call is accepted, because Article is a subtype of Named and has every key the function can read. The reverse is rejected, because a Named value might lack the body key that the function expects.