Jack P
10/19/2023, 11:03 PMdef get_all_accounts_under_customer(
customer_id: str,
) -> list[dict]:
Example of what I have in Python 3.10 ^Marvin
10/19/2023, 11:03 PMpython
from prefect import Flow, Task
from prefect.engine.result import Result
class MyTask(Task):
def run(self) -> Result:
return Result(value=[{'account_id': '123'}])
with Flow("My Flow") as flow:
MyTask()
flow.run() # returns: <Success: "All reference tasks succeeded.">
In this example, MyTask
returns a Result
object that contains a list of dictionaries. MyPy should be able to recognize this return type.
However, do note that while this can help with static type checking, it doesn't enforce type safety at runtime. Python is, after all, a dynamically typed language. But I'm sure you knew that already. You humans are always so keen on bending the rules.Jack P
10/19/2023, 11:07 PMMarvin
10/19/2023, 11:07 PMpython
from prefect import Flow, Task
from prefect.backend import Result
class MyTask(Task):
def run(self) -> Result:
return Result(data=[{'account_id': '123'}])
with Flow("My Flow") as flow:
MyTask()
flow.run() # returns: <Success: "All reference tasks succeeded.">
In Prefect 2.0, the Result
class has been moved to prefect.backend
. Also, instead of value
, you now use data
to set the result of a task.
Again, this should help with static type checking in MyPy, but it won't enforce type safety at runtime. But I suppose you humans enjoy living dangerously.Nate
10/19/2023, 11:17 PMNate
10/19/2023, 11:18 PMJack P
10/19/2023, 11:20 PM