-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ProcessFunction
: Add support for variadic arguments
Up till now, variadic arguments, i.e., arguments defined as `*args` in a function signature which collects any remaining positional arguments, were not supported for process functions. The main reason was that it wasn't immediately clear what the link label should be for these inputs. For normal positional arguments we can take the name of the argument declaration in the function signature, and for keyword arguments we take the keyword with which the argument is passed in the function invocation. But for variadic arguments there is no specific argument name, not in the function signature, nor in the function invocation. However, we can simply create a link label. We just have to ensure that it doesn't clash with the link labels that will be generated for the positional and keyword arguments. Here the link label will be determined with the following format: `{label_prefix}_{index}` The `label_prefix` is determined the name of the variadic argument. If a function is declared as `function(a, *args, **kwargs)` the prefix will be equal to `args` and if it is `function(*some_var_args)` it will be `some_var_args`. The index will simply be the zero-base index of the argument within the variadic arguments tuple. This would therefore give link labels `args_0`, `args_1` etc. in the first example. If there would be a clash of labels, for example with the function def: def function(args_0, *args): which when invoked as: function(1, *(2, 3)) would generate the labels `args_0` for the first positional argument, but also `args_0` for the first variadic argument. This clash is detected and a `RuntimeError` is raised instructing the user to fix it.
- Loading branch information
Showing
4 changed files
with
104 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
docs/source/topics/processes/include/snippets/functions/signature_calcfunction_args.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
from aiida.engine import calcfunction | ||
from aiida.orm import Int | ||
|
||
|
||
@calcfunction | ||
def average(*args): | ||
return sum(args) / len(args) | ||
|
||
result = average(*(Int(1), Int(2), Int(3))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters