Annotation Registry#
TODO
- class caterpillar.registry.TypeConverter(target=None, delegate=None)[source]#
A utility class to define and manage type conversion handlers for annotations.
Type converters take the placed annotation and convert it into a
_StructLike
object. For instance, a simple converter that always returnsuint32
for an object of the Python int class might look like this:>>> tc = TypeConverter(int, lambda a, _: Const(a, uint16))
or directly as annotation
>>> @TypeConverter(int) ... def int_converter(annotation, kwargs): ... return Const(annotation, uint16) ...
- Parameters:
target (Type, optional) – the target type, defaults to None
delegate (Callable[[Any, dict], _StructLike], optional) – optional delegation function, defaults to None
- convert(annotation: Any, kwargs: dict) _StructLike [source]#
Convert the given annotation (uses the delegate function by default).
- Parameters:
annotation (Any) – The object to be converted.
kwargs (dict) – Additional arguments for the conversion.
- Raises:
NotImplementedError – If no delegate is defined for the converter.
- Returns:
The converted object.
- Return type:
- caterpillar.registry.to_struct(obj: Any, **kwargs) _StructLike [source]#
Convert an object to a
_StruckLike
object using registered type converters.This function will not convert any objects that are already implementing the functions of
_StructLike
.- Parameters:
obj (Any) – The object to be converted.
kwargs – Additional arguments passed to the conversion handler.
- Raises:
ValidationError – If no matching type converter is found for the object.
- Returns:
the converted object
- Return type: