Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process functions allow non-Data arguments to be passed as input #5128

Closed
sphuber opened this issue Sep 10, 2021 · 0 comments · Fixed by #5532
Closed

Process functions allow non-Data arguments to be passed as input #5128

sphuber opened this issue Sep 10, 2021 · 0 comments · Fixed by #5532

Comments

@sphuber
Copy link
Contributor

sphuber commented Sep 10, 2021

MWE:

In [1]: from aiida import engine, orm

In [2]: @engine.calcfunction
   ...: def test_kwargs(**kwargs):
   ...:     for value in kwargs.values():
   ...:         assert isinstance(value, orm.Data)
   ...: 
   ...: 

In [3]: test_kwargs(**{'a': orm.Int(1)})
Out[3]: {}

In [4]: test_kwargs(**{'a': orm.Int(1), 'b': 1})
Report: [487|test_kwargs|on_except]: Traceback (most recent call last):
  File "/home/sph/.virtualenvs/aiida_dev/lib/python3.9/site-packages/plumpy/process_states.py", line 231, in execute
    result = self.run_fn(*self.args, **self.kwargs)
  File "/home/sph/code/aiida/env/dev/aiida-core/aiida/engine/processes/functions.py", line 395, in run
    result = self._func(*args, **kwargs)
  File "<ipython-input-2-5fb70fcb7652>", line 4, in test_kwargs
    assert isinstance(value, orm.Data)
AssertionError

As you can see, the second invocation of the test_kwargs calcfunction passes a normal int as an argument, but the input validation does not catch it and simply executes the function where the kwargs contains the normal int value. In contrast, if we don't use **kwargs but explicitly define the arguments, the validation does work:

In [5]: @engine.calcfunction
   ...: def test_explicit(a, b):
   ...:     assert isinstance(a, orm.Data)
   ...:     assert isinstance(b, orm.Data)

In [6]: test_explicit(a=orm.Int(1), b=5)

ValueError: Error occurred validating port 'inputs.b': value 'b' is not of the right type. Got '<class 'int'>', expected '(<class 'aiida.orm.nodes.data.data.Data'>,)'

Originally reported on the mailing list: https://groups.google.com/g/aiidausers/c/BmDOzte4vWQ/m/v7PZUTywBAAJ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant