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

Engine: Do not call serializer for None values #5694

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aiida/engine/processes/ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ def __init__(self, *args, **kwargs) -> None:
self._serializer: Callable[[Any], 'Data'] = serializer

def serialize(self, value: Any) -> 'Data':
"""Serialize the given value if it is not already a Data type and a serializer function is defined
"""Serialize the given value, unless it is ``None``, already a Data type, or no serializer function is defined.

:param value: the value to be serialized
:returns: a serialized version of the value or the unchanged value
"""
if self._serializer is None or isinstance(value, Data):
if self._serializer is None or value is None or isinstance(value, Data):
return value

return self._serializer(value)
Expand Down
4 changes: 2 additions & 2 deletions docs/source/topics/processes/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ This allows one to pass any normal value that one would also be able to pass to
Automatic input serialization
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Quite often, inputs which are given as python data types need to be cast to the corresponding AiiDA type before passing them to a process.
Quite often, inputs which are given as Python data types need to be cast to the corresponding AiiDA type before passing them to a process.
Doing this manually can be cumbersome, so you can define a function when defining the process specification, which does the conversion automatically.
This function, passed as ``serializer`` parameter to ``spec.input``, is invoked if the given input is *not* already an AiiDA type.
This function, passed as ``serializer`` parameter to ``spec.input``, is invoked if the given input is not ``None`` *and* not already an AiiDA type.

For inputs which are stored in the database (``non_db=False``), the serialization function should return an AiiDA data type.
For ``non_db`` inputs, the function must be idempotent because it might be applied more than once.
Expand Down
5 changes: 5 additions & 0 deletions tests/engine/test_process_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,8 @@ def test_input_serialization(argument, node_cls):
assert result.get_dict() == argument
else:
assert result.value == argument


def test_input_serialization_none_default():
"""Test that calling a function with explicit ``None`` for an argument that defines ``None`` as default works."""
assert function_with_none_default(int_a=1, int_b=2, int_c=None).value == 3