Skip to content

Commit 9888080

Browse files
Merge branch 'fix-bug-is-fairworkflows-decorator' of github.com:fair-workflows/fairworkflows into use-noodles-from-call
2 parents dd79526 + 53e8b9a commit 9888080

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

fairworkflows/fairworkflow.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,8 @@ def _modify_function(func):
519519
num_params = len(inspect.signature(func).parameters)
520520
empty_args = ([inspect.Parameter.empty()] * num_params)
521521
workflow_level_promise = scheduled_workflow(*empty_args)
522+
_validate_decorated_function(func, empty_args)
522523
step_level_promise = func(*empty_args)
523-
if not isinstance(step_level_promise, PromisedObject):
524-
raise TypeError("The workflow does not return a 'promise'. Did you use the "
525-
"is_fairstep decorator on all the steps?")
526524

527525
# Description of workflow is the raw function code
528526
description = inspect.getsource(func)
@@ -531,3 +529,22 @@ def _modify_function(func):
531529
is_pplan_plan=is_pplan_plan, derived_from=None)
532530
return workflow_level_promise
533531
return _modify_function
532+
533+
534+
def _validate_decorated_function(func, empty_args):
535+
"""
536+
Validate that a function decorated with is_fairworkflow actually consists of steps that are
537+
decorated with is_fairstep. Call the function using empty arguments to test. NB: This won't
538+
catch all edgecases of users misusing the is_fairworkflow decorator, but at least will
539+
provide more useful error messages in frequently occurring cases.
540+
"""
541+
try:
542+
result = func(*empty_args)
543+
except TypeError as e:
544+
raise TypeError("Marking the function as workflow with `is_fairworkflow` decorator "
545+
"failed. "
546+
"Did you use the is_fairstep decorator on all the steps? "
547+
f"Detailed error message: {e}")
548+
if not isinstance(result, PromisedObject):
549+
raise TypeError("The workflow does not return a 'promise'. Did you use the "
550+
"is_fairstep decorator on all the steps?")

tests/test_fairworkflow.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,24 @@ def my_workflow(in1):
386386
"""
387387
return return_value(in1)
388388
assert "The workflow does not return a 'promise'" in str(e.value)
389+
390+
def test_workflow_mixed_decorated_steps(self):
391+
def add(a: float, b: float) -> float:
392+
"""Adding up numbers. NB: no is_fairstep decorator!"""
393+
return a + b
394+
395+
@is_fairstep(label='Subtraction')
396+
def sub(a: float, b: float) -> float:
397+
"""Subtracting numbers."""
398+
return a - b
399+
400+
with pytest.raises(TypeError) as e:
401+
@is_fairworkflow(label='My Workflow')
402+
def my_workflow(in1, in2):
403+
"""
404+
A simple addition, subtraction workflow
405+
"""
406+
return add(in1, sub(in2, in2))
407+
assert ("Marking the function as workflow with `is_fairworkflow` decorator failed. "
408+
in str(e.value))
409+
assert "unsupported operand type(s)" in str(e.value)

0 commit comments

Comments
 (0)