Skip to content

Commit

Permalink
remove function calls from validate_script
Browse files Browse the repository at this point in the history
  • Loading branch information
RLKRo committed Mar 1, 2024
1 parent bf89805 commit deb0c30
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 84 deletions.
54 changes: 10 additions & 44 deletions dff/pipeline/pipeline/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,53 +404,19 @@ def validate_script(self, pipeline: Pipeline, verbose: bool = True):

error_msgs = []
for flow_label, node_label, label, condition in zip(flow_labels, node_labels, labels, conditions):
ctx = Context()
ctx.validation = True
ctx.add_request(Message("text"))
if not callable(label):
label = normalize_label(label, flow_label)

label = label(ctx, pipeline) if callable(label) else normalize_label(label, flow_label)

# validate labeling
try:
node = self.script[label[0]][label[1]]
except Exception as exc:
msg = (
f"Could not find node with label={label}, "
f"error was found in (flow_label, node_label)={(flow_label, node_label)}"
)
error_handler(error_msgs, msg, exc, verbose)
break

# validate responsing
response_func = normalize_response(node.response)
try:
response_result = asyncio.run(wrap_sync_function_in_async(response_func, ctx, pipeline))
if not isinstance(response_result, Message):
# validate labeling
try:
node = self.script[label[0]][label[1]]
except Exception as exc:
msg = (
"Expected type of response_result is `Message`.\n"
+ f"Got type(response_result)={type(response_result)}"
f" for label={label} , error was found in (flow_label, node_label)={(flow_label, node_label)}"
f"Could not find node with label={label}, "
f"error was found in (flow_label, node_label)={(flow_label, node_label)}"
)
error_handler(error_msgs, msg, None, verbose)
continue
except Exception as exc:
msg = (
f"Got exception '''{exc}''' during response execution "
f"for label={label} and node.response={node.response}"
f", error was found in (flow_label, node_label)={(flow_label, node_label)}"
)
error_handler(error_msgs, msg, exc, verbose)
continue

# validate conditioning
try:
condition_result = condition(ctx, pipeline)
if not isinstance(condition(ctx, pipeline), bool):
raise Exception(f"Returned condition_result={condition_result}, but expected bool type")
except Exception as exc:
msg = f"Got exception '''{exc}''' during condition execution for label={label}"
error_handler(error_msgs, msg, exc, verbose)
continue
error_handler(error_msgs, msg, exc, verbose)
break
return error_msgs


Expand Down
14 changes: 0 additions & 14 deletions tests/pipeline/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ def test_pretty_format():
tutorial_module.pipeline.pretty_format()


@pytest.mark.parametrize("validation", (True, False))
def test_from_script_with_validation(validation):
def response(ctx, pipeline: Pipeline):
raise RuntimeError()

script = {"": {"": {RESPONSE: response, TRANSITIONS: {"": cnd.true()}}}}

if validation:
with pytest.raises(ValueError):
_ = Pipeline.from_script(script=script, start_label=("", ""), validation_stage=validation)
else:
_ = Pipeline.from_script(script=script, start_label=("", ""), validation_stage=validation)


def test_script_getting_and_setting():
script = {"old_flow": {"": {RESPONSE: lambda c, p: Message(), TRANSITIONS: {"": cnd.true()}}}}
pipeline = Pipeline.from_script(script=script, start_label=("old_flow", ""))
Expand Down
26 changes: 0 additions & 26 deletions tests/script/core/test_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,6 @@ async def test_actor():
raise Exception("can not be passed: fail of missing node")
except ValueError:
pass
try:
# fail of condition returned type
Pipeline.from_script({"flow": {"node1": {TRANSITIONS: {"node1": std_func}}}}, start_label=("flow", "node1"))
raise Exception("can not be passed: fail of condition returned type")
except ValueError:
pass
try:
# fail of response returned Callable
pipeline = Pipeline.from_script(
{"flow": {"node1": {RESPONSE: lambda c, a: lambda x: 1, TRANSITIONS: {repeat(): true()}}}},
start_label=("flow", "node1"),
)
ctx = Context()
await pipeline.actor(pipeline, ctx)
raise Exception("can not be passed: fail of response returned Callable")
except ValueError:
pass
try:
# failed response
Pipeline.from_script(
{"flow": {"node1": {RESPONSE: raised_response, TRANSITIONS: {repeat(): true()}}}},
start_label=("flow", "node1"),
)
raise Exception("can not be passed: failed response")
except ValueError:
pass

# empty ctx stability
pipeline = Pipeline.from_script(
Expand Down

0 comments on commit deb0c30

Please sign in to comment.