skillfed

polyfactory

Mock data generation factories

polyfactory v3.3.0 11.3M downloads/30d#1,401 on PyPI1,501
Permissive license MIT Active released

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 polyfactory

uv

uv add polyfactory

poetry

poetry add polyfactory

Installing 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

Development Status :: 5 - Production/StableEnvironment :: Web EnvironmentFramework :: PytestIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseNatural Language :: EnglishOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.9Topic :: Software DevelopmentTopic :: Software Development :: LibrariesTopic :: Software Development :: TestingTopic :: Software Development :: Testing :: UnitTopic :: UtilitiesTyping :: Typed

Tags

mock data generationtest fixture factorydataclass mock builderpydantic model factorytype-hint based test datafake data from typestesting data generator
testingfixturesmock-data

More Software Development packages