Josh

    Josh

    1 year ago
    Are task
    run
    methods allowed to return arbitrary objects?
    class ReturnObject:
      def__init__(self, value: str = "hello", list: List = ["world"]):
        self.value = value
        self.list = list
    
    class MyTask(Task):
      def run(self) -> ReturnObject:
        return ReturnObject("some value", ["a", "b", "c"])
    
    task = MyTask()
    
    with Flow() as flow:
      result = task()
      some_other_task(param1=result.value, param2=result.list)
    If I try to run this code, I’ll get an error like
    AttributeError: 'MyTask' object has no attribute 'value'
    If I want to return different type objects from a task to be used in subsequent tasks, what is the Prefect way of doing this?
    Chris White

    Chris White

    1 year ago
    Hi Josh - you’re conflating runtime with build time; the result of the first task isn’t available until you’ve run the code, the flow context manager is only building the dependency structure. You can return arbitrary objects from tasks but you cannot access their attributes as you’re doing here — instead you should perform that attribute access within
    some_other_task