Coder Social home page Coder Social logo

litestar-org / pydantic-factories Goto Github PK

View Code? Open in Web Editor NEW
13.0 3.0 0.0 1.32 MB

Simple and powerful mock data generation using pydantic or dataclasses

Home Page: https://litestar-org.github.io/pydantic-factories/

License: MIT License

Python 99.89% Dockerfile 0.11%
starlite starlite-api starlite-framework pydantic python

pydantic-factories's Introduction

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!

pydantic-factories's People

Contributors

anthonyh209 avatar avihai-yosef avatar butch78 avatar daanrademaker avatar danielkatzan avatar dependabot[bot] avatar eltonchou avatar gegnew avatar gigelu avatar goldziher avatar jacobcoffee avatar jtraub avatar lindycoder avatar lyz-code avatar mciszczon avatar nguyent avatar peterschutt avatar phbernardes avatar provinzkraut avatar reznikovroman avatar roeeyn avatar sondrelg avatar thorin-schiffer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.