Title
b

Bruno Murino

06/30/2021, 2:03 PM
Hi everyone — I have a task that returns a python dictionary, now I’m trying to use one of the elements of the dictionary as input for another task, but I’m getting an error.. Here’s a minimal working example:
import prefect
from prefect import task, Flow, Parameter, unmapped, case
from prefect.tasks.shell import ShellTask

shell_task = ShellTask(stream_output=True)

@task
def get_config():
    return {
        'path': '/root/',
    }

with Flow("test") as flow:
    config = get_config()
    bar = shell_task(command="pwd", helper_script = f"cd {config['path']}")

flow.run()
k

Kevin Kho

06/30/2021, 2:06 PM
Hey @Bruno Murino, tasks execution is deferred but that dictionary call is done during build time. So there won’t be
config['path']
to pull yet. In this case you need an intermediate task that takes in
config
and pulls out the
path
and returns it.
b

Bruno Murino

06/30/2021, 2:06 PM
hmmmmm, let me try that!
it worked! I ended up just enclosing a “shell_task.run()” inside another task, and I only pass the full config object to it and deal with that inside the task — I found this cleaner and more consistent with how I do other tasks
👍 1
1
k

Kevin Kho

06/30/2021, 2:20 PM
You can also do this in the flow:
task(lambda _x_: x['path'])(config)
👍 1