Keyword-only parameters¶
sigtools
provides two decorators to emulate keyword-only parameters:
- sigtools.modifiers.autokwoargs(func=None, *, exceptions=())[source]
Converts all parameters with default values to keyword-only parameters:
from sigtools.modifiers import autokwoargs @autokwoargs def func(arg, opt1=False, opt2=False, *rest): print(arg, rest, opt1, opt2) func(1, 2, 3) # 1 (2, 3) False False func(1, 2, opt2=True) # -> 1 (2,) False True
In the example above, the
opt1
andopt2
parameters are ‘skipped’ by positional arguments and can only be set using named arguments (opt2=True
).If you wish to prevent parameters that have a default from becoming keyword-only, you can use the
exceptions=
parameter:@autokwoargs(exceptions=['arg2']) def func(arg1, arg2=42, opt1=False, opt2=False): print(arg1, arg2, opt1, opt2) func(1, 2, opt1=False) # 1 2 False False
If you wish to pick individual parameters to convert, use
sigtools.modifiers.kwoargs
. This module also allows you to add function
annotations using the annotate
decorator.