smartcall.PosOrKw

class smartcall.PosOrKw(*, required: bool = False, **kwarg: Any)[source]

A value that can be passed as either a positional or a keyword argument.

Parameters:
  • kwarg – A single name-value pair. The name is the “keyword” that will be used when passing this argument as a keyword argument.

  • required – What to do when passing this argument to a function with an incompatible signature. If True, raise an error. If False (the default), ignore it.

When the function could accept either kind of argument, a positional argument will be used. This is because positional arguments don’t require that the function use the same argument names as the caller.

Examples

>>> from smartcall import PosOnly, call
>>> def f(a):
...     return a
...
>>> call(f, PosOrKw(a=1))
1

Note that the name given to the argument doesn’t need to match the function’s signature, if the argument is to be passed positionally:

>>> call(f, PosOrKw(b=1))
1