annotation.composition - Decorator to use annotations for function composition

compose_annotations(combine_var_positional=False, combine_var_keyword=False)[source]

Decorator to use compose a function with its callable annotations.

Parameters:
  • combine_var_positional (bool) – Transform VAR_POSIITIONAL arguments (see inspect.Parameter) collectively instead of element-wise (the default)
  • combine_var_keyword (bool) – Transform VAR_KEYWORD arguments (see inspect.Parameter) collectively instead of element-wise (the default)
Returns:

Original function composed with its callable annotations

Return type:

func

A collections.abc.Sequence containing only callable elements is treated as a pipeline (ie: the raw value is passed to the first element, its return value to the second etc.)

Example

>>> @compose_annotations
... def to_str(x: str):
...     return x
>>> to_str(5)
'5'

The behaviour of the resulting (wrapped) function is that the “raw” parameters and return value are “passed through” their respective annotations (ie: their values are replaced with those returned from their annotations). This can be useful for coercion or validation.