httk.core.views.coercion¶
General coercion into registered view or value classes.
Two verbs share the machinery here: coerce_view() is the backend-aware, best-effort
coercion (results may be httk Views retaining their exact backend, and lossless fallbacks of
another type are allowed), while coerce() is strict (the result is a non-View instance of
the requested target, or TypeError).
Attributes¶
Functions¶
|
Append |
|
Create a coercer that tries matching view classes in |
|
Coerce |
|
Coerce |
Module Contents¶
- httk.core.views.coercion.register_coercer(coercer, target)[source]¶
Append
coercerto the registry, preserving registration order.targetdeclares what the coercer can coerce into: a class, a tuple of classes, ortyping.Anyfor a fully general coercer. Duringcoerce(), a registered coercer is only tried when the requested target class is a subclass of (one of) its declared targets;Anymatches every target. Invalid declarations raiseTypeErroreagerly.
- httk.core.views.coercion.view_class_coercer(view_classes)[source]¶
Create a coercer that tries matching view classes in
view_classesorder.- Parameters:
view_classes (collections.abc.Sequence[type]) – View classes to try in order.
- Returns:
A coercer for the supplied view classes.
- Return type:
- httk.core.views.coercion.coerce_view(value, target)[source]¶
Coerce
valueto a target class or prototype instance, backend-aware and best-effort.The exact string
"natural"is a documented sentinel that returnsvalueunchanged. 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 — including httk Views that subclass the target, so the exact backend is retained. A target that is aViewsubclass is then tried directly as a view conversion ofvalue, so any view family works without a registered coercer. Failing that, registered coercers whose declared targets match are tried in registration order, and the first non-Noneresult wins. If none succeeds,TypeErroris raised naming the value type and target. Coercion is best effort and favors lossless view wrapping; a coercer may return a lossless fallback of another type (e.g.Fraction(1, 2)for targetint), and individual coercers document any deliberately lossy conversion. Callers that need a plain, exactly-typed result usecoerce()instead.- Parameters:
value (Any) – Value to convert.
target (Any) – Target class, prototype instance, or the
"natural"sentinel.
- Returns:
The best available backend-aware conversion.
- Raises:
TypeError – If no registered or direct conversion succeeds.
- Return type:
Any
- httk.core.views.coercion.coerce(value, target)[source]¶
Coerce
valuestrictly: return a non-View instance of the requested target or raise.The exact string
"natural"returnsvalueunchanged (no coercion, even for a View). Otherwise the resolution ofcoerce_view()applies, and then: an httk View result is shed viaunview()unless the requested target is itself a View class; a View result that cannot shed raisesunview’s ownTypeError; and the final result must satisfyisinstance(result, target)— a lossless fallback of another type (available throughcoerce_view()) makes strict coercion fail withTypeError. An existing non-View subtype of the target is an identity result.- Parameters:
value (Any) – Value to convert.
target (Any) – Target class, prototype instance, or the
"natural"sentinel.
- Returns:
A non-View instance matching the requested target.
- Raises:
TypeError – If strict conversion cannot produce the requested target.
- Return type:
Any