Supports Auto Freeze¶
Protocol for classes that support the auto-freeze mechanism.
supports_auto_freeze
¶
Protocol for classes that support the auto-freeze mechanism.
SupportsAutoFreeze
¶
Bases: Protocol
Contract for classes compatible with the @auto_freeze decorator.
Implementers must provide methods to transition between mutable and
immutable states. The @auto_freeze decorator calls these methods
based on class configuration.
Example
Source code in src/forging_blocks/foundation/autofreeze/supports_auto_freeze.py
should_use_internal_freezing() -> bool
classmethod
¶
Determine whether the decorator should auto-freeze on init.
Returns:
| Type | Description |
|---|---|
bool
|
True if instances should be automatically frozen after init |
bool
|
completes, False to opt out of automatic freezing. |
Notes
- Override to return False to disable auto-freeze for a class tree.
- Decorator checks this at runtime before calling freeze_instance().
Source code in src/forging_blocks/foundation/autofreeze/supports_auto_freeze.py
freeze_instance() -> None
¶
Transition the instance into an immutable state.
Called automatically by @auto_freeze after init completes
(if should_use_internal_freezing() returns True).
Responsibilities
- Mark the instance as frozen.
- Prevent future attribute modifications.
- Raise CantModifyImmutableAttributeError on setattr.
Notes
- Implementation details are class-specific.
- May use internal flags or other mechanisms.
Source code in src/forging_blocks/foundation/autofreeze/supports_auto_freeze.py
unfreeze_instance() -> None
¶
Transition the instance into a mutable state.
Typically used only in testing or rollback scenarios.
Normal application code should not call this directly.
Responsibilities
- Remove the frozen marker.
- Allow subsequent attribute modifications.
Notes
- Use sparingly; violates immutability contract.
- Intended for test setup/teardown and transaction rollback.
Source code in src/forging_blocks/foundation/autofreeze/supports_auto_freeze.py
freeze_attributes(attrs: Sequence[str]) -> None
¶
Selectively freeze specific attributes.
Optional method for classes that support partial freezing.
If not implemented, freeze_instance() is called instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs
|
Sequence[str]
|
Attribute names to freeze (e.g., ["_id"]). |
required |
Notes
- Useful for Entities where only identity should be immutable.
- Default behavior (if not overridden): freeze_instance().