-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RunnerInput/Output Pydantic V2 classes (#938)
Part of #858, follow up to #920 * Enables users to use Pydantic V2 objects in their scripts while maintaining V1 usage internally for Hera * RunnerInput/Output classes are created in hera.workflows.io depending on the value of _PYDANTIC_VERSION - users will automatically get classes using their (possibly pinned) version of Pydantic * I have not yet managed to get an explicit test that uses the automatic import of V1 classes - we may just have to rely on the Pydantic V1 CI check. --------- Signed-off-by: Elliot Gunton <[email protected]>
- Loading branch information
1 parent
c124309
commit b146956
Showing
16 changed files
with
464 additions
and
63 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,12 @@ | ||
"""Hera IO models.""" | ||
from importlib.util import find_spec | ||
|
||
if find_spec("pydantic.v1"): | ||
from hera.workflows.io.v2 import RunnerInput, RunnerOutput | ||
else: | ||
from hera.workflows.io.v1 import RunnerInput, RunnerOutput # type: ignore | ||
|
||
__all__ = [ | ||
"RunnerInput", | ||
"RunnerOutput", | ||
] |
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,110 @@ | ||
"""Pydantic V2 input/output models for the Hera runner. | ||
RunnerInput/Output are only defined in this file if Pydantic v2 is installed. | ||
""" | ||
from collections import ChainMap | ||
from typing import Any, List, Optional, Union | ||
|
||
from hera.shared.serialization import serialize | ||
from hera.workflows.artifact import Artifact | ||
from hera.workflows.parameter import Parameter | ||
|
||
try: | ||
from inspect import get_annotations # type: ignore | ||
except ImportError: | ||
from hera.workflows._inspect import get_annotations # type: ignore | ||
|
||
try: | ||
from typing import Annotated, get_args, get_origin # type: ignore | ||
except ImportError: | ||
from typing_extensions import Annotated, get_args, get_origin # type: ignore | ||
|
||
from importlib.util import find_spec | ||
|
||
if find_spec("pydantic.v1"): | ||
from pydantic import BaseModel | ||
|
||
class RunnerInput(BaseModel): | ||
"""Input model usable by the Hera Runner. | ||
RunnerInput is a Pydantic model which users can create a subclass of. When a subclass | ||
of RunnerInput is used as a function parameter type, the Hera Runner will take the fields | ||
of the user's subclass to create template input parameters and artifacts. See the example | ||
for the script_pydantic_io experimental feature. | ||
""" | ||
|
||
@classmethod | ||
def _get_parameters(cls, object_override: "Optional[RunnerInput]" = None) -> List[Parameter]: | ||
parameters = [] | ||
annotations = {k: v for k, v in ChainMap(*(get_annotations(c) for c in cls.__mro__)).items()} | ||
|
||
for field in cls.model_fields: # type: ignore | ||
if get_origin(annotations[field]) is Annotated: | ||
if isinstance(get_args(annotations[field])[1], Parameter): | ||
param = get_args(annotations[field])[1] | ||
if object_override: | ||
param.default = serialize(getattr(object_override, field)) | ||
elif cls.model_fields[field].default: # type: ignore | ||
# Serialize the value (usually done in Parameter's validator) | ||
param.default = serialize(cls.model_fields[field].default) # type: ignore | ||
parameters.append(param) | ||
else: | ||
# Create a Parameter from basic type annotations | ||
if object_override: | ||
parameters.append(Parameter(name=field, default=serialize(getattr(object_override, field)))) | ||
else: | ||
parameters.append(Parameter(name=field, default=cls.model_fields[field].default)) # type: ignore | ||
return parameters | ||
|
||
@classmethod | ||
def _get_artifacts(cls) -> List[Artifact]: | ||
artifacts = [] | ||
annotations = {k: v for k, v in ChainMap(*(get_annotations(c) for c in cls.__mro__)).items()} | ||
|
||
for field in cls.model_fields: # type: ignore | ||
if get_origin(annotations[field]) is Annotated: | ||
if isinstance(get_args(annotations[field])[1], Artifact): | ||
artifact = get_args(annotations[field])[1] | ||
if artifact.path is None: | ||
artifact.path = artifact._get_default_inputs_path() | ||
artifacts.append(artifact) | ||
return artifacts | ||
|
||
class RunnerOutput(BaseModel): | ||
"""Output model usable by the Hera Runner. | ||
RunnerOutput is a Pydantic model which users can create a subclass of. When a subclass | ||
of RunnerOutput is used as a function return type, the Hera Runner will take the fields | ||
of the user's subclass to create template output parameters and artifacts. See the example | ||
for the script_pydantic_io experimental feature. | ||
""" | ||
|
||
exit_code: int = 0 | ||
result: Any = None | ||
|
||
@classmethod | ||
def _get_outputs(cls) -> List[Union[Artifact, Parameter]]: | ||
outputs = [] | ||
annotations = {k: v for k, v in ChainMap(*(get_annotations(c) for c in cls.__mro__)).items()} | ||
|
||
for field in cls.model_fields: # type: ignore | ||
if field in {"exit_code", "result"}: | ||
continue | ||
if get_origin(annotations[field]) is Annotated: | ||
if isinstance(get_args(annotations[field])[1], (Parameter, Artifact)): | ||
outputs.append(get_args(annotations[field])[1]) | ||
else: | ||
# Create a Parameter from basic type annotations | ||
outputs.append(Parameter(name=field, default=cls.model_fields[field].default)) # type: ignore | ||
return outputs | ||
|
||
@classmethod | ||
def _get_output(cls, field_name: str) -> Union[Artifact, Parameter]: | ||
annotations = {k: v for k, v in ChainMap(*(get_annotations(c) for c in cls.__mro__)).items()} | ||
annotation = annotations[field_name] | ||
if get_origin(annotation) is Annotated: | ||
if isinstance(get_args(annotation)[1], (Parameter, Artifact)): | ||
return get_args(annotation)[1] | ||
|
||
# Create a Parameter from basic type annotations | ||
return Parameter(name=field_name, default=cls.model_fields[field_name].default) # type: ignore |
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
Oops, something went wrong.