When to use **kwargs in function definition

I was talking to a colleague and we were not 100% sure in which functions VIKTOR requires **kwargs in the input definition. Could someone explain when this is needed exactly?

The reason for VIKTOR to require **kwargs in certain controller methods and callback functions (e.g. action button methods), is that it allows us to pass new arguments to the signature of that function without breaking (older) apps, since the new argument is silently passed into **kwargs.

An example of such an addition is the entity_name argument that was introduced in v12.12.0. Note that apps implementing this would crash:

def callback_function(self, params, entity_id):  # error: missing entity_name!

But having **kwargs makes this no problem:

def callback_function(self, params, entity_id, **kwargs):  # entity_name is in kwargs now

Thank you!

I was wondering if there is a clear definition somewhere of ‘certain’ controller methods and callback functions?

For clarity reasons I would like to only add **kwargs wherever it is needed but not anywhere else to make sure it is clear that when a **kwargs is present in my code it should be there. Right now I often just add it in every function to avoid errors but this goes at a cost of code readability.

The way I understand it now is that it should be present in controller methods and methods that are connected to action buttons. Is this always the case or are there exceptions to this rule?

I could look and sum up for you all functions/methods that now require **kwargs, but I think it would be better to follow the general rule: when VIKTOR calls your function (view methods, action button methods, field visibility callback functions), use **kwargs.

1 Like

Thank you, I will follow that rule :D!