dispatchrun / dispatch-py

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

Improve type safety with ParamSpec #100

Closed chriso closed 6 months ago

chriso commented 6 months ago

This PR improves type safety by using ParamSpec for the registration decorators.

mypy now rejects invalid arguments to __call__ or to .dispatch on the decorated function:

@dispatch.function
async def foo(a: int, b: str = "foo") -> list[str]:
    return [b] * a

foo(1, "abc")              # ok
foo.dispatch(2, b="xyz")   # ok
foo("oops")                # error: Expected type 'int', got 'str' instead

I added py.typed files to our packages to indicate that type information is available. See https://peps.python.org/pep-0561/ for more information.

I had to make one breaking change, which is that the @dispatch.function and @dispatch.primitive_function decorators no longer accept parameters and return a decorator. These must be used as decorators directly, e.g. @dispatch.function works but @dispatch.function() no longer works as a decorator.

cc #99