Skip to content

Commit

Permalink
WorkChains: Raise if predicate if_/while_ does not return boolean (
Browse files Browse the repository at this point in the history
…#5924)

Before the conditionals created by the `if_` and `while_` constructs
would enter their body as long as the predicate returned anything that
was truthy. This could lead to unexpected behavior. For example, if a
user tried to abort a workchain from the predicate of a while-loop by
returning an `ExitCode` instance, the while stepper would accept the
exit code as truthy and continue the loop.

To prevent this, the conditionals will now raise a `TypeError` whenever
the value returned by the predicate is not a boolean. Since this logic
was added in `plumpy==0.21.4` the dependency requirement is updated.
  • Loading branch information
sphuber authored Mar 9, 2023
1 parent 059ab66 commit f7d7a4f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies:
- importlib-resources~=5.0
- numpy~=1.19
- paramiko>=2.7.2,~=2.7
- plumpy~=0.21.3
- plumpy~=0.21.4
- pgsu~=0.2.1
- psutil~=5.6
- psycopg2-binary~=2.8
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies = [
"importlib-resources~=5.0;python_version<'3.9'",
"numpy~=1.19",
"paramiko~=2.7,>=2.7.2",
"plumpy~=0.21.3",
"plumpy~=0.21.4",
"pgsu~=0.2.1",
"psutil~=5.6",
"psycopg2-binary~=2.8",
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements-py-3.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pickleshare==0.7.5
Pillow==9.3.0
plotly==5.4.0
pluggy==1.0.0
plumpy==0.21.3
plumpy==0.21.4
prometheus-client==0.12.0
prompt-toolkit==3.0.37
psutil==5.8.0
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements-py-3.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Pillow==9.3.0
platformdirs==2.5.4
plotly==5.11.0
pluggy==1.0.0
plumpy==0.21.3
plumpy==0.21.4
prometheus-client==0.15.0
prompt-toolkit==3.0.37
psutil==5.9.4
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements-py-3.8.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pickleshare==0.7.5
Pillow==9.3.0
plotly==5.4.0
pluggy==1.0.0
plumpy==0.21.3
plumpy==0.21.4
prometheus-client==0.12.0
prompt-toolkit==3.0.37
psutil==5.8.0
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements-py-3.9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pickleshare==0.7.5
Pillow==9.3.0
plotly==5.4.0
pluggy==1.0.0
plumpy==0.21.3
plumpy==0.21.4
prometheus-client==0.12.0
prompt-toolkit==3.0.37
psutil==5.8.0
Expand Down
34 changes: 34 additions & 0 deletions tests/engine/test_work_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,40 @@ def read_context(self):
def test_str(self):
assert isinstance(str(Wf.spec()), str)

def test_invalid_if_predicate(self):
"""Test that workchain raises if the predicate of an ``if_`` condition does not return a boolean."""

class TestWorkChain(WorkChain):

@classmethod
def define(cls, spec):
super().define(spec)
spec.outline(if_(cls.predicate))

def predicate(self):
"""Invalid predicate whose return value is not a boolean."""
return 'true'

with pytest.raises(TypeError, match=r'The conditional predicate `predicate` did not return a boolean'):
launch.run(TestWorkChain)

def test_invalid_while_predicate(self):
"""Test that workchain raises if the predicate of an ``while_`` condition does not return a boolean."""

class TestWorkChain(WorkChain):

@classmethod
def define(cls, spec):
super().define(spec)
spec.outline(while_(cls.predicate))

def predicate(self):
"""Invalid predicate whose return value is not a boolean."""
return 'true'

with pytest.raises(TypeError, match=r'The conditional predicate `predicate` did not return a boolean'):
launch.run(TestWorkChain)

def test_malformed_outline(self):
"""
Test some malformed outlines
Expand Down

0 comments on commit f7d7a4f

Please sign in to comment.