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

Workchains: Raise if if_/while_ predicate does not return boolean #259

Merged
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
7 changes: 6 additions & 1 deletion src/plumpy/workchains.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,12 @@ def predicate(self) -> PREDICATE_TYPE:
return self._predicate

def is_true(self, workflow: 'WorkChain') -> bool:
return self._predicate(workflow)
result = self._predicate(workflow)

if not isinstance(result, bool):
raise TypeError(f'The conditional predicate `{self._predicate.__name__}` did not return a boolean')

return result

def __call__(self, *instructions: Union[_Instruction, WC_COMMAND_TYPE]) -> _Instruction:
assert self._body is None, 'Instructions have already been set'
Expand Down
11 changes: 11 additions & 0 deletions test/test_workchains.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,3 +618,14 @@ def step_two(self):

workchain = Wf(inputs=dict(subspace={'one': 1, 'two': 2}))
workchain.execute()


@pytest.mark.parametrize('construct', (if_, while_))
def test_conditional_return_type(construct):
"""Test that a conditional passed to the ``if_`` and ``while_`` functions that does not return a ``bool`` raises."""

def invalid_conditional(self):
return 'true'

with pytest.raises(TypeError, match='The conditional predicate `invalid_conditional` did not return a boolean'):
construct(invalid_conditional)[0].is_true(None)