File: getting-started.rst

package info (click to toggle)
python-polyfactory 2.22.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,892 kB
  • sloc: python: 11,338; makefile: 103; sh: 37
file content (42 lines) | stat: -rw-r--r-- 1,800 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
===============
Getting Started
===============

Polyfactory is a simple and powerful mock data generation library, based around type
hints and supporting :doc:`dataclasses <python:library/dataclasses>`, :class:`TypedDicts <typing.TypedDict>`,
Pydantic models, :class:`msgspec Struct's <msgspec.Struct>` and more.

Installation
------------

.. code-block:: bash

    pip install polyfactory

Example
-------

.. literalinclude:: /examples/declaring_factories/test_example_1.py
    :caption: Minimal example using a dataclass
    :language: python

That is 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.

Relation to ``pydantic-factories``
----------------------------------

Prior to version 2, this library was known as `pydantic-factories <https://pypi.org/project/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
:doc:`dataclasses <python:library/dataclasses>`, :class:`TypedDicts <typing.TypedDict>`,
`Pydantic <https://docs.pydantic.dev/latest/>`_, `Odmantic <https://art049.github.io/odmantic/>`_,
and `Beanie ODM <https://beanie-odm.dev/>`_ models, as well as custom factories.