Skip to main content

How to Make a Key Optional in a Python TypedDict with NotRequired

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A TypedDict expects every key it lists, so a dictionary that leaves one out gets an error from your type checker. Some keys really are optional, such as a book's subtitle. NotRequired marks such a key, and this guide uses it on Python 3.15.2, checked with mypy 2.4.1.

Syntax​

Syntax
from typing import NotRequired, TypedDict


class <Name>(TypedDict):
<key>: <type>
<optional_key>: NotRequired[<type>]
PartWhat it means
<Name>The name of the kind of dictionary you're describing
<key>: <type>A key that every such dictionary must have
<optional_key>: NotRequired[<type>]A key that a dictionary may leave out. When it's there, its value must be a <type>

The typing documentation describes NotRequired as a "Special typing construct to mark a TypedDict key as potentially missing." It was "Added in version 3.11".

The Example​

A Book always has a title, and only some books have a subtitle. The script makes two books, one without a subtitle and one with, and then asks Book which keys it requires and which it doesn't.

Prerequisites​

  • uv and Python 3.11 or newer. NotRequired came to the typing module in Python 3.11. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

library.py
from typing import NotRequired, TypedDict


class Book(TypedDict):
title: str
subtitle: NotRequired[str]


dune: Book = {"title": "Dune"}
emma: Book = {"title": "Emma", "subtitle": "A Novel"}

print(dune)
print(emma)
print(Book.__required_keys__)
print(Book.__optional_keys__)

The library.py script in a macOS-style code window: it imports NotRequired and TypedDict, defines a Book with a title of type str and a subtitle of type NotRequired of str, creates Dune without a subtitle and Emma with the subtitle A Novel, and prints both books and the Book's required and optional keys.

  • subtitle: NotRequired[str] says that a book may leave out subtitle, and that when it has one, the subtitle is text.
  • dune has no subtitle, and emma has one. Both are meant to be valid books.
  • __required_keys__ and __optional_keys__ are two attributes that every TypedDict class has. They list its required and its optional keys.

Step 2: Run It​

Terminal
uv run python library.py
Output
{'title': 'Dune'}
{'title': 'Emma', 'subtitle': 'A Novel'}
frozenset({'title'})
frozenset({'subtitle'})

The terminal after uv run python library.py: the Dune book with only a title, the Emma book with a title and the subtitle A Novel, then frozenset with title and frozenset with subtitle.

Both books printed. The last two lines show how Book sees its keys: title is required, and subtitle is optional. A frozenset is a set that can't be changed. The documentation confirms that "keys marked with NotRequired will always appear in __optional_keys__".

Step 3: Check It with a Type Checker​

Python doesn't check type hints when it runs your code, so a missing key never stops the script. A type checker, a program that reads your type hints and reports values that don't match them, is what enforces TypedDict. Check the file with mypy:

Terminal
uvx mypy library.py
Output
Success: no issues found in 1 source file

The terminal after uvx mypy library.py: Success: no issues found in 1 source file, in green.

uvx runs a command from a Python package without adding that package to your project. uv's command reference says that such packages "are installed into an ephemeral virtual environment in the uv cache directory." mypy accepted both books: dune without a subtitle, and emma with one. Both commands in this guide work unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out a key that isn't marked NotRequiredIn a copy of the script with subtitle: str, mypy reported library_strict.py:9: error: Missing key "subtitle" for TypedDict "Book" [typeddict-item] on the dune lineMark the key as NotRequired[str]
Writing subtitle: str | None to make the key optionalmypy still reported Missing key "subtitle" for TypedDict "Book"Use NotRequired[str]. str | None allows the value None, but the key still has to be there
Setting a NotRequired key to Nonemypy reported Incompatible types (expression has type "None", TypedDict item "subtitle" has type "str") [typeddict-item]Leave the key out, or write NotRequired[str | None] if None is a value you really want

FAQ​

Does Python itself reject a book without a required key?​

No. Python doesn't check type hints when the code runs, so with subtitle: str, {"title": "Dune"} still printed {'title': 'Dune'}. Only a type checker, such as mypy or the one in your editor, reports the missing key. That's why Step 3 runs mypy.

What's the difference between NotRequired and str | None?​

They answer different questions. NotRequired is about the key: may it be missing? str | None is about the value: may it be None? The documentation states the default that NotRequired changes: "By default, all keys must be present in a TypedDict."

Conclusion​

subtitle: NotRequired[str] let dune leave out its subtitle while emma kept one, and mypy printed Success: no issues found in 1 source file. Book.__optional_keys__ listed subtitle as the one optional key.

Additional Resources​