Skip to main content

PyFactories

PyFactories

🚀 Mock data generation for pydantic and dataclasses. 🚀

Test Suite Package version Supported Python versions


Documentation: https://pyfactories.tarsild.io 📚

Source Code: https://github.com/tarsil/pyfactories


Motivation

Pydantic Factories was initially created and used by a lot of people and it is an extremely powerful package that was heavily tested by the same authors of Starlite, now, Litestar. During their own evolution and development they felt the need to add more support to the package that was no longer only pydantic and therefore they created the Polyfactory and archived the Pydantic Factories.

PyFactories is a literal fork from the latest Pydantic Factories and aims to continue the same work set by the previous creators with the difference that will be only maintaining the factories for Pydantic and the upcoming Pydantic 2.0+.

The team behind Pydantic Factories did a fantastic job and the credit goes to them while PyFactories continues its own fork path.

If you aim to use something more than just Pydantic, it is strongly recommended to visit Polyfactory and migrate to it and of course, leave them a ⭐️.

In the meantime if you only want the Pydantic side of things, then you can use PyFactories in the same fashion as its ancestor.

Installation

pip install pyfactories

Migrate from Pydantic Factories

It is actually straightforward, you simply need to replace pydantic_factories with pyfactories in your imports and that is it.

Example

from datetime import date, datetime
from typing import List, Union

from pydantic import UUID4, BaseModel

from pyfactories import ModelFactory


class Person(BaseModel):
    id: UUID4
    name: str
    hobbies: List[str]
    age: Union[float, int]
    birthday: Union[datetime, date]


class PersonFactory(ModelFactory):
    __model__ = Person


result = PersonFactory.build()

And just its own ancestor, this is it. Almost no work, you are able to create mock data objects that fits the Person Pydantic class model definition.

This is possible because of the typing information available on the pydantic model and model-fields, which are used as a source of truth for data generation.

The factory parses the information stored in the pydantic model and generates a dictionary of kwargs that are passed to the Person class' init method.

Features

Being initially the fork of Pydantic Factories, that also means:

  • ✅ Supports both built-in and pydantic types
  • ✅ Supports pydantic field constraints
  • ✅ Supports complex field types
  • ✅ Supports custom model fields
  • ✅ Supports dataclasses
  • ✅ Supports TypedDicts

Why This Library

This library was widely used before because of its powerfull features and therefore with the upcoming Pydantic 2.0, it will be even more robust with the core done in Rust.

  • 💯 Powerful
  • 💯 Extensible
  • 💯 Simple
  • 💯 Rigorously tested

Contributing

This library is open to contributions. Please see the contribution guide!

Star the initial authors

Although the intial Pydantic Factories is no longer maintained, there is always the Polyfactory that deserves a ⭐️.

Release files for pyfactories 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyfactories 2.0.0
File Size Uploaded
pyfactories-2.0.0.tar.gz 22.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyfactories 2.0.0
File Interpreter ABI Platform
pyfactories-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 52.5 kB

Release files / pyfactories-2.0.0.tar.gz

Download URL pyfactories-2.0.0.tar.gz
Size 22.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9f51c2fd45085c3cb1dda11f9c6c44e55b0c9eaf59c9d410d77121927fa5a5eb
BLAKE2b-256 checksum
How to use checksums
f8e7c3204a2fe0ad155288cdbce287f2906c90e317350d675379d972a16acf74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.17

Release files / pyfactories-2.0.0-py3-none-any.whl

Download URL pyfactories-2.0.0-py3-none-any.whl
Size 30.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2c15bfe51b23877e001ff06d68e70f1ade6d98f533305e76f5e57bfad4aa3139
BLAKE2b-256 checksum
How to use checksums
f4289724a1bcfcf0b353659b8aa69bce362d102271fd9fed6dde6328a86720ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.17

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page