pydantic-factories

Mock data generation for pydantic based models and python dataclasses

MIT 62 个版本 Python >=3.8,<4.0
Na'aman Hirschfeld <nhirschfeld@gmail.com>
安装
pip install pydantic-factories
poetry add pydantic-factories
pipenv install pydantic-factories
conda install pydantic-factories
描述

Starlite Logo - Light Starlite Logo - Dark

PyPI - License PyPI - Python Version

Discord Matrix Reddit

⚠️

The next version of this library is released as polyfactory. Users are encouraged to migrate to it.

⚠️

Pydantic-Factories

This library offers powerful mock data generation capabilities for pydantic based models, dataclasses and TypeDicts. It can also be used with other libraries that use pydantic as a foundation.

Check out the documentation 📚.

Installation

pip install pydantic-factories

Example

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

from pydantic import BaseModel, UUID4

from pydantic_factories 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()

That's it - with almost no work, we are able to create a mock data object fitting the Person 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

  • ✅ 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?

  • 💯 powerful
  • 💯 extensible
  • 💯 simple
  • 💯 rigorously tested

Contributing

This library is open to contributions - in fact we welcome it. Please see the contribution guide!

版本列表
1.17.3 2023-04-03
1.17.2 2023-02-09
1.17.1 2023-01-15
1.17.0 2022-12-04
1.16.0 2022-11-24
1.15.0 2022-11-07
1.14.0 2022-11-04
1.13.0 2022-10-26
1.12.0 2022-10-17
1.11.1 2022-10-16
1.11.0 2022-10-16
1.10.0 2022-10-13
1.9.0 2022-10-09
1.8.2 2022-10-08
1.8.1 2022-10-07
1.8.0 2022-10-07
1.7.1 2022-10-01
1.7.0 2022-09-28
1.6.2 2022-09-15
1.6.1 2022-08-30
1.6.0 2022-08-27
1.5.4 2022-08-12
1.5.2 2022-08-11
1.5.1 2022-08-10
1.5.0 2022-08-09
1.4.1 2022-07-25
1.4.0 2022-07-18
1.3.0 2022-05-27
1.2.9 2022-05-20
1.2.8 2022-05-03
1.2.7 2022-03-29
1.2.6 2022-03-04
1.2.5 2022-02-18
1.2.4 2022-02-11
1.2.3 2022-01-21
1.2.2 2022-01-21
1.2.1 2022-01-16
1.2.0 2022-01-09
1.1.0 2022-01-07
1.0.0 2022-01-01
0.8.0 2021-12-31
0.7.0 2021-12-28
0.6.3 2021-12-16
0.6.2 2021-12-16
0.6.1 2021-12-15
0.6.0 2021-12-14
0.5.0 2021-11-29
0.4.6 2021-11-29
0.4.5 2021-11-29
0.4.4 2021-11-29
0.4.3 2021-11-29
0.4.2 2021-11-28
0.4.1 2021-11-27
0.4.0 2021-11-25
0.3.5b0 2021-11-24
0.3.4b0 2021-11-24
0.3.3b0 2021-11-23
0.3.2b0 2021-11-23
0.3.1b0 2021-11-22
0.3.0b0 2021-11-21
0.2.0a0 2021-11-18
0.1.0a0 2021-11-17