risa.state.schema¶
A view's durable fields, and how their values are packed into state.
Fields are durable by default. risa.Prop[T] marks a field that is rebuilt fresh on
every click instead: it is never saved, costs nothing on the wire, must have a
default, and is typically filled in load.
Prop
¶
Prop = Annotated[T, _PropMarker]
Marks a view field as a per-dispatch prop: refilled in load() and never written to state.
DurableField
¶
StateSchema
¶
StateSchema(fields: Sequence[DurableField], *, view_name: str)
The durable fields of a view, in declaration order, and their packing.
risa builds one per view at registration. State is packed as the values of the durable fields in order, so appending a field that has a default leaves earlier state readable.
empty_fingerprint
property
¶
empty_fingerprint: str
The fingerprint of a view without durable fields.
fingerprint
property
¶
fingerprint: str
The hash of the durable fields' names and types, which state is tagged with.
accepts
¶
adopt
¶
adopt(view: Struct, source: Struct) -> None
Copy the durable fields of source onto view.
Parameters:
-
view(Struct) –The view that takes the values; it is changed in place.
-
source(Struct) –The view the values come from; it needs the same durable fields.
field_sizes
¶
of
classmethod
¶
of(view_cls: type[Struct], *, view_name: str) -> StateSchema
Read the schema of a view class from its fields.
Parameters:
-
view_cls(type[Struct]) –The view class, a
msgspec.Struct, whose annotated fields are read. -
view_name(str) –The view's name, for error messages.
Returns:
-
StateSchema–The schema of the class's durable fields.
Raises:
-
ViewDeclarationError–If a prop has no default, a
risa.Propmarker sits inside an annotation instead of around it, or a field's type cannot be described.
pack
¶
Pack the values of the durable fields, in order, into state.
Parameters:
Returns:
-
bytes–The packed state, empty for a view without durable fields.
Raises:
-
StateEncodeError–If a value cannot be serialized.
pack_view
¶
pack_view(view: Struct) -> bytes
Pack the durable fields of view into state.
Parameters:
-
view(Struct) –The view whose durable fields are packed.
Returns:
-
bytes–The packed state.
Raises:
-
StateEncodeError–If a value cannot be serialized.
structural_type_id
¶
Describe a field type by its structure, for the schema fingerprint.
An enum or Literal counts by the kinds of its values, so a new value of a kind it already
has leaves old state readable, and a removed one fails only for the state that holds it. A
Literal that gains a kind, or loses its last value of one, changes the shape.
Parameters:
-
annotation(object) –The field's type annotation.
Returns:
-
str–The same text for types of the same structure, whatever they are named.
Raises:
-
ValueError–If msgspec cannot describe the type.