Specifications for callback functions passed in to an API
Project description
Specifications for callback functions passed in to an API
If your code lets other people supply callback functions, it’s important to specify the function signature you expect, and check that functions support that. Adding extra parameters later would break other peoples code unless you’re careful.
backcall provides a way of specifying the callback signature using a prototype function:
from backcall import callback_prototype
@callback_prototype
def handle_ping(sender, delay=None):
# Specify positional parameters without a default, and keyword
# parameters with a default.
pass
def register_ping_handler(callback):
# This checks and adapts the function passed in:
callback = handle_ping.adapt(callback)
ping_callbacks.append(callback)
If the callback takes fewer parameters than your prototype, backcall will wrap it in a function that discards the extra arguments. If the callback expects more arguments, a TypeError is thrown when it is registered.
For more details, see the docs or the Demo notebook.
The tests are run with pytest. In the root directory, execute:
py.test
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
| Filename, size | File type | Python version | Upload date | Hashes |
|---|---|---|---|---|
| Filename, size backcall-0.2.0-py2.py3-none-any.whl (11.2 kB) | File type Wheel | Python version py2.py3 | Upload date | Hashes View |
| Filename, size backcall-0.2.0.tar.gz (18.0 kB) | File type Source | Python version None | Upload date | Hashes View |
Hashes for backcall-0.2.0-py2.py3-none-any.whl
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 | fbbce6a29f263178a1f7915c1940bde0ec2b2a967566fe1c65c1dfb7422bd255 |
|
| MD5 | 4671e998e1853d64a365680de7ecc7a2 |
|
| BLAKE2-256 | 4c1cff6546b6c12603d8dd1070aa3c3d273ad4c07f5771689a7b69a550e8c951 |