Skip to content

Commit

Permalink
Create a new experimental flag
Browse files Browse the repository at this point in the history
Add a new experimental flag to gate use of Pydantic types in a workflow context manager

Signed-off-by: Alice Purcell <alicederyn@gmail.com>
  • Loading branch information
alicederyn committed Oct 16, 2024
1 parent f201ef1 commit 5969aa5
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from hera.workflows import DAG, Parameter, WorkflowTemplate, script
from hera.workflows.io.v1 import Input, Output

global_config.experimental_features["decorator_syntax"] = True
global_config.experimental_features["context_manager_pydantic_io"] = True


class CutInput(Input):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from hera.workflows import Parameter, Steps, WorkflowTemplate, script
from hera.workflows.io.v1 import Input, Output

global_config.experimental_features["decorator_syntax"] = True
global_config.experimental_features["context_manager_pydantic_io"] = True


class CutInput(Input):
Expand Down
9 changes: 5 additions & 4 deletions src/hera/shared/_global_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
TypeTBase = Type[TBase]

Hook = Callable[[TBase], TBase]
"""`Hook` is a callable that takes a Hera objects and returns the same, optionally mutated, object.
"""`Hook` is a callable that takes a Hera objects and returns the same, optionally mutated, object.
This can be a Workflow, a Script, a Container, etc - any Hera object.
This can be a Workflow, a Script, a Container, etc - any Hera object.
"""

_HookMap = Dict[TypeTBase, List[Hook]]
Expand Down Expand Up @@ -202,14 +202,15 @@ def _set_defaults(cls, values):
_SCRIPT_ANNOTATIONS_FLAG = "script_annotations"
_SCRIPT_PYDANTIC_IO_FLAG = "script_pydantic_io"
_DECORATOR_SYNTAX_FLAG = "decorator_syntax"
_CONTEXT_MANAGER_PYDANTIC_IO_FLAG = "context_manager_pydantic_io"
_SUPPRESS_PARAMETER_DEFAULT_ERROR_FLAG = "suppress_parameter_default_error"

# A dictionary where each key is a flag that has a list of flags which supersede it, hence
# the given flag key can also be switched on by any of the flags in the list. Using simple flat lists
# for now, otherwise with many superseding flags we may want to have a recursive structure.
_SUPERSEDING_FLAGS: Dict[str, List] = {
_SCRIPT_ANNOTATIONS_FLAG: [_SCRIPT_PYDANTIC_IO_FLAG, _DECORATOR_SYNTAX_FLAG],
_SCRIPT_PYDANTIC_IO_FLAG: [_DECORATOR_SYNTAX_FLAG],
_SCRIPT_ANNOTATIONS_FLAG: [_SCRIPT_PYDANTIC_IO_FLAG, _DECORATOR_SYNTAX_FLAG, _CONTEXT_MANAGER_PYDANTIC_IO_FLAG],
_SCRIPT_PYDANTIC_IO_FLAG: [_DECORATOR_SYNTAX_FLAG, _CONTEXT_MANAGER_PYDANTIC_IO_FLAG],
_DECORATOR_SYNTAX_FLAG: [],
}

Expand Down
6 changes: 3 additions & 3 deletions src/hera/workflows/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from hera.expr import g
from hera.shared import BaseMixin, global_config
from hera.shared._global_config import (
_DECORATOR_SYNTAX_FLAG,
_CONTEXT_MANAGER_PYDANTIC_IO_FLAG,
_SCRIPT_ANNOTATIONS_FLAG,
_SCRIPT_PYDANTIC_IO_FLAG,
_SUPPRESS_PARAMETER_DEFAULT_ERROR_FLAG,
Expand Down Expand Up @@ -775,10 +775,10 @@ def task_wrapper(*args, **kwargs) -> Union[FuncR, Step, Task, None]:
"""Invokes a `Script` object's `__call__` method using the given SubNode (Step or Task) args/kwargs."""
if _context.active:
if len(args) == 1 and isinstance(args[0], (InputV1, InputV2)):
if not _flag_enabled(_DECORATOR_SYNTAX_FLAG):
if not _flag_enabled(_CONTEXT_MANAGER_PYDANTIC_IO_FLAG):
raise SyntaxError(
"Cannot pass a Pydantic type inside a context. "
+ _enable_experimental_feature_msg(_DECORATOR_SYNTAX_FLAG)
+ _enable_experimental_feature_msg(_CONTEXT_MANAGER_PYDANTIC_IO_FLAG)
)
arguments = args[0]._get_as_arguments()
arguments_list = [
Expand Down
4 changes: 2 additions & 2 deletions tests/test_pydantic_io_workflow_syntax.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from hera.shared._global_config import _DECORATOR_SYNTAX_FLAG
from hera.shared._global_config import _CONTEXT_MANAGER_PYDANTIC_IO_FLAG
from hera.workflows import Input, Output, Steps, Workflow, script


Expand All @@ -14,7 +14,7 @@ class IntOutput(Output):

@pytest.fixture(autouse=True)
def enable_pydantic_io(global_config_fixture):
global_config_fixture.experimental_features[_DECORATOR_SYNTAX_FLAG] = True
global_config_fixture.experimental_features[_CONTEXT_MANAGER_PYDANTIC_IO_FLAG] = True


def test_output_field_contains_argo_template(global_config_fixture):
Expand Down

0 comments on commit 5969aa5

Please sign in to comment.