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?
PyContract seems like a library that you would want to look into.
See https://pypi.python.org/pypi/PyContracts
See also
Tools for static type checking in Python
How to use type hints in python 3.6?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With