polyfactory
Mock data generation factories
What it is and what it does
Polyfactory is a mock data generation library that reads type hints from your data models and automatically generates realistic test objects without manual configuration. It works with dataclasses, Pydantic models, typed-dicts, msgspec structs, and other typed structures, making it straightforward to create test fixtures that match your schema.
You define a factory class that inherits from the appropriate factory type for your model, and then call `.build()` to generate a populated instance. The library uses faker under the hood to produce realistic values (names, addresses, dates, etc.) that respect the types declared in your model. This eliminates boilerplate fixture code and keeps your test data generation in sync with your schema as it evolves.
Use it for:
- Generate realistic test data for unit tests without writing fixture factories by hand for each model.
- Create mock Pydantic or dataclass instances for integration tests with pre-populated fields matching your schema.
- Populate test databases with diverse, type-correct sample data for testing queries and business logic.
- Build mock API responses during development when the real service is unavailable or not yet implemented.
- Generate synthetic data for performance testing or load testing with properly typed, realistic values.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Polyfactory generates mock data objects from type hints, supporting dataclasses, Pydantic models, typed-dicts, msgspec structs, and other typed structures for testing and development.
Yes. Polyfactory is actively maintained, has no known vulnerabilities, low install friction, and solves a real testing problem—eliminating manual fixture boilerplate. It's well-suited for any project using typed Python models that needs realistic mock data for tests.
Install
polyfactory on PyPI
pip
pip install polyfactoryuv
uv add polyfactorypoetry
poetry add polyfactoryInstalling polyfactory
Before you install
Low friction install with only two runtime dependencies (faker and typing-extensions). Actively maintained as part of the Litestar project with recent activity and no known vulnerabilities.
License in practice
MIT license is permissive; you can use, modify, and distribute this package freely in both open-source and commercial projects with minimal restrictions.
Quickstart
pip install polyfactory
from dataclasses import dataclass
from polyfactory.factories import DataclassFactory
@dataclass
class Person:
name: str
age: float
class PersonFactory(DataclassFactory[Person]):
pass
person = PersonFactory.build()
Verify before relying
- Whether the library supports generating data for custom classes beyond the documented types (dataclasses, Pydantic, typed-dicts, msgspec, attrs, SQLAlchemy).
- Performance characteristics when generating large volumes of mock objects or deeply nested structures.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (<4.0,>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — faker, typing-extensions |
| Maintenance | actively maintained — 173 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 11,299,823/month — #1,401 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: polyfactory-3.3.0-py3-none-any.whl
Keywords: attrs, dataclasses, msgspec, pydantic, sqlalchemy
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
pydantic-factoriesGenerates mock data objects that conform to…
permissive · top 15,000 on PyPI
datamodel-code-generatorGenerates Python data models (Pydantic,…
permissive · top 5,000 on PyPI
django-cloneDuplicates Django model instances with…
permissive · top 15,000 on PyPI
coqpitCoqpit provides configuration management…
unclear · top 15,000 on PyPI
dataclass-factoryConverts dataclasses to and from dictionaries…
permissive · top 15,000 on PyPI
dataclasses-jsonschemaGenerates JSON Schema from Python dataclasses…
permissive · top 15,000 on PyPI
dydanticDynamically generates Pydantic models from JSON…
permissive · top 5,000 on PyPI
jsonschema-pydanticConverts JSON Schema definitions into Pydantic…
unclear · top 15,000 on PyPI
desertDesert generates marshmallow serialization…
permissive · top 15,000 on PyPI
wagtail-factoriesProvides factory-boy classes for generating…
permissive · top 15,000 on PyPI