Skip to content

Commit

Permalink
Put in an explicit check raising when calculations call another proce…
Browse files Browse the repository at this point in the history
…ss (#2250)

By definition, calculation type processes cannot call other processes.
This was already indirectly guarded against by the link validation
process. If one attempted to add a `CALL` link from a `CalulationNode`,
the link validation would raise a `ValueError`. However, it is more
instructive for the user to catch this problem before attempting to add
the link. Therefore, when setting up the database record for a process,
if a parent process is available, we ensure that it is not a calculation
type process, or we raise an `InvalidOperation` exception. A test is
added for a `calcfunction` calling another `calcfunction`.
  • Loading branch information
sphuber authored and giovannipizzi committed Nov 29, 2018
1 parent fa8f617 commit def85b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
11 changes: 11 additions & 0 deletions aiida/backends/tests/work/test_calcfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from __future__ import absolute_import

from aiida.backends.testbase import AiidaTestCase
from aiida.common import exceptions
from aiida.common.caching import enable_caching
from aiida.common.links import LinkType
from aiida.orm.data.int import Int
Expand Down Expand Up @@ -104,3 +105,13 @@ def test_calcfunction_do_not_store_provenance(self):
self.assertFalse(data.is_stored)
self.assertFalse(node.is_stored)
self.assertEqual(result, data + 1)

def test_calculation_cannot_call(self):
"""Verify that calling another process from within a calcfunction raises as it is forbidden."""

@calcfunction
def test_calcfunction_caller(data):
self.test_calcfunction(data)

with self.assertRaises(exceptions.InvalidOperation):
test_calcfunction_caller(self.default_int)
4 changes: 2 additions & 2 deletions aiida/common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################


from __future__ import division
from __future__ import print_function
from __future__ import absolute_import


class AiidaException(Exception):
"""
Base class for all AiiDA exceptions.
Expand Down
6 changes: 5 additions & 1 deletion aiida/work/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,14 @@ def _setup_db_record(self):
parent_calc = self.get_parent_calc()

if parent_calc:

if isinstance(parent_calc, CalculationNode):
raise exceptions.InvalidOperation('calling processes from a calculation type process is forbidden.')

if isinstance(self.calc, CalculationNode):
self.calc.add_incoming(parent_calc, LinkType.CALL_CALC, 'CALL_CALC')

if isinstance(self.calc, WorkflowNode):
elif isinstance(self.calc, WorkflowNode):
self.calc.add_incoming(parent_calc, LinkType.CALL_WORK, 'CALL_WORK')

self._setup_db_inputs()
Expand Down

0 comments on commit def85b5

Please sign in to comment.