Skip to content

Commit

Permalink
Fix the annotation imports
Browse files Browse the repository at this point in the history
Signed-off-by: Mikolaj Deja <mdeja2@bloomberg.net>
  • Loading branch information
Mikolaj Deja committed Jul 11, 2023
1 parent 92b69a8 commit 47346eb
Show file tree
Hide file tree
Showing 8 changed files with 627 additions and 4 deletions.
128 changes: 128 additions & 0 deletions docs/examples/workflows/script_annotations_combined.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Script Annotations Combined






=== "Hera"

```python linenums="1"
from typing import Any, List, Optional

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore

from hera.shared._base_model import BaseModel
from hera.workflows import Workflow, script
from hera.workflows.steps import Steps


class InputParameter(BaseModel):
default: Optional[Any]
description: Optional[str]
enum: Optional[List[Any]]


# Old style
# @script(
# inputs=[
# Parameter(name="an_int", description="an_int parameter", default=1, enum=[1, 2, 3]),
# Parameter(name="a_bool", description="a_bool parameter", default=True, enum=[True, False]),
# Parameter(name="a_string", description="a_string parameter", default="a", enum=["a", "b", "c"])
# ]
# )
# def echo_all(an_int, a_bool, a_string):
# print(an_int)
# print(a_bool)
# print(a_string)


# New style
@script()
def echo_all(
an_int: Annotated[int, InputParameter(description="an_int parameter", default=1, enum=[1, 2, 3])],
a_bool: Annotated[bool, InputParameter(description="a_bool parameter", default=True, enum=[True, False])],
a_string: Annotated[str, InputParameter(description="a_string parameter", default="a", enum=["a", "b", "c"])],
):
print(an_int)
print(a_bool)
print(a_string)


with Workflow(generate_name="test-types-", entrypoint="my_steps") as w:
with Steps(name="my_steps") as s:
echo_all(an_int=1, a_bool=True, a_string="a")
```

=== "YAML"

```yaml linenums="1"
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: test-types-
spec:
entrypoint: my_steps
templates:
- name: my_steps
steps:
- - name: echo-all
template: echo-all
- inputs:
parameters:
- default: '1'
description: an_int parameter
enum:
- '1'
- '2'
- '3'
name: an_int
- default: 'true'
description: a_bool parameter
enum:
- 'True'
- 'False'
name: a_bool
- default: a
description: a_string parameter
enum:
- a
- b
- c
name: a_string
name: echo-all
script:
command:
- python
image: python:3.8
source: 'import os

import sys

sys.path.append(os.getcwd())

import json

try: a_bool = json.loads(r''''''{{inputs.parameters.a_bool}}'''''')

except: a_bool = r''''''{{inputs.parameters.a_bool}}''''''

try: a_string = json.loads(r''''''{{inputs.parameters.a_string}}'''''')

except: a_string = r''''''{{inputs.parameters.a_string}}''''''

try: an_int = json.loads(r''''''{{inputs.parameters.an_int}}'''''')

except: an_int = r''''''{{inputs.parameters.an_int}}''''''


print(an_int)

print(a_bool)

print(a_string)'
```

156 changes: 156 additions & 0 deletions docs/examples/workflows/script_annotations_default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# Script Annotations Default






=== "Hera"

```python linenums="1"
from typing import Any, List, Optional

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore

from hera.shared._base_model import BaseModel
from hera.workflows import Workflow, script
from hera.workflows.steps import Steps


class InputParameter(BaseModel):
default: Optional[Any]
description: Optional[str]
enum: Optional[List[Any]]


# Old style
#
# @script()
# def echo_int(an_int=1):
# print(an_int)

# @script()
# def echo_boolean(a_bool=True):
# print(a_bool)

# @script()
# def echo_string(a_string="a"):
# print(a_string)

# New style


@script()
def echo_int(an_int: Annotated[int, InputParameter(default=1)]):
print(an_int)


@script()
def echo_boolean(a_bool: Annotated[bool, InputParameter(default=True)]):
print(a_bool)


@script()
def echo_string(a_string: Annotated[str, InputParameter(default="a")]):
print(a_string)


with Workflow(generate_name="test-types-", entrypoint="my_steps") as w:
with Steps(name="my_steps") as s:
echo_int(an_int=1)
echo_boolean(a_bool=True)
echo_string(a_string="a")
```

=== "YAML"

```yaml linenums="1"
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: test-types-
spec:
entrypoint: my_steps
templates:
- name: my_steps
steps:
- - name: echo-int
template: echo-int
- - name: echo-boolean
template: echo-boolean
- - name: echo-string
template: echo-string
- inputs:
parameters:
- default: '1'
name: an_int
name: echo-int
script:
command:
- python
image: python:3.8
source: 'import os

import sys

sys.path.append(os.getcwd())

import json

try: an_int = json.loads(r''''''{{inputs.parameters.an_int}}'''''')

except: an_int = r''''''{{inputs.parameters.an_int}}''''''


print(an_int)'
- inputs:
parameters:
- default: 'true'
name: a_bool
name: echo-boolean
script:
command:
- python
image: python:3.8
source: 'import os

import sys

sys.path.append(os.getcwd())

import json

try: a_bool = json.loads(r''''''{{inputs.parameters.a_bool}}'''''')

except: a_bool = r''''''{{inputs.parameters.a_bool}}''''''


print(a_bool)'
- inputs:
parameters:
- default: a
name: a_string
name: echo-string
script:
command:
- python
image: python:3.8
source: 'import os

import sys

sys.path.append(os.getcwd())

import json

try: a_string = json.loads(r''''''{{inputs.parameters.a_string}}'''''')

except: a_string = r''''''{{inputs.parameters.a_string}}''''''


print(a_string)'
```

Loading

0 comments on commit 47346eb

Please sign in to comment.