Builder
momapy.builder
Classes and functions for building objects from dataclasses.
This module provides functionality to automatically generate builder classes from dataclasses, allowing for flexible object construction and transformation between objects and their builder representations.
Example
from dataclasses import dataclass @dataclass ... class Person: ... name: str ... age: int
Create a builder class automatically
Builder = get_or_make_builder_cls(Person) builder = Builder(name="John", age=30) person = builder.build() print(person) Person(name='John', age=30)
Classes:
| Name | Description |
|---|---|
Builder |
Abstract base class for builder objects. |
Functions:
| Name | Description |
|---|---|
builder_from_object |
Convert an object (or collection of objects) to builder(s). |
get_builder_cls |
Get the registered builder class for a given class. |
get_or_make_builder_cls |
Get an existing builder class or create a new one. |
has_builder_cls |
Check if a builder class is registered for a given class. |
isinstance_or_builder |
Check if object is instance of class or its builder class. |
issubclass_or_builder |
Check if class is subclass of class or its builder class. |
new_builder_object |
Create a new builder instance for a given class. |
object_from_builder |
Convert a builder (or collection of builders) to actual object(s). |
register_builder_cls |
Register a builder class. |
super_or_builder |
Get super() proxy for a class or its builder class. |
Builder
dataclass
Bases: ABC, Monitored
Abstract base class for builder objects.
Builder classes are automatically generated from dataclasses to provide a mutable representation for constructing immutable objects.
Attributes:
| Name | Type | Description |
|---|---|---|
_cls_to_build |
type
|
The class that this builder constructs. |
Methods:
| Name | Description |
|---|---|
build |
Build and return an object from the builder. |
from_object |
Create a builder from an existing object. |
build
abstractmethod
Build and return an object from the builder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inside_collections
|
bool
|
Whether to recursively build objects inside collections (lists, dicts, etc.). Defaults to True. |
True
|
builder_to_object
|
dict[int, Any] | None
|
Optional cache mapping builder ids to built objects for handling circular references. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The constructed object of type |
Source code in src/momapy/builder.py
from_object
abstractmethod
classmethod
from_object(obj: Any, inside_collections: bool = True, omit_keys: bool = True, object_to_builder: dict[int, Builder] | None = None) -> Self
Create a builder from an existing object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to convert to a builder. |
required |
inside_collections
|
bool
|
Whether to recursively convert objects inside collections. Defaults to True. |
True
|
omit_keys
|
bool
|
Whether to skip converting dictionary keys to builders. Defaults to True. |
True
|
object_to_builder
|
dict[int, Builder] | None
|
Optional cache mapping object ids to builders for handling circular references. |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
A builder instance representing the input object. |
Source code in src/momapy/builder.py
builder_from_object
builder_from_object(obj: Any, inside_collections=True, omit_keys=True, object_to_builder: dict[int, Builder] | None = None) -> Builder
Convert an object (or collection of objects) to builder(s).
Recursively converts class instances to their corresponding builder objects. Handles collections (list, tuple, set, dict) and circular references.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
An object instance, collection of objects, or any value. |
required |
inside_collections
|
Whether to recursively process items inside collections. Defaults to True. |
True
|
|
omit_keys
|
Whether to skip converting dictionary keys to builders. Defaults to True. |
True
|
|
object_to_builder
|
dict[int, Builder] | None
|
Optional cache mapping object ids to already-created builders for handling circular references. |
None
|
Returns:
| Type | Description |
|---|---|
Builder
|
The builder object, collection of builders, or the original value |
Builder
|
if no builder class exists. |
Example
from dataclasses import dataclass @dataclass ... class Point: ... x: float ... y: float
point = Point(x=1.0, y=2.0) builder = builder_from_object(point) print(type(builder).name) PointBuilder
Source code in src/momapy/builder.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | |
get_builder_cls
Get the registered builder class for a given class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type
|
The class to get the builder for. |
required |
Returns:
| Type | Description |
|---|---|
Type
|
The builder class if registered, None otherwise. |
Source code in src/momapy/builder.py
get_or_make_builder_cls
get_or_make_builder_cls(cls: Type, builder_fields: Collection[tuple[str, Type, Field]] | None = None, builder_bases: Collection[Type] | None = None, builder_namespace: dict[str, Any] | None = None) -> Type
Get an existing builder class or create a new one.
Returns the registered builder class for the given class if it exists, otherwise creates and registers a new builder class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type
|
The class to get or create a builder for. |
required |
builder_fields
|
Collection[tuple[str, Type, Field]] | None
|
Optional collection of additional fields to include in the builder class. |
None
|
builder_bases
|
Collection[Type] | None
|
Optional collection of base classes for the builder. |
None
|
builder_namespace
|
dict[str, Any] | None
|
Optional namespace dictionary for the builder class. |
None
|
Returns:
| Type | Description |
|---|---|
Type
|
The builder class for the given class, or the original class if |
Type
|
it's not a dataclass. |
Example
from dataclasses import dataclass @dataclass ... class Point: ... x: float ... y: float
PointBuilder = get_or_make_builder_cls(Point) print(PointBuilder.name) PointBuilder
Source code in src/momapy/builder.py
has_builder_cls
Check if a builder class is registered for a given class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type
|
The class to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if a builder class is registered, False otherwise. |
Source code in src/momapy/builder.py
isinstance_or_builder
Check if object is instance of class or its builder class.
Extends isinstance() to also check against registered builder classes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to check. |
required |
type_
|
Type | tuple[Type]
|
The type or tuple of types to check against. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if obj is an instance of type_ or its builder class(es). |
Example
from dataclasses import dataclass @dataclass ... class Point: ... x: float ... y: float
PointBuilder = get_or_make_builder_cls(Point) builder = PointBuilder(x=1.0, y=2.0) isinstance_or_builder(builder, Point) True
Source code in src/momapy/builder.py
issubclass_or_builder
Check if class is subclass of class or its builder class.
Extends issubclass() to also check against registered builder classes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type
|
The class to check. |
required |
type_
|
Type | tuple[Type]
|
The type or tuple of types to check against. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if cls is a subclass of type_ or its builder class(es). |
Source code in src/momapy/builder.py
new_builder_object
new_builder_object(cls: Type, *args, **kwargs) -> Builder
Create a new builder instance for a given class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type
|
The class to build (or a builder class). |
required |
*args
|
Positional arguments for the builder. |
()
|
|
**kwargs
|
Keyword arguments for the builder. |
{}
|
Returns:
| Type | Description |
|---|---|
Builder
|
A new builder instance. |
Example
from dataclasses import dataclass @dataclass ... class Person: ... name: str ... age: int = 0
builder = new_builder_object(Person, name="Alice") print(builder.name) Alice
Source code in src/momapy/builder.py
object_from_builder
object_from_builder(builder: Builder, inside_collections=True, builder_to_object: dict[int, Any] | None = None)
Convert a builder (or collection of builders) to actual object(s).
Recursively converts builder objects to their corresponding class instances. Handles collections (list, tuple, set, dict) and circular references.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
builder
|
Builder
|
A builder instance, collection of builders, or regular object. |
required |
inside_collections
|
Whether to recursively process items inside collections. Defaults to True. |
True
|
|
builder_to_object
|
dict[int, Any] | None
|
Optional cache mapping builder ids to already-built objects for handling circular references. |
None
|
Returns:
| Type | Description |
|---|---|
|
The built object, collection of built objects, or the original value |
|
|
if not a builder. |
Example
from dataclasses import dataclass @dataclass ... class Point: ... x: float ... y: float
PointBuilder = get_or_make_builder_cls(Point) builder = PointBuilder(x=1.0, y=2.0) point = object_from_builder(builder) print(point) Point(x=1.0, y=2.0)
Source code in src/momapy/builder.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
register_builder_cls
Register a builder class.
Registers a builder class so it can be looked up by its target class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
builder_cls
|
Type
|
The builder class to register. Must have a |
required |
Source code in src/momapy/builder.py
super_or_builder
Get super() proxy for a class or its builder class.
Attempts to get the super() proxy for the given type. If that fails, tries with the builder class of the given type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
Type
|
The class to get super() for. |
required |
obj
|
Any
|
The object to get the super proxy of. |
required |
Returns:
| Type | Description |
|---|---|
Type
|
A super() proxy object. |