Polyfactory
Polyfactory is a simple and powerful mock data generation library, based around type hints and supporting dataclasses, typed-dicts, pydantic models, msgspec structs and more.
Polyfactory part of the Litestar project and as such actively maintained by a community of maintainers and contributors.
Example
from dataclasses import dataclass
from polyfactory.factories import DataclassFactory
@dataclass
class Person:
name: str
age: float
height: float
weight: float
class PersonFactory(DataclassFactory[Person]):
...
def test_is_person() -> None:
person_instance = PersonFactory.build()
assert isinstance(person_instance, Person)
That's it - with almost no work, we are able to create a mock data object fitting the Person class model definition.
This is possible because of the typing information available on the dataclass, which are used as a source of truth for data generation.
The factory parses the information stored in the dataclass and generates a dictionary of kwargs that are passed to
Person.
Documentation
Usage and API reference documentation is available on https://polyfactory.litestar.dev/.
Installation
pip install polyfactory
Relation to Pydantic-Factories
Prior to version 2, this library was known as pydantic-factories, a name
under which it gained quite a bit of popularity.
A main motivator for the 2.0 release was that we wanted to support more than just Pydantic models, something which also
required a change to its core architecture. As this library would no longer be directly tied to Pydantic, polyfactory
was chosen as its new name to reflect its capabilities; It can generate mock data for dataclasses, typed-dicts,
Pydantic, odmantic, and beanie ODM models, as well as custom factories.
Contributing
This library is a community driven open source project. We welcome and encourage contributions. Please checkout the GitHub issues, read the contribution guide (at the repository's root), and you're always welcome to join our discord server.
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Release files for polyfactory 3.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| polyfactory-3.3.0.tar.gz | 348.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| polyfactory-3.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 411.4 kB
Release files / polyfactory-3.3.0.tar.gz
| Download URL | polyfactory-3.3.0.tar.gz |
|---|---|
| Size | 348.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
237258b6ff43edf362ffd1f68086bb796466f786adfa002b0ac256dbf2246e9a
|
|
BLAKE2b-256 checksum How to use checksums |
85687717bd9e63ed254617a7d3dc9260904fb736d6ea203e58ffddcb186c64e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 22, 2026.
Transparency logRelease files / polyfactory-3.3.0-py3-none-any.whl
| Download URL | polyfactory-3.3.0-py3-none-any.whl |
|---|---|
| Size | 62.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
686abcaa761930d3df87b91e95b26b8d8cb9fdbbbe0b03d5f918acff5c72606e
|
|
BLAKE2b-256 checksum How to use checksums |
dd34b6f19941adcdaf415b5e8a8d577499f5b6a76b59cbae37f9b125a9ffe9f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 22, 2026.
Transparency log