-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update script to handle Optional and Union input parameters (#1160)
**Pull Request Checklist** - [x] Fixes #1012 - [x] Tests added - [x] Documentation/examples added : I think no needs for document? - [x] [Good commit messages](https://cbea.ms/git-commit/) and/or PR title **Description of PR** Currently, `script` decorator cannot handle `Union` or `Optional` input parameters because of the error in `issubclass`, so updated it to be able to handle it as expected. --- original PR: #1147 --------- Signed-off-by: Ukjae Jeong <[email protected]> Signed-off-by: Ukjae Jeong <[email protected]> Co-authored-by: Elliot Gunton <[email protected]>
- Loading branch information
1 parent
3470e8e
commit 523a9b9
Showing
9 changed files
with
259 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import sys | ||
from typing import Optional, Union | ||
|
||
from hera.shared import global_config | ||
from hera.workflows import script | ||
|
||
global_config.experimental_features["script_annotations"] = True | ||
|
||
|
||
@script(constructor="runner") | ||
def optional_str_parameter(my_string: Optional[str] = None) -> Optional[str]: | ||
return my_string | ||
|
||
|
||
@script(constructor="runner") | ||
def optional_str_parameter_using_union(my_string: Union[None, str] = None) -> Union[None, str]: | ||
return my_string | ||
|
||
|
||
if sys.version_info[0] >= 3 and sys.version_info[1] >= 10: | ||
# Union types using OR operator are allowed since python 3.10. | ||
@script(constructor="runner") | ||
def optional_str_parameter_using_or(my_string: str | None = None) -> str | None: | ||
return my_string | ||
|
||
|
||
@script(constructor="runner") | ||
def optional_int_parameter(my_int: Optional[int] = None) -> Optional[int]: | ||
return my_int | ||
|
||
|
||
@script(constructor="runner") | ||
def union_parameter(my_param: Union[str, int] = None) -> Union[str, int]: | ||
return my_param |
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import Optional | ||
|
||
import pytest | ||
|
||
from hera.workflows import Parameter, Steps, Workflow, WorkflowsService, script | ||
from hera.workflows.models import ( | ||
NodeStatus, | ||
Parameter as ModelParameter, | ||
) | ||
|
||
|
||
@script(outputs=Parameter(name="message-out", value_from={"path": "/tmp/message-out"})) | ||
def print_msg(message: Optional[str] = None): | ||
with open("/tmp/message-out", "w") as f: | ||
f.write("Got: {}".format(message)) | ||
|
||
|
||
def get_workflow() -> Workflow: | ||
with Workflow( | ||
generate_name="optional-param-", | ||
entrypoint="steps", | ||
namespace="argo", | ||
workflows_service=WorkflowsService( | ||
host="https://localhost:2746", | ||
namespace="argo", | ||
verify_ssl=False, | ||
), | ||
) as w: | ||
with Steps(name="steps"): | ||
print_msg(name="step-1", arguments={"message": "Hello world!"}) | ||
print_msg(name="step-2", arguments={}) | ||
print_msg(name="step-3") | ||
|
||
return w | ||
|
||
|
||
@pytest.mark.on_cluster | ||
def test_create_workflow_with_optional_input_parameter(): | ||
model_workflow = get_workflow().create(wait=True) | ||
assert model_workflow.status and model_workflow.status.phase == "Succeeded" | ||
|
||
step_and_expected_output = { | ||
"step-1": "Got: Hello world!", | ||
"step-2": "Got: None", | ||
"step-3": "Got: None", | ||
} | ||
|
||
for step, expected_output in step_and_expected_output.items(): | ||
node: NodeStatus = next(filter(lambda n: n.display_name == step, model_workflow.status.nodes.values())) | ||
message_out: ModelParameter = next(filter(lambda n: n.name == "message-out", node.outputs.parameters)) | ||
assert message_out.value == expected_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters