Skip to content

proposal: new decorator to use config as default values for kwargs

Matias Guijarro requested to merge decorator_config_default_values into master

I propose the following new decorator in utils :

def apply_config_to_kwargs(dict_or_config, shorten_signature=False):
    """
    Decorator that can be used to modify the default values of keywords in a function call.
    There are two ways to use this decorator depending if a class variable is used as 
    dict_or_config or not.
    
    1) classical use as decorator:
        @apply_config_to_kwargs({"kw1":30,"kw2":"from dict"})
        def func3(arg, kw1=0, kw2="from def"):
            return ("func3", arg, kw1, kw2)
    will change the signature to "(arg, kw1=10, kw2='from config')"
    
    2) use without syntactical sugar e.g. in __init__ of a class:
        class DummyKWObject:
        def __init__(self, name, config):
            self.func = apply_config_to_kwargs(config)(self._func)

        def _func(self, arg, kw1=0, kw2="from def"):
            return ("func", arg, kw1, kw2)
    
    this results in self.func calling self._func with the default values for kwargs 
    taken from config 
    """

I think this would be very nice to have e.g. to define the default behavior of axis.home from the config

Merge request reports