Auto Freeze¶
auto_freeze
¶
Auto-freeze decorator for enforcing immutability on class instances.
Provides the :func:auto_freeze decorator that automatically freezes instances
after __init__ completes. The decorator injects a frozen state marker and
a __setattr__ override to prevent attribute modifications.
Can be used as @auto_freeze, @auto_freeze(), or
@auto_freeze(attrs=[...]) for selective freezing.
Useful for: Entities, value objects, and any domain type that
should be immutable after construction.
Example
from forging_blocks.foundation.autofreeze import auto_freeze
from forging_blocks.foundation.errors import CantModifyImmutableAttributeError
@auto_freeze
class Money:
def __init__(self, amount: int, currency: str) -> None:
if amount < 0:
raise ValueError("Amount cannot be negative")
self._amount = amount
self._currency = currency.upper()
@property
def amount(self) -> int:
return self._amount
@property
def currency(self) -> str:
return self._currency
With selective freezing (e.g., for Entities):
auto_freeze(class_: type[T] | None = None, *, attrs: Sequence[str] | None = None) -> type[T] | Callable[[type[T]], type[T]]
¶
Automatically freeze class instances after __init__ completes.
Can be used as @auto_freeze, @auto_freeze(), or
@auto_freeze(attrs=[...]). No protocol implementation is required
on the target class - the decorator handles freezing internally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_
|
type[T] | None
|
The target class (when used directly as |
None
|
attrs
|
Sequence[str] | None
|
Optional attribute names for selective freezing. If |
None
|
Returns:
| Type | Description |
|---|---|
type[T] | Callable[[type[T]], type[T]]
|
The decorated class if class_ is provided; otherwise a callable |
type[T] | Callable[[type[T]], type[T]]
|
that can be used as a decorator. |