-
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.
Support Literal types in script runner (#1249)
**Pull Request Checklist** - [X] Fixes #1173 - [X] Tests added - [ ] Documentation/examples added - [X] [Good commit messages](https://cbea.ms/git-commit/) and/or PR title **Description of PR** Currently, the script runner does not support `Literal` types, as `origin_type_issubtype` does not special-case them, so `_is_str_kwarg_of` does not return True. This PR adds supports for Literals to `origin_type_issubtype`, and additionally copies their values from the annotation into the `enum` field on input Parameters. It also combines two paths in `_get_inputs_from_callable` into one by using `construct_io_from_annotation` instead of having a fallback branch if `get_workflow_annotation` returns `None`; this fixes a bug where the default was being verified as None for optional strings only if there was no IO annotation. --------- Signed-off-by: Alice Purcell <[email protected]>
- Loading branch information
1 parent
52670c4
commit 4328576
Showing
10 changed files
with
164 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from typing import Annotated, Literal | ||
|
||
from hera.shared import global_config | ||
from hera.workflows import Parameter, Steps, Workflow, script | ||
|
||
global_config.experimental_features["script_annotations"] = True | ||
|
||
|
||
@script(constructor="runner") | ||
def literal_str( | ||
my_str: Annotated[Literal["foo", "bar"], Parameter(name="my-str")], | ||
) -> Annotated[Literal[1, 2], Parameter(name="index")]: | ||
return {"foo": 1, "bar": 2}[my_str] | ||
|
||
|
||
with Workflow(name="my-workflow", entrypoint="steps") as w: | ||
with Steps(name="steps"): | ||
literal_str() |
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,13 @@ | ||
from typing import Literal | ||
|
||
from hera.workflows import Steps, Workflow, script | ||
|
||
|
||
@script(constructor="runner") | ||
def literal_str(my_str: Literal["foo", "bar"]) -> Literal[1, 2]: | ||
return {"foo": 1, "bar": 2}[my_str] | ||
|
||
|
||
with Workflow(name="my-workflow", entrypoint="steps") as w: | ||
with Steps(name="steps"): | ||
literal_str() |
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,24 @@ | ||
from typing import Literal | ||
|
||
from hera.shared import global_config | ||
from hera.workflows import Input, Output, Steps, Workflow, script | ||
|
||
global_config.experimental_features["script_pydantic_io"] = True | ||
|
||
|
||
class ExampleInput(Input): | ||
my_str: Literal["foo", "bar"] | ||
|
||
|
||
class ExampleOutput(Output): | ||
index: Literal[1, 2] | ||
|
||
|
||
@script(constructor="runner") | ||
def literal_str(input: ExampleInput) -> ExampleOutput: | ||
return ExampleOutput(index={"foo": 1, "bar": 2}[input.my_str]) | ||
|
||
|
||
with Workflow(name="my-workflow", entrypoint="steps") as w: | ||
with Steps(name="steps"): | ||
literal_str() |
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
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