autosig package

Top-level package for autosig.

class autosig.Signature(*params, **kwparams)[source]

Bases: object

Class to represent signatures.

Parameters:
  • *params ((str, attr.Attribute)) – Optional first non-pair argument describes the return value. Each following argument is a pair with the name of an argument in the signature and a description of it generated with a call to param.
  • **kwparams (attr.Attribute) – Each keyword argument becomes an argument named after the key in the signature of a function and must be initialized with a param call. Requires python >=3.6. If both *param and **params are provided the first will be concatenated with items of the second, in this order.
Returns:

The object created.

Return type:

Signature

set_late_init(init)[source]

Set a function to be called immediately after all arguments have been initialized.

Use this function to perform initialization logic that involves multiple arguments in the signature.

Parameters:init (FunctionType) – The init function is called after the initialization of all arguments in the signature but before the execution of the body of a function with that signature and is passed as an argument a dictionary with all arguments of the function. Returns None and acts exclusively by side effects.
Returns:Returns self.
Return type:Signature
autosig.autosig(sig_or_f)[source]

Decorate functions or methods to attach signatures.

Use with (W) or without (WO) an argument:

@autosig(Signature(a = param(), b=param()))
def fun(a, b)

or, equivlently (WO):

@autosig
def fun(a=param(), b=param())

Do not include the self argument in the signature when decorating methods

Parameters:sig_or_f (Signature or function) – An instance of class Signature (W) or a function or method (WO) whose arguments are intialized with a call to param.
Returns:A decorator (W) or an already decorated function (WO) The decorated function, will intialize, convert, and validate its arguments and will include argument docstrings in its docstring.
Return type:function
autosig.param(default=NOTHING, validator=<function always_valid>, converter=<function identity>, docstring='', position=-1, kw_only=False)[source]

Define parameters in a signature class.

Parameters:
  • default (Any) – The default value for the parameter (defaults to no default, that is, mandatory).
  • validator (callable or type) – If a callable, it takes the actual parameter as an argument, raising an exception or returning False if invalid; returning True otherwise. If a type, the actual parameter must be instance of that type.
  • converter (callable) – The callable is executed with the parameter as an argument and its value assigned to the parameter itself. Useful for type conversions, but not only (e.g. truncate range of parameter).
  • docstring (string) – The docstring fragment for this parameter.
  • position (int) – Desired position of the param in the signature. Negative values start from the end.
  • kw_only (bool) – Whether to make this parameter keyword-only.
Returns:

Object describing all the properties of the parameter. Can be reused in multiple signature definitions to enforce consistency.

Return type:

attr.Attribute

class autosig.Retval(validator=<function always_valid>, converter=<function identity>, docstring='')[source]

Bases: object

Define return values in a Signature class.

Parameters:
  • validator (callable or type) – If a callable, it takes the return value as an argument, raising an exception or returning False if invalid; returning True otherwise. If a type, the return value must be an instance of that type.
  • converter (callable) – The callable is executed with the return value as an argument and its return value is returned instead. Useful to enforce properties of return values, e.g. type, but not only.
  • docstring (string) – The content for the docstring Returns section.