hera icon indicating copy to clipboard operation
hera copied to clipboard

Error messages improvement

Open ljyanesm opened this issue 1 year ago • 0 comments

Currently when one is creating a Workflow with steps that may be defined elsewhere, for example having a separate python function that defines the Steps and invoking it from a Workflow users may try to instantiate the Steps outside the context of the Workflow leading to a non-intuitive error message.

For example:

@script()
def echo(message: str):
    print(message)

def my_steps_def():
    with Steps(name="steps") as my_steps:
        echo(arguments={"message": "Hello world!"})
    return my_steps


my_steps = my_steps_def()
with Workflow(
    generate_name="hello-world-",
    entrypoint="steps",
) as wf:
    my_steps

if __name__ == "__main__":
    # dump as yaml for inspection
    with pathlib.Path(__file__).with_suffix(".yaml").open("w") as fh:
        wf.to_yaml(fh)

Leads to the following stacktrace:

Traceback (most recent call last):
  File "/testing/hera-examples/reusable-steps-error-message.py", line 18, in <module>
    my_steps = my_steps_def()
               ^^^^^^^^^^^^^^
  File "/testing/hera-examples/reusable-steps-error-message.py", line 14, in my_steps_def
    echo(arguments={"message": "Hello world!"})
  File "/testing/.venv/lib/python3.11/site-packages/hera/workflows/script.py", line 599, in task_wrapper
    return s.__call__(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/testing/.venv/lib/python3.11/site-packages/hera/workflows/_mixins.py", line 667, in __call__
    return Step(*args, template=self, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pydantic/main.py", line 349, in pydantic.main.BaseModel.__init__
  File "/testing/.venv/lib/python3.11/site-packages/hera/shared/_global_config.py", line 150, in _init_private_attributes
    self.__hera_init__()
  File "/testing/.venv/lib/python3.11/site-packages/hera/workflows/_context.py", line 28, in __hera_init__
    _context.add_sub_node(self)
  File "/testing/.venv/lib/python3.11/site-packages/hera/workflows/_context.py", line 93, in add_sub_node
    for t in pieces[0].templates:  # type: ignore
             ^^^^^^^^^^^^^^^^^^^
AttributeError: 'Steps' object has no attribute 'templates'

The error message above could be improved by suggesting the user that the object that is being instantiated should be within a Workflow context.

ljyanesm avatar Jan 11 '24 10:01 ljyanesm