Search code examples
pythoninterfaceabstract-classabc

Python Library for Function Interfaces?


I'm wondering if there's an existing Python libary/technique for enforcing function interfaces/"contracts". Something like ABC but for functions.

E.g. An example with made-up syntax:

@implements(i_state_updater)
def my_update(position, state, forces):
    ... 
    return new_position, new_state

Where

@declare_interface
def i_state_updater(position, state, forces):
    """
    :param position: ...
    :returns: ....
    """

So that when I pass this function as an argument I can verify it, eg

def compute_trajectory(update_func, n_steps, initial_state):
    """
    :param update_func: An i_state_updater
    ...
    """
    assert update_func.implements(i_state_updater)

Python's existing abc module does something like this, but only for classes. Is there an equivalent out there for functions?


Solution

  • PyContract seems like a library that you would want to look into.

    See https://pypi.python.org/pypi/PyContracts

    See also