Polyfactory Documentation

repository·main·Indexed 23 days ago

https://github.com/litestar-org/polyfactory

A mock data generation library that leverages Python type hints to create realistic data for dataclasses, Pydantic models, TypedDicts, msgspec structs, Odmantic models, and Beanie ODM models. Successor to pydantic-factories, it provides specialized factories like DataclassFactory and MsgspecFactory to automatically generate mock objects via the .build() method.

Tokens
7.7K
Snippets
7
Records
80
Agent score
70%

What's inside polyfactory

  1. What is Polyfactory

    main

    Polyfactory is a mock data generation library that uses Python type hints to automatically generate data. It supports various data structures including:

    • Dataclasses
    • TypedDicts
    • Pydantic models
    • msgspec structs
    • And more.

    It is part of the Litestar project and is actively maintained by the community.

  2. Use specialized Polyfactory base factories

    main

    Polyfactory provides specialized factory classes for different data models. To create a factory, you must inherit from the base factory corresponding to your data model type.

    Note that while core factories are available in polyfactory.factories, specialized factories (like those for Pydantic or SQLAlchemy) are namespaced to avoid requiring unnecessary dependencies. You should import them from their specific modules.

    Available base factories include:

    • DataclassFactory: For Python dataclasses.
    • TypedDictFactory: For TypedDict objects.
    • ModelFactory: For pydantic models (requires pydantic).
    • BeanieDocumentFactory: For beanie documents.
    • OdmanticModelFactory: For odmantic models.
    • MsgspecFactory: For msgspec Structs.
    • AttrsFactory: For attrs models.
    • SQLAlchemyFactory: For SQLAlchemy models.
  3. Supported model types in polyfactory

    main

    Polyfactory is designed to be model-agnostic and can generate mock data for several different types of Python objects, including:

    • Dataclasses
    • TypedDicts
    • Pydantic models
    • Odmantic models
    • Beanie ODM models
    • Custom factories
  4. Configure SQLAlchemy relationships in SQLAlchemyFactory

    main

    By default, __set_relationships__ is set to True. When enabled, all fields defined with SQLAlchemy relationship() will be included in the object created by the build() method.

    Note: If __set_relationships__ is True, ForeignKey fields associated with the relationship are automatically generated (because __set_foreign_keys__ is True by default), but their values will be overwritten by the SQLAlchemy ORM when using create_sync or create_async methods.

  5. Configure SQLAlchemy Association Proxies in SQLAlchemyFactory

    main

    By default, __set_association_proxy__ is set to True. When enabled, all SQLAlchemy fields mapped to an ORM Association Proxy will be included in the result created by build().

    Note: Polyfactory will create both the association proxy field and its underlying relationship, but the relationship field will eventually be overwritten by the SQLAlchemy ORM when using create_sync or create_async methods.

  6. Understand the relationship between Polyfactory and Pydantic-Factories

    main

    Polyfactory is the successor to pydantic-factories. While the previous version was focused on Pydantic models, Polyfactory (v2.0+) features a redesigned core architecture to support a wider range of data structures. It can generate mock data for:

    • Dataclasses
    • TypedDict
    • Pydantic models
    • odmantic models
    • beanie ODM models
    • Custom factories
  7. Create sub-factories by overriding parent configuration

    main
    You can call create_factory on an existing concrete factory to create a sub-factory. This allows you to inherit the parent factory's configuration while potentially overriding specific aspects. When calling create_factory on an existing factory, you do not need to specify the model argument; it will be inherited from the parent.
  8. Set up the Polyfactory development environment

    main
    To set up a local development environment for Polyfactory, ensure you have uv installed. Use uv sync --all-extras to create a virtual environment and install all necessary dependencies. Additionally, install prek and run prek install to set up the pre-commit hooks used for linting and formatting.