Skip to content

bagof.factories.typeddicts

Factory for TypedDict types.

Classes

TypedDictFactory

Bases: MappingFactory

Factory for TypedDict subclasses.

Builds a dict populated with a value for each required key, built recursively from the key's annotation. Optional keys (from total=False or NotRequired) are omitted, so the result is the minimal valid instance.

A plain dict is not a TypedDict, so it keeps using DictFactory: the registry matches dict to its exact key, which wins over the TypedDict entry.

Example

>>> import typing_extensions as tx
>>> from bagof.factories import get_factory
>>> class Movie(tx.TypedDict):
...     title: str
...     year: int
>>> factory = get_factory(Movie)
>>> factory
TypedDictFactory(<class '__main__.Movie'>)
>>> factory()
{'title': '', 'year': 0}

Methods:

__call__
__call__() -> Any

Build a dict of the required keys, each from its annotation.