Skip to main content

Create instances of dataclasses with the builder pattern.

Project description

Create instances of Python dataclasses with the builder pattern.

Build status Test coverage

PyPI Package latest release Supported versions PyPI Wheel Status


There are two ways to use dataclass-builder. Via a builder instance or by creating a dedicated builder.

Builder Instance

Using a builder instance is the fastest way to get started with dataclass-builder.

from dataclasses import dataclass
from dataclass_builder import DataclassBuilder, build, fields

class Point:
    x: float
    y: float
    w: float = 1.0

Now we can build a point.

>>> p1_builder = DataclassBuilder(Point)
>>> p1_builder.x = 5.8
>>> p1_builder.y = 8.1
>>> p1_builder.w = 2.0
>>> p1 = build(p1_builder)
Point(x=5.8, y=8.1, w=2.0)

Field values can also be provided in the constructor.

>>> p3_builder = DataclassBuilder(Point, w=100)
>>> p3_builder.x = 5.8
>>> p3_builder.y = 8.1
>>> p3 = build(p3_builder)
Point(x=5.8, y=8.1, w=100)

Fields with default values in the dataclass are optional in the builder.

>>> p4_builder = DataclassBuilder(Point)
>>> p4_builder.x = 5.8
>>> p4_builder.y = 8.1
>>> p4 = build(p4_builder)
Point(x=5.8, y=8.1, w=1.0)

Fields that don’t have default values in the dataclass are not optional.

>>> p5_builder = DataclassBuilder(Point)
>>> p5_builder.y = 8.1
>>> p5 = build(p5_builder)
MissingFieldError: field 'x' of dataclass 'Point' is not optional

Fields not defined in the dataclass cannot be set in the builder.

>>> p6_builder = DataclassBuilder(Point)
>>> p6_builder.z = 3.0
UndefinedFieldError: dataclass 'Point' does not define field 'z'

Accessing a field of the builder before it is set results in an AttributeError.

>>> p8_builder = DataclassBuilder(Point)
>>> p8.x
AttributeError: 'DataclassBuilder' object has no attribute 'x'

Dedicated Builder (coming soon)

A dedicated builder can make more sense if used often or when needing to document the builder.

from dataclasses
from dataclass_builder import dataclass_builder, build

class Point:
    x: float
    y: float
    w: float = 1.0

class PointBuilder:

Now we can build a point.

>>> p_builder = PointBuilder()
>>> p_builder.x = 5.8
>>> p_builder.y = 8.1
>>> p_builder.w = 2.0
>>> p = build(p_builder)
Point(x=5.8, y=8.1, w=2.0)

In addition to providing field values during initialization as with the Builder Instance they can also be provided in the decorator.

class PointBuilder:

The following two statements are mostly equivalent, with the exception of documentation and type.


Therefore, see the section on Builder Instance for further documentation.


  • Python 3.6 or greater
  • dataclasses if using Python 3.6


dataclass-builder is on PyPI so the best way to install it is:

$ pip install dataclass-builder

Project details

Download files

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

Files for dataclass-builder, version 0.0.1.post1
Filename, size File type Python version Upload date Hashes
Filename, size dataclass_builder-0.0.1.post1-py3-none-any.whl (6.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size dataclass-builder-0.0.1.post1.tar.gz (6.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page