Skip to content

Commit

Permalink
Also copy Literals outside of experimental feature
Browse files Browse the repository at this point in the history
Signed-off-by: Alice Purcell <[email protected]>
  • Loading branch information
alicederyn committed Oct 28, 2024
1 parent ec24caf commit 3ee9ef3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/hera/shared/_type_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ def get_workflow_annotation(annotation: Any) -> Optional[Union[Artifact, Paramet
return metadata[0]


def add_metadata_from_type(parameter: Parameter, annotation: Any) -> None:
if not parameter.enum:
type_ = unwrap_annotation(annotation)

Check warning on line 88 in src/hera/shared/_type_util.py

View check run for this annotation

Codecov / codecov/patch

src/hera/shared/_type_util.py#L88

Added line #L88 was not covered by tests
if get_origin(type_) is Literal:
parameter.enum = list(get_args(type_))

Check warning on line 90 in src/hera/shared/_type_util.py

View check run for this annotation

Codecov / codecov/patch

src/hera/shared/_type_util.py#L90

Added line #L90 was not covered by tests


def construct_io_from_annotation(python_name: str, annotation: Any) -> Union[Parameter, Artifact]:
"""Constructs a Parameter or Artifact object based on annotations.
Expand Down
2 changes: 2 additions & 0 deletions src/hera/workflows/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
)
from hera.shared._pydantic import _PYDANTIC_VERSION, root_validator, validator
from hera.shared._type_util import (
add_metadata_from_type,
construct_io_from_annotation,
get_workflow_annotation,
is_subscripted,
Expand Down Expand Up @@ -379,6 +380,7 @@ def _get_parameters_from_callable(source: Callable) -> List[Parameter]:
default = MISSING

param = Parameter(name=p.name, default=default)
add_metadata_from_type(param, p.annotation)

Check warning on line 383 in src/hera/workflows/script.py

View check run for this annotation

Codecov / codecov/patch

src/hera/workflows/script.py#L383

Added line #L383 was not covered by tests
parameters.append(param)

return parameters
Expand Down
6 changes: 4 additions & 2 deletions tests/test_script_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,12 @@ def test_script_with_param(global_config_fixture, module_name):
pytest.param("tests.script_annotations.pydantic_io_literals", "my_str", id="pydantic-io"),
],
)
def test_script_literals(global_config_fixture, module_name, input_name):
@pytest.mark.parametrize("experimental_feature", ["", "script_annotations", "script_pydantic_io"])
def test_script_literals(global_config_fixture, module_name, input_name, experimental_feature):
"""Test that Literals work correctly as direct type annotations."""
# GIVEN
global_config_fixture.experimental_features["script_annotations"] = True
if experimental_feature:
global_config_fixture.experimental_features[experimental_feature] = True

# Force a reload of the test module, as the runner performs "importlib.import_module", which
# may fetch a cached version
Expand Down

0 comments on commit 3ee9ef3

Please sign in to comment.