Skip to main content

Mock data generation factories

Project description

Polyfactory

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

PyPI - License PyPI - Python Version

Coverage Maintainability Rating Reliability Rating Quality Gate Status Security Rating

Reddit Discord Matrix Medium

This library offers factories for mock data generation using python type hints. It part of the LiteStar-API project and is 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]):
    __model__ = 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 the Person class constructor.

Documentation

A full API reference and usage documentation is available in the dedicated documentation site.

Installation

pip install polyfactory

Relation to Pydantic-Factories

The earlier version of this library was released under the name pydantic-factories. While this library became very popular (above 100K monthly downloads), we decided to rename it because we changed the core architecture - polyfactory is a fitting name because we no longer rely on pydantic, which is now an optional dependency. This library is capable of generating mock data for dataclasses, typed-dicts and any custom factory using type annotations. It also supports using pydantic models - but that is optional.

Contributing

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polyfactory-2.0.0a1.tar.gz (27.0 kB view hashes)

Uploaded Source

Built Distribution

polyfactory-2.0.0a1-py3-none-any.whl (34.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page