httk.core.views.coercion

General best-effort coercion into registered view or value classes.

Attributes

Functions

register_coercer(→ None)

Append coercer to the registry, preserving registration order.

view_class_coercer(→ Coercer)

Return a coercer that tries matching view classes in view_classes order.

coerce(→ Any)

Coerce value to a target class or prototype instance.

Module Contents

type httk.core.views.coercion.Coercer = Callable[[Any, type], Any | None][source]
httk.core.views.coercion.register_coercer(coercer: Coercer) None[source]

Append coercer to the registry, preserving registration order.

httk.core.views.coercion.view_class_coercer(view_classes: collections.abc.Sequence[type]) Coercer[source]

Return a coercer that tries matching view classes in view_classes order.

A TypeError, ValueError, or OverflowError from a view constructor means that view cannot represent the value, so construction continues with the next matching class.

httk.core.views.coercion.coerce(value: Any, target: Any) Any[source]

Coerce value to a target class or prototype instance.

The exact string "natural" is a documented sentinel that returns value unchanged. Otherwise, a class target is used directly and an instance target is treated as a prototype, using its type. Values already matching the target are returned unchanged. Registered coercers are then tried in registration order, and the first non-None result wins. If none succeeds, TypeError is raised naming the value type and target. Coercion is best effort and favors lossless view wrapping; individual coercers document any deliberately lossy conversion.