Skip to content

litestar-org/polyfactory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Litestar - Polyfactory Logo - Light Litestar - Polyfactory Logo - Dark

Project Status
CI/CD Publish package ci pages-build-deployment
Quality codecov
Package PyPI - Version PyPI - Support Python Versions Pydantic Factories PyPI - Downloads Polyfactory PyPI - Downloads
Community Reddit Discord Matrix Medium Twitter Blog
Meta Litestar Project types - Mypy License - MIT Litestar Sponsors Sourcery linting - Ruff code style - Ruff Sourcery All Contributors

Polyfactory

Polyfactory is a simple and powerful mock data generation library, based around type hints and supporting dataclasses, typed-dicts, pydantic models, msgspec structs and more.

Polyfactory part of the Litestar project and as such actively maintained by a community of maintainers and contributors.

Example

from dataclasses import dataclass

from polyfactory.factories import DataclassFactory


@dataclass
class Person:
    name: str
    age: float
    height: float
    weight: float


class PersonFactory(DataclassFactory[Person]):
    ...


def test_is_person() -> None:
    person_instance = PersonFactory.build()
    assert isinstance(person_instance, Person)

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 dataclass, which are used as a source of truth for data generation.

The factory parses the information stored in the dataclass and generates a dictionary of kwargs that are passed to Person.

Documentation

Usage and API reference documentation is available on https://polyfactory.litestar.dev/.

Installation

pip install polyfactory

Relation to Pydantic-Factories

Prior to version 2, this library was known as pydantic-factories, a name under which it gained quite a bit of popularity. A main motivator for the 2.0 release was that we wanted to support more than just Pydantic models, something which also required a change to its core architecture. As this library would no longer be directly tied to Pydantic, polyfactory was chosen as its new name to reflect its capabilities; It can generate mock data for dataclasses, typed-dicts, Pydantic, odmantic, and beanie ODM models, as well as custom factories.

Contributing

This library is a community driven open source project. We welcome and encourage contributions. Please check out the GitHub issues, read the contribution guide (at the repository's root), and you're always welcome to join our discord server.

Contributors โœจ

Thanks goes to these wonderful people (emoji key):

Na'aman Hirschfeld
Na'aman Hirschfeld

๐Ÿšง ๐Ÿš‡ โš ๏ธ ๐Ÿ’ป ๐Ÿ“–
Jacob Coffee
Jacob Coffee

๐Ÿšง ๐Ÿ“– โš ๏ธ
Janek Nouvertnรฉ
Janek Nouvertnรฉ

๐Ÿšง ๐Ÿ“– โš ๏ธ ๐Ÿ’ป
Peter Schutt
Peter Schutt

๐Ÿšง โš ๏ธ ๐Ÿ’ป ๐Ÿ“–
Marek Czaplicki
Marek Czaplicki

๐Ÿ’ป โš ๏ธ
Piotr Przybyล‚o
Piotr Przybyล‚o

๐Ÿ’ป
sygutss
sygutss

๐Ÿ› ๐Ÿ’ป
chrisbeardy
chrisbeardy

๐Ÿ“–
guacs
guacs

๐Ÿ’ป ๐Ÿš‡ ๐Ÿ“– โš ๏ธ ๐Ÿšง
Vadim
Vadim

๐Ÿ’ป
Simske
Simske

๐Ÿ’ป
Sondre Lillebรธ Gundersen
Sondre Lillebรธ Gundersen

๐Ÿ’ป
Mateusz Ciszczoล„
Mateusz Ciszczoล„

๐Ÿ’ป
Pedro Bernardes
Pedro Bernardes

๐Ÿ’ป
Martin Roy
Martin Roy

๐Ÿ’ป
Matthew Aylward
Matthew Aylward

๐Ÿ’ป
Elton H.Y. Chou
Elton H.Y. Chou

๐Ÿ’ป
Thang
Thang

๐Ÿ’ป
Daan
Daan

๐Ÿ’ป
Lyz
Lyz

๐Ÿ’ป
Thorin Schiffer
Thorin Schiffer

๐Ÿ’ป
Iipin
Iipin

๐Ÿ’ป
avihai-yosef
avihai-yosef

๐Ÿ’ป
anthonyh209
anthonyh209

๐Ÿ’ป
Roman Reznikov
Roman Reznikov

๐Ÿ’ป
gigelu
gigelu

๐Ÿ’ป
Rodrigo Medina
Rodrigo Medina

๐Ÿ’ป
Gerrit Egnew
Gerrit Egnew

๐Ÿ’ป
danielkatzan
danielkatzan

๐Ÿ“–
Abdulhaq Emhemmed
Abdulhaq Emhemmed

๐Ÿ’ป
klimantje
klimantje

๐Ÿ’ป
Tom Crasset
Tom Crasset

๐Ÿ’ป
cฤƒtฤƒlin
cฤƒtฤƒlin

๐Ÿ’ป
Andrew Truong
Andrew Truong

๐Ÿ’ป ๐Ÿ“–
Anthony
Anthony

๐Ÿ’ป ๐Ÿ“– โš ๏ธ
Jonathan Liuti
Jonathan Liuti

๐Ÿ’ป ๐Ÿ“– โš ๏ธ
Julio Olivera
Julio Olivera

๐Ÿ’ป โš ๏ธ
Benoรฎt Godard
Benoรฎt Godard

๐Ÿ’ป ๐Ÿ“– โš ๏ธ
Henry Sorsky
Henry Sorsky

๐Ÿš‡
Kim Minki
Kim Minki

๐Ÿ“–
Tim Joseph Dumol
Tim Joseph Dumol

๐Ÿ“–
jeffry
jeffry

๐Ÿ’ป
Sergey Kovalev
Sergey Kovalev

๐Ÿ’ป
Isaac Jackson
Isaac Jackson

๐Ÿ’ป
Nikita Semenov
Nikita Semenov

๐Ÿ’ป ๐Ÿ“–
Michael Bianco
Michael Bianco

๐Ÿ’ป ๐Ÿ“–
Marcus
Marcus

๐Ÿ’ป
Rub1kCube
Rub1kCube

โš ๏ธ

This project follows the all-contributors specification. Contributions of any kind welcome!