-
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 inherited fields for RunnerInput, other niceties (#1093)
## Pull Request Checklist - [ ] Fixes #<!--issue number goes here--> - [x] Tests added - [ ] Documentation/examples added - [x] [Good commit messages](https://cbea.ms/git-commit/) and/or PR title ## Summary Currently, users cannot leverage inheritance when defining `RunnerInput` models that utilize `Annotated` fields. For example: ``` class ResourceIndex(RunnerInput): resource_name: Annotated[str, Parameter(name="resource-name")] resource_namespace: Annotated[str, Parameter(name="resource-namespace")] class GetStatusInput(ResourceIndex): wait: Annotated[bool, Parameter(name="wait")] timeout_s: Annotated[int, Parameter(name="timeout")] ``` This would fail at runtime with a KeyError at [this line](https://github.com/argoproj-labs/hera/blob/main/src/hera/workflows/_runner/script_annotations_util.py#L152), because apparently `cls.__annotations__` are not inherited by child classes. This PR enables the above inheritance structure by utilizing the existing [get_field_annotations](https://github.com/argoproj-labs/hera/blob/main/src/hera/shared/_pydantic.py#L69) instead of `my_runner_input.__annotations__`. #### Parameter Default Names This PR also changes logic so that `Input` field `Parameters` adopt the name of their field if no name is provided. For example, in ``` class MyInput(Input): my_int: Annotated[int, Parameter(description="this is my int")] ``` The parameter is converted to `Parameter(name="my_int", description="this is my int")`. This avoids an error from [here](https://github.com/argoproj-labs/hera/blob/main/src/hera/workflows/parameter.py#L30) which is thrown when trying to generate a workflow yaml. --------- Signed-off-by: Mitchell Douglass <mdouglass15@bloomberg.net> Co-authored-by: Sambhav Kothari <skothari44@bloomberg.net>
- Loading branch information
Showing
4 changed files
with
128 additions
and
16 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,21 @@ | ||
from hera.workflows.io import Input | ||
from hera.workflows.parameter import Parameter | ||
|
||
try: | ||
from typing import Annotated | ||
except ImportError: | ||
from typing_extensions import Annotated | ||
|
||
|
||
def test_input_mixin_get_parameters(): | ||
class Foo(Input): | ||
foo: Annotated[int, Parameter(name="foo")] | ||
|
||
assert Foo._get_parameters() == [Parameter(name="foo")] | ||
|
||
|
||
def test_input_mixin_get_parameters_default_name(): | ||
class Foo(Input): | ||
foo: Annotated[int, Parameter(description="a foo")] | ||
|
||
assert Foo._get_parameters() == [Parameter(name="foo", description="a foo")] |
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