is there a clean way to invoke tasks as functions?...
# prefect-community
a
is there a clean way to invoke tasks as functions? e.g. for unit testing outside a prefect flow? The cleanest way I could think of is to define the function regularly (without the
task
decorator) and treat them as tasks only inside the flow. i.e.:
Copy code
def f():
    pass

# regular call
foo = f()

# inside a flow
with Flow() as flow:
    foo = task(f)()
any nicer suggestions?
j
Hi @Avi A, you can call task.run() to invoke the task as a function. All the “Prefect stuff” happens in the
TaskRunner
class to preserve easy testing of tasks!
👏 1
Copy code
@task
def f(x, y):
    return x + y

f.run(1,2) == 3
hattip 1
l
For some extra inspo while you are working on testing, you might be interested in these two pages in the docs too! https://docs.prefect.io/core/idioms/testing-flows.html#testing-prefect-flows-and-tasks https://docs.prefect.io/core/advanced_tutorials/local-debugging.html
upvote 1
👏 1
a
Thanks guys, I adore your work!
👍 1
🤗 1
j
@Marvin archive “Is there an easy way to test a task as a function?”
😍 1