The Wayback Machine - https://web.archive.org/web/20221016194517/https://github.com/starlite-api/pydantic-factories
Skip to content

starlite-api/pydantic-factories

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Starlite logo

PyPI - License PyPI - Python Version

Language grade: Python Total alerts Coverage Maintainability Rating Reliability Rating Quality Gate Status

Discord

Pydantic-Factories

This library offers powerful mock data generation capabilities for pydantic based models and dataclasses. 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

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!