From f012868ef16f31a3fd00c9ac770be9abb10297d0 Mon Sep 17 00:00:00 2001 From: Alice Purcell Date: Thu, 5 Sep 2024 18:43:22 +0100 Subject: [PATCH] Move new syntax behind decorator_syntax feature Signed-off-by: Alice Purcell --- .../pydantic_io_in_dag_context.md | 2 +- .../pydantic_io_in_steps_context.md | 2 +- .../pydantic_io_in_dag_context.py | 2 +- .../pydantic_io_in_steps_context.py | 2 +- src/hera/workflows/script.py | 22 ++++++++++++++----- ...py => test_pydantic_io_workflow_syntax.py} | 4 ++-- 6 files changed, 22 insertions(+), 12 deletions(-) rename tests/{test_pydantic_io_syntax.py => test_pydantic_io_workflow_syntax.py} (91%) diff --git a/docs/examples/workflows/experimental/pydantic_io_in_dag_context.md b/docs/examples/workflows/experimental/pydantic_io_in_dag_context.md index 2b0d97d6e..d5c58e8de 100644 --- a/docs/examples/workflows/experimental/pydantic_io_in_dag_context.md +++ b/docs/examples/workflows/experimental/pydantic_io_in_dag_context.md @@ -21,7 +21,7 @@ from hera.workflows import DAG, Parameter, WorkflowTemplate, script from hera.workflows.io.v1 import Input, Output - global_config.experimental_features["script_pydantic_io"] = True + global_config.experimental_features["decorator_syntax"] = True class CutInput(Input): diff --git a/docs/examples/workflows/experimental/pydantic_io_in_steps_context.md b/docs/examples/workflows/experimental/pydantic_io_in_steps_context.md index d0cf9b977..6bc6d7bbe 100644 --- a/docs/examples/workflows/experimental/pydantic_io_in_steps_context.md +++ b/docs/examples/workflows/experimental/pydantic_io_in_steps_context.md @@ -21,7 +21,7 @@ from hera.workflows import Parameter, Steps, WorkflowTemplate, script from hera.workflows.io.v1 import Input, Output - global_config.experimental_features["script_pydantic_io"] = True + global_config.experimental_features["decorator_syntax"] = True class CutInput(Input): diff --git a/examples/workflows/experimental/pydantic_io_in_dag_context.py b/examples/workflows/experimental/pydantic_io_in_dag_context.py index 8066d4eca..6f1e802f4 100644 --- a/examples/workflows/experimental/pydantic_io_in_dag_context.py +++ b/examples/workflows/experimental/pydantic_io_in_dag_context.py @@ -11,7 +11,7 @@ from hera.workflows import DAG, Parameter, WorkflowTemplate, script from hera.workflows.io.v1 import Input, Output -global_config.experimental_features["script_pydantic_io"] = True +global_config.experimental_features["decorator_syntax"] = True class CutInput(Input): diff --git a/examples/workflows/experimental/pydantic_io_in_steps_context.py b/examples/workflows/experimental/pydantic_io_in_steps_context.py index 2517b20f8..be297b897 100644 --- a/examples/workflows/experimental/pydantic_io_in_steps_context.py +++ b/examples/workflows/experimental/pydantic_io_in_steps_context.py @@ -11,7 +11,7 @@ from hera.workflows import Parameter, Steps, WorkflowTemplate, script from hera.workflows.io.v1 import Input, Output -global_config.experimental_features["script_pydantic_io"] = True +global_config.experimental_features["decorator_syntax"] = True class CutInput(Input): diff --git a/src/hera/workflows/script.py b/src/hera/workflows/script.py index 497e60821..b1b7ceb00 100644 --- a/src/hera/workflows/script.py +++ b/src/hera/workflows/script.py @@ -41,6 +41,7 @@ from hera.expr import g from hera.shared import BaseMixin, global_config from hera.shared._global_config import ( + _DECORATOR_SYNTAX_FLAG, _SCRIPT_ANNOTATIONS_FLAG, _SCRIPT_PYDANTIC_IO_FLAG, _SUPPRESS_PARAMETER_DEFAULT_ERROR_FLAG, @@ -379,15 +380,19 @@ def _get_parameters_from_callable(source: Callable) -> List[Parameter]: return parameters +def please_enable_experimental_feature(flag: str) -> str: + return ( + "Please turn on experimental features by setting " + f'`hera.shared.global_config.experimental_features["{flag}"] = True`.' + " Note that experimental features are unstable and subject to breaking changes." + ) + + def _assert_pydantic_io_enabled(annotation: str) -> None: if not _flag_enabled(_SCRIPT_PYDANTIC_IO_FLAG): raise ValueError( - ( - "Unable to instantiate {} since it is an experimental feature." - " Please turn on experimental features by setting " - '`hera.shared.global_config.experimental_features["{}"] = True`.' - " Note that experimental features are unstable and subject to breaking changes." - ).format(annotation, _SCRIPT_PYDANTIC_IO_FLAG) + f"Unable to instantiate {annotation} since it is an experimental feature. " + + please_enable_experimental_feature(_SCRIPT_PYDANTIC_IO_FLAG) ) @@ -770,6 +775,11 @@ 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): + raise SyntaxError( + "Cannot pass a Pydantic type inside a context. " + + please_enable_experimental_feature(_DECORATOR_SYNTAX_FLAG) + ) arguments = args[0]._get_as_arguments() arguments_list = [ *(arguments.artifacts or []), diff --git a/tests/test_pydantic_io_syntax.py b/tests/test_pydantic_io_workflow_syntax.py similarity index 91% rename from tests/test_pydantic_io_syntax.py rename to tests/test_pydantic_io_workflow_syntax.py index cf9e16735..353a9c2e7 100644 --- a/tests/test_pydantic_io_syntax.py +++ b/tests/test_pydantic_io_workflow_syntax.py @@ -1,6 +1,6 @@ import pytest -from hera.shared._global_config import _SCRIPT_PYDANTIC_IO_FLAG +from hera.shared._global_config import _DECORATOR_SYNTAX_FLAG from hera.workflows import Input, Output, Steps, Workflow, script @@ -14,7 +14,7 @@ class IntOutput(Output): @pytest.fixture(autouse=True) def enable_pydantic_io(global_config_fixture): - global_config_fixture.experimental_features[_SCRIPT_PYDANTIC_IO_FLAG] = True + global_config_fixture.experimental_features[_DECORATOR_SYNTAX_FLAG] = True def test_output_field_contains_argo_template(global_config_fixture):