Registry / testing / pydantic-factories

pydantic-factories

JSON →
library1.17.3pypypiunverified

Pydantic Factories (pydantic-factories) is a Python library designed to generate mock data for Pydantic models and Python dataclasses. Its features include custom field builders, nested model support, and type-safe data generation, making it suitable for testing and prototyping. Version 1.17.3 is the final release; all future development and maintenance have transitioned to the `polyfactory` library.

pip install pydantic-factories
INSTALL
IMPORT
SIG · PYDANTIC-FACTORIES
P
pydantic-factories
testingpythonv1.17.3
Install
4.5s avg
Import
Disk
50MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.17.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 52.7MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 4.5s · import 0.000s · 52MB
50MB installed
● package 50MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ModelFactory
from pydantic_factories import ModelFactory
from pydantic_factories import ModelFactory

Demonstrates defining a simple Pydantic model and creating a corresponding `ModelFactory` to generate single instances or batches of mock data. This is the primary usage pattern for `pydantic-factories`.

from pydantic import BaseModel from pydantic_factories import ModelFactory from datetime import date class User(BaseModel): id: int name: str email: str birth_date: date # Define a factory for your Pydantic model class UserFactory(ModelFactory): __model__ = User # Generate a single instance user_instance = UserFactory.build() print(f"Generated User: {user_instance}") # Generate a batch of instances users_batch = UserFactory.batch(size=3) print(f"Generated Batch of Users: {len(users_batch)}")
Debug
Known issues
breakingThe `pydantic-factories` library is no longer actively maintained. Version 1.17.3 is the final release, and users are strongly advised to migrate to its successor, `polyfactory`.
fix
Install `polyfactory` (`pip install polyfactory`) and refactor your code. While the APIs are similar, `polyfactory` offers enhanced features and better maintenance. Refer to the `polyfactory` documentation for migration guidance.
affects: All (from 1.17.3 onwards)
gotchaDefault generators for basic types (e.g., `str`, `int`, `float`) might produce values that violate specific Pydantic model constraints (e.g., `min_length`, `ge`, `le`, `regex`).
fix
Explicitly define generation parameters using `FactoryField` on the model field, or define custom builders within your factory to ensure generated data conforms to your Pydantic model's validation rules. Example: `name: str = FactoryField(min_length=5, max_length=20)`.
affects: All
gotchaGenerating data for complex or custom types (e.g., a custom class not derived from Pydantic `BaseModel` or `dataclass`) without explicit instructions will result in errors.
fix
Provide a custom builder method on your `ModelFactory` for the specific type, or use `FactoryField(callable=...)` on the Pydantic field definition to tell the factory how to generate that type.
affects: All
Upgrade
Version history
1.17.3latest on PyPI · released Apr 3, 2023
Audit
Dependencies
pydanticrequiredCore dependency for Pydantic model factories.
Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
pydantic-factories — pip install pydantic-factories · libregistry