built-in coercers

The following are built in validators in records

class CoercionToken

A base class for all object that should be interpreted as coercers

class CallCoercion(CoercionToken)

A coercion token to call arbitrary functions

__init__(func: Callable[[], T], *args, **kwargs)
Parameters
  • func – The callable to use as the coercion callback.

  • args – Optional positional arguments to pass to func, after the validation argument.

  • kwargs – Optional keyword arguments to pass to func.

Note

calling CallCoercion with args or kwargs is akin to calling it with a functools.partial() as func.

>>> CallCoercion(foo, a, b, c=d)
>>> # is equivalent to
>>> CallCoercion(lambda v: foo(v, a, b, c=d))
class MapCoercion(CoercionToken)

A coercion token to map values by arbitrary, pre-defined mappings

__init__(value_map: Optional[Mapping[Any, T]] = None, factory_map: Optional[Mapping[Any, Callable[], T]]] = None)
Parameters
  • value_map – a mapping to map values to coerced values

  • factory_map – a mapping to map values to factory callbacks that create coerced values

class ClassMethodCoercion(CoercionToken)

A coercion token to call a class method in the target class

__init__(method: str, *args, **kwargs)
Parameters
  • method – The name of the class method to call

  • args – Optional positional arguments to pass to the class method, after the validation argument.

  • kwargs – Optional keyword arguments to pass to class method.

class ComposeCoercer(CoercionToken)

A coercion token to chain two coercion callbacks one after the other

__init__(*inner_coercers: Union[Type[records.fillers.coercers.CoercionToken], records.fillers.coercers.CoercionToken])
Parameters

inner_coercers – an iterable of coercion tokens to apply upon the argument, in reverse order.

Note

the inner coercion callbacks are called in reverse order. So if a token A will result in callback a, and token B will result in callback b, then the token ComposeCoercer(A,B) will result in callback lambda v: a(b(v)).

class Eval(CoercionToken)

A coercion token to evaluate a string input as a python expression using eval()

Warning

evaluating arbitrary strings is always risky!

__init__(*args, **kwargs)
Parameters
  • args – Objects to be accessible to the eval function, by their name. Users can also input Ellipsis as the first argument to add the target type to the namespace.

  • kwargs – Additional names to be accessible to the eval function.

class LiteralEval(CoercionToken)

A coercion token to evaluate string inputs with literal_eval()

class Loose(CoercionToken)

A coercion token to call the class constructor with the input as an argument

__init__(*args, **kwargs)
Parameters
  • args – arguments forwarded to the callback

  • kwargs – keywords forwarded to the callback

classmethod constrain(item: Union[type, Tuple[type, ]])

Can be used to constrain Loose to only accept specific inputs of specific types. :param item: the type or types to constrain inputs by. :return: a factory function to a constrained Loose token.

class LooseUnpack(CoercionToken)

A coercion token to call the class constructor with the input as an unpacked iterable

__init__(*args, **kwargs)
Parameters
  • args – arguments forwarded to the callback

  • kwargs – keywords forwarded to the callback

classmethod constrain(*items: Union[type, Tuple[type, ], ellipsis])

Can be used to constrain LooseUnpack to only accept specific inputs of specific types.

Parameters

items – the type or types to constrain inputs by. If there are exactly two values, and the second is Ellipsis, the coercion will accept any number of arguments of type items[0].

Returns

a factory function to a constrained LooseUnpack token.

class LooseUnpackMap(CoercionToken)

A coercion token to call the class constructor with the input as an unpacked mapping

__init__(*args, **kwargs)
Parameters
  • args – arguments forwarded to the callback

  • kwargs – keywords forwarded to the callback

classmethod constrain(**items: Union[type, Tuple[type, ]])

Can be used to constrain LooseUnpackMap to only accept specific inputs of specific types. :param items: the type or types to constrain inputs by. :return: a factory function to a constrained LooseUnpackMap token.

class Whole(CoercionToken)

A coercion token to attempt to convert a whole Number to an Integer type

__init__(*args, **kwargs)
Parameters
  • args – arguments forwarded to the callback

  • kwargs – keywords forwarded to the callback

class ToBytes(CoercionToken)

A coercion token to convert a n Integer value to a bytestring

__init__(*args, **kwargs)
Parameters
  • args – arguments forwarded to the callback

  • kwargs – keywords forwarded to the callback

class FromInteger(CoercionToken)

A coercion token to convert an integer to a boolean, failing if the value is not 0 or 1.

class Falsish(CoercionToken)
A coercion token to construct an instance of the target type ignoring the input, only if the argument is falsish.

Useful to create empty objects from None inputs.

__init__(*args, **kwargs)
Parameters
  • args – arguments forwarded to the callback

  • kwargs – keywords forwarded to the callback