SmartCall
This library provides a way to call functions that won’t necessarily accept all of the arguments that you could pass to them. This situation often occurs when writing libraries that accept callback functions. The library might have lots of information that it could pass to callbacks, but not every callback will want every piece of information.
The following snippet shows how the library works. As an example, we’ll invoke a callback with one required positional argument and two optional keyword arguments. Note that the callback only accepts the first keyword argument:
>>> from smartcall import call, PosOnly, KwOnly
>>> def my_callback(a, b):
... return a, b
...
>>> call(
... my_callback,
...
... # This argument is required, so if the callback can't accept it, an
... # error will be raised.
... PosOnly(1, required=True),
...
... # These arguments are not required, so they will only be passed to the
... # callback if it has a compatible signature.
... KwOnly(b=2),
... KwOnly(c=3),
... )
(1, 2)
Refer to the online documentation for more information.