dispatchrun / dispatch-py

Python package to develop applications with Dispatch.
https://pypi.org/project/dispatch-py/
Apache License 2.0
56 stars 3 forks source link

Introduce higher-level @dispatch.function decorator #38

Closed chriso closed 9 months ago

chriso commented 9 months ago

This PR reworks the API so that the default registration strategy (via @dispatch.function) lets you register arbitrary functions, and not just those that accept dispatch.function.Input and return dispatch.function.Output.

@dispatch.function()
def hello(name: str) -> str:
    return f"Hello, {name}!"

# dispatch a call to the function
dispatch.call(hello, "Chris")

The previous registration decorator is now named @dispatch.primitive_function.

When you use the higher level decorator, it generates a wrapper for the function that makes it conform to (Input) -> Output. It's then registered with @dispatch.primitive_function. Exceptions are caught by the wrapper and converted to an Output. Errors and output values are automatically categorized in order to derive a Status for the Dispatch orchestrator.

Note that you must use @dispatch.primitive_function in order to access all of the capabilities of Dispatch. For example, you may want to tail call or poll. In a follow-up PR I will add a @dispatch.coroutine registration strategy that exposes this functionality in a higher-level way.

As part of this PR, I've consolidated the categorization of errors and output values into the dispatch.status file, to make it easy to add extensions (in a follow-up PR).