Skip to main content

Python Builder

Simple implementation of a builder pattern for Python. This library is done to be as small and simple as possible. I wanted an easy wrapper around any class.

In my tests, I explicitly check if my library works with:

  • base python class
  • python dataclass
  • python "slotted" class (using __slots__)
  • pydantic BaseModel

But because of how simple the code is, I expect it to work with all classes.

Usage

To use the Python Builder, decorate your classes with @add_builder and utilize the generated builder() method to construct instances fluently.

Example with a Regular Python Class

from python_builder import add_builder

@add_builder
class RegularClass:
    def __init__(self, a: int, b: str, c: bool):
        self.a = a
        self.b = b
        self.c = c

# Building an instance
builder = RegularClass.builder()
instance = (
    builder
    .set("a", 10)
    .set("b", "test")
    .set("c", True)
    .build()
)

print(instance.a)  # Output: 10
print(instance.b)  # Output: test
print(instance.c)  # Output: True

Example with a Dataclass

from dataclasses import dataclass
from python_builder import add_builder

@add_builder
@dataclass
class DataClass:
    x: float = None
    y: str = None
    z: int = None

# Building an instance
instance = (
    DataClass.builder()
    .set("x", 3.14)
    .set("y", "pi")
    .set("z", 42)
    .build()
)

print(instance.x)  # Output: 3.14
print(instance.y)  # Output: pi
print(instance.z)  # Output: 42

Example with a Pydantic Model

from pydantic import BaseModel
from python_builder import add_builder

@add_builder
class PydanticModel(BaseModel):
    foo: str
    bar: int
    baz: bool

# Building an instance
instance = (
    PydanticModel.builder()
    .set("foo", "hello")
    .set("bar", 123)
    .set("baz", False)
    .build()
)

print(instance.foo)  # Output: hello
print(instance.bar)  # Output: 123
print(instance.baz)  # Output: False

Example with a Slotted Class

from python_builder import add_builder

@add_builder
class SlotClass:
    __slots__ = ["x", "y", "z"]

    def __init__(self, x: int, y: str, z: bool):
        self.x = x
        self.y = y
        self.z = z

# Building an instance
instance = (
    SlotClass.builder()
    .set("x", 100)
    .set("y", "slot test")
    .set("z", True)
    .build()
)

print(instance.x)  # Output: 100
print(instance.y)  # Output: slot test
print(instance.z)  # Output: True

Merging Builders

You can merge multiple builder instances using the | operator. The resulting builder inherits properties from both builders. In cases of conflicting properties, the values from the builder on the right take precedence.

builder1 = RegularClass.builder().set("a", 1).set("b", "initial")
builder2 = RegularClass.builder().set("b", "overridden").set("c", True)
merged_builder = builder1 | builder2  # Merges builder1 and builder2; 'b' from builder2 takes precedence
instance = merged_builder.build()

print(instance.a)  # Output: 1       # Inherited from builder1
print(instance.b)  # Output: overridden  # Overridden by builder2
print(instance.c)  # Output: True    # Inherited from builder2

When merging builders using the | operator, the resulting builder combines the properties from both builders. If both builders set the same property, the value from the builder on the right side of the | operator overrides the one from the left.

Handling Errors

If you set an invalid property or omit required properties, the builder will raise appropriate errors.

# Setting an invalid property
builder = RegularClass.builder().set("d", "invalid")
instance = builder.build()  # Raises TypeError

# Building with missing required properties
builder = RegularClass.builder().set("a", 100)
instance = builder.build()  # Raises TypeError

These examples demonstrate how to utilize the Python Builder with different types of classes, handle merging of builders, and manage potential errors during the building process.

Developement

I use uv so I expect you have it installed too. After cloning the repo, run:

uv sync

That should install all dev dependencies. After that, activate venv and run:

pre-commit install

That will auto format your code on each commit.

Metadata

Release files for python-builder 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-builder 0.4.2
File Size Uploaded Provenance
python_builder-0.4.2.tar.gz 5.3 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for python-builder 0.4.2
File Interpreter ABI Platform
python_builder-0.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.1 kB

Release files / python_builder-0.4.2.tar.gz

Download URL python_builder-0.4.2.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
396151a084fb1bce8d5ccfe009479c160b57f65a182e72d502d126f53b0dd192
BLAKE2b-256 checksum
How to use checksums
dacae25dc1476e31ee93e47c9fb897d97430c029a7e828cb8157bd5aa6ffb45e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 12, 2025.

Transparency log

Release files / python_builder-0.4.2-py3-none-any.whl

Download URL python_builder-0.4.2-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d0b18ffcd1ce6df0d18337f4ac2b508efddb298019c7ec7f0eac32735830afe7
BLAKE2b-256 checksum
How to use checksums
1afa76d9c7bbf1ce7258ebee7a478e5b11e1e3735af4aff084b211ff9c5bb5f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 12, 2025.

Transparency log

Security for python-builder 0.4.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for python_builder-0.4.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 12, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page