polyfactory Python mock data factories for typed models
Simple and powerful factories for mock data generation
At a glance
- What is it?
- polyfactory generates mock data through factories, with optional integrations for Pydantic, msgspec, SQLAlchemy, ODMantic, and Beanie. It suits developers who need typed fixtures without repeating model construction across tests.
- Who is it for?
- Adopt polyfactory when tests need model-shaped mock objects and your chosen model integration is documented. Do not choose it for persistent database seeding or domain-specific records without verifying its output first.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository received new commits within the last day.
- What is it written in?
- Mainly Python, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on October 24, 2026, and from our analysis. They are not legal advice.
Star history
- litestar-org/polyfactory1,517 ★ · +2
| Repository | 2026-10-16 | 2026-10-24 |
|---|---|---|
| litestar-org/polyfactory | 1.5k | 1.5k |
2 Stars in the last 8 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
Why polyfactory belongs in typed Python tests
polyfactory addresses the repetitive work involved in constructing Python objects for tests. Developers often need fixtures that match a Pydantic model, dataclass, msgspec type, SQLAlchemy entity, or document model. Writing those constructors by hand is straightforward for one model, but copied fixture code becomes expensive when the same values and relationships are needed throughout a test suite.
The repository description identifies polyfactory as a library for mock data generation through factories. Its package metadata classifies the software as a library for testing, development, and utilities. This positioning makes the appropriate audience clear: developers and test authors who want generated objects to conform to a project's declared Python types.
The value is narrower than arbitrary test-data generation. polyfactory is intended to prepare fixtures, not execute tests, assert application behavior, or persist records. This separation lets it sit inside a unit-test setup and hand a constructed object to the function under examination. The repository does not document domain-specific realism or production-like value distributions, so generated fixtures should not automatically be treated as realistic sample records.
How polyfactory connects model fields to generated values
The repository contains a main `polyfactory/` package, a `tests/` directory, documentation under `docs/`, and package configuration in `pyproject.toml`. Its core dependencies are `faker>=5.0.0` and `typing-extensions>=4.6.0`. Together, these details indicate that value generation and Python type handling form the foundation of the library.
The package metadata also declares optional dependencies for `sqlalchemy`, `pydantic`, `msgspec`, `odmantic`, and `beanie`. The project keywords additionally include dataclasses and attrs. These entries show that polyfactory is organized around compatibility with typed model libraries rather than a single database framework. The exact field-processing path is not visible in the README, so the documentation should be consulted for details such as annotation resolution, constrained fields, defaults, aliases, and nested models.
Do not infer identical semantics for every integration. A generated Pydantic object, SQLAlchemy model, msgspec structure, and Beanie document may have different construction and persistence requirements. Polyfactory's shared factory concept can reduce repeated setup, but the model-specific API remains an adoption boundary. In particular, the README does not establish guarantees for uniqueness, deterministic seeds, relationship handling, or automatic validation of cross-field business rules.
Installing polyfactory for a repository checkout
The Makefile documents `make install` as the local development setup. It synchronizes the project with all optional dependencies and installs repository hooks. This command is suitable for contributors working from the repository, rather than being the only documented installation route for application developers.
Run this command from the repository root:
make installThe target first checks for a virtual environment. If one exists, it removes it through the Makefile's cleanup targets. It then runs `uv sync --all-extras` and installs the configured hooks with `uv run prek install --install-hooks`. A successful invocation should end with the Makefile's installation message.
The package declares `requires-python = ">=3.9,<4.0"` and lists Python 3.9 through 3.14 in its classifiers. The version in `pyproject.toml` is `3.3.0`. Because `--all-extras` installs integrations for every supported model library, local development has a larger dependency surface than an application that needs only one extra. The README does not show a separate minimal application installation command, so the project homepage at `https://polyfactory.litestar.dev/` is the appropriate source for that current example.
Creating a first polyfactory fixture
A first fixture must be written for the model integration used by the application. The README ends before a complete usage example, and the visible repository files do not provide a verified factory declaration or invocation. Inventing one would risk presenting an API that polyfactory does not expose.
Start at `https://polyfactory.litestar.dev/` and open the documentation for the relevant integration. If the application uses Pydantic, follow the Pydantic factory example rather than adapting a dataclass example unchanged. The same rule applies to msgspec, SQLAlchemy, ODMantic, and Beanie. The example should identify the factory class or configuration object, the model being generated, and the form of the generated result.
Before using the result in a test, inspect whether it is an instance of the expected model and whether required fields have values. Also check any field with a validator, constrained value, relationship, or special default. Polyfactory's dependency on Faker makes it suitable for supplying varied values, but the README does not say that every generated collection is unique or reproducible. Tests that rely on either property need a confirmed project mechanism rather than an assumption.
Polyfactory's limits for persistent and domain-specific data
polyfactory is a poor fit for application seeding when the primary requirement is to create durable database state. A production-style seeding process may need migrations, transactions, commit behavior, uniqueness repair, referential ordering, and inspection through database queries. A factory can construct an in-memory object, but object construction alone does not meet those operational requirements.
It is also unsuitable when a test depends on exact business records. Names, addresses, timestamps, and other plausible values are not a substitute for known edge cases such as expired credentials, zero balances, incompatible dates, or permission failures. Those conditions often need explicit values. The README does not claim that polyfactory encodes domain rules, and its generic generation purpose should not be interpreted as support for them.
Optional model libraries introduce another constraint. The dependency declarations include lower bounds, but the README does not provide a compatibility matrix for every supported version. A project spanning several services may need to coordinate polyfactory releases with its Pydantic, msgspec, SQLAlchemy, ODMantic, or Beanie versions. The safe first review is therefore model-specific: confirm that the documented integration matches the version already present in the application.
polyfactory compared with constructors and direct Faker use
Hand-built constructors are the clearest alternative for a small test suite. Each test can instantiate its model with exact values, making assertions and data dependencies visible. The tradeoff appears when many modules construct the same types and changes to a model must be propagated through scattered fixture code. Polyfactory groups that construction work, but it introduces another abstraction between a test and the values being passed to application code.
Direct Faker use is a lower-level alternative because `faker>=5.0.0` is already a polyfactory dependency. Faker can supply individual values, but the repository metadata does not establish that direct Faker use automatically produces the right Pydantic, msgspec, dataclass, SQLAlchemy, ODMantic, or Beanie object. Polyfactory's distinguishing role is the factory and integration layer around those values.
For a few stable models, explicit constructors may be easier to maintain. For a suite with many typed fixtures or several supported model libraries, a shared factory layer can remove repeated construction code. The relevant choice is not randomness versus control. It is whether the project values grouped typed construction enough to accept dependency and abstraction costs.
Release history and maintenance boundaries in polyfactory
The repository is not archived, and its last push was on 2026-10-11. Recorded releases include `v3.3.0` on 2026-02-22, `v3.2.0` on 2025-12-21, and `v3.1.0` on 2025-11-25. This shows recent repository changes and published versions, but the README does not include upgrade instructions or a compatibility policy for each release.
The package uses the MIT license. That designation is recorded in `pyproject.toml` and the repository contains a `LICENSE` file. Teams redistributing the software should review that file and retain notices where its terms require them. This is a licensing note rather than legal advice.
Maintenance cost depends on the integrations a project enables. A core installation has two declared dependencies, while enabling model or database extras adds their own constraints. The repository includes `uv.lock`, and its `Makefile` exposes `make upgrade` for synchronizing dependencies with upgrades and running `uv run prek autoupdate`. That target supports repository development, but the README does not state how application projects should coordinate upgrades. Review the relevant release notes and dependency constraints before changing a pinned version.
Editorial conclusion
Adopt polyfactory when tests need model-shaped mock objects and your chosen model integration is documented. Do not choose it for persistent database seeding or domain-specific records without verifying its output first. Check the integration API, returned object type, dependency constraints, and handling of required or constrained fields before adding it as a shared test dependency.
Frequently asked questions
What is polyfactory used for?
polyfactory is a Python library for generating mock data through factories. The repository describes it as a factory library for creating model-shaped test data.
Which Python versions does polyfactory support?
The package requires Python `>=3.9,<4.0`. Its classifiers list Python 3.9, 3.10, 3.11, 3.12, 3.13, and 3.14.
Which integrations are declared by polyfactory?
The optional dependencies include SQLAlchemy, Pydantic, msgspec, ODMantic, and Beanie. The project keywords also include dataclasses and attrs.
How do I install polyfactory for local development?
The Makefile documents `make install`. It runs `uv sync --all-extras` and then installs repository hooks with `uv run prek install --install-hooks`.
When was the latest listed polyfactory release published?
The latest listed release is `v3.3.0`, dated 2026-02-22. The repository's last push was on 2026-10-11.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/litestar-org-polyfactory)Star history chart for your README
[](https://hysenlabs.com/projects/litestar-org-polyfactory)Was this useful?