Skip to content

Computed

didactic.api.computed

computed(fn: Callable[..., FieldValue]) -> property
computed(
    *, materialise: bool = False
) -> Callable[[Callable[..., FieldValue]], property]
computed(
    fn: Callable[..., FieldValue] | None = None,
    *,
    materialise: bool = False,
) -> (
    property
    | Callable[[Callable[..., FieldValue]], property]
)

Mark a method as a computed field on a Model.

Parameters:

Name Type Description Default
fn Callable[..., FieldValue] | None

The method to mark. Supplied positionally when the decorator is used without arguments (@dx.computed); None when the decorator is invoked with options (@dx.computed(materialise=True)).

None
materialise bool

Recorded on the underlying property's marker for downstream tooling (theory codegen, axiom emission). At the Python level this flag has no effect: the value is always recomputed on access, never stored.

False

Returns:

Type Description
property or Callable

Either a property (when used directly as @dx.computed) or a decorator that produces one (when used as @dx.computed(materialise=True)).

Notes

The wrapped method should take self and return a value of any didactic-supported type. Computed fields participate in model_dump but never in storage.

Examples:

>>> import didactic.api as dx
>>> class Person(dx.Model):
...     first_name: str
...     last_name: str
...
...     @dx.computed
...     def full_name(self) -> str:
...         return f"{self.first_name} {self.last_name}"
>>> p = Person(first_name="Ada", last_name="Lovelace")
>>> p.full_name
'Ada Lovelace'
>>> p.model_dump()["full_name"]
'Ada Lovelace'