# Builder

> Implements the Builder pattern in Python: constructing complex objects step-by-step with fluent APIs. Use when working with python, builder or when the user mentions python, builder.

- Skill: `opknock/builder` (Agent Skill)
- Install (CLI): `npx get-skill add opknock/builder`
- Raw SKILL.md: https://api.skillmd.com/api/skills/opknock/builder/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: MIT
- Author: OpKnock (https://skillmd.com/u/opknock)
- Updated: 2026-11-23
- Page: https://skillmd.com/skills/opknock/builder

---


Implements the Builder pattern in Python: constructing complex objects step-by-step with fluent APIs.

## Agentic Workflow: Read -> Reason -> Act (builder)

You are **Builder** (patterns/general) — a sub-agent that **Reads, Reasons, and Acts** via `allowed-tools`.

### 1. Read — patterns context for `builder`
- Domain: Implements the Builder pattern in Python: constructing complex objects step-by-step with fluent APIs.
- **python**: Implement and test Builder pattern examples. — `python -m venv .venv && .venv/Scripts/activate`
- Check `knowledge` and `prerequisites: python`

### 2. Reason — think for `builder`
- For `python`: Implement and test Builder pattern examples. — decide which checks to run
- Evaluate trust/policy: `kdesk trust` + `kdesk doctor` patterns for your inputs

### 3. Act — execute with `builder` tools
- Tools: `Glob`, `Grep`, `Read`, `Bash` (see frontmatter `tools`/`allowed-tools`)
- Use `safe_path` for any write; record evidence (paths, checksums)
- Fingerprint: `builder:390b71a1`

# Builder Pattern

Construct complex objects step by step.

## When to Use

- Objects with many optional parameters
- Immutable value objects assembled from parts
- Reducing constructor overloads

## Example (Python)

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class Order:
    sku: str
    qty: int
    priority: str = "normal"
    note: str = ""

class OrderBuilder:
    def __init__(self) -> None:
        self._sku = ""
        self._qty = 1
        self._priority = "normal"
        self._note = ""

    def with_sku(self, sku: str) -> "OrderBuilder":
        self._sku = sku
        return self

    def with_qty(self, qty: int) -> "OrderBuilder":
        self._qty = qty
        return self

    def with_priority(self, priority: str) -> "OrderBuilder":
        self._priority = priority
        return self

    def build(self) -> Order:
        return Order(self._sku, self._qty, self._priority, self._note)
```

## Test

```python
from builder import OrderBuilder

def test_builder_defaults():
    order = OrderBuilder().with_sku("A1").with_qty(2).build()
    assert order.sku == "A1"
    assert order.qty == 2
    assert order.priority == "normal"
```

```bash
python -m pytest tests/ -v
```

## Best practices

- Return self from setters for fluent chaining.
- Validate in build() - fail fast on missing requireds.
- Keep the builder mutable, the product immutable.
- Consider a Director only for standard configurations.

## Testing

Cover defaults, overrides, and validation errors.

## Capabilities

### python
Implement and test Builder pattern examples.

**Parameters:**
- `test-path` (string): pytest path filter
- `k` (string): pytest -k expression
- `maxfail` (number): Stop after N failures

**Commands:**
- `python -m venv .venv && .venv/Scripts/activate`
- `python -m pip install pytest`
- `python -m pytest tests/ -v`
- `python -m pytest tests/test_builder.py -k price`
- `python -m compileall builder.py`

**Examples:**
- python -m pytest tests/ -q
- python -m pytest tests/test_builder.py --maxfail=1
- python -c 'from builder import OrderBuilder; print(OrderBuilder().with_sku("A1").with_qty(2).build())'

## References
- [Refactoring Guru: Builder](https://refactoring.guru/design-patterns/builder)
- [Python Typing](https://docs.python.org/3/library/typing.html)

