modal-labs / synchronicity

Synchronicity lets you interoperate with asynchronous Python APIs.
Apache License 2.0
80 stars 3 forks source link

Type-stubs: add support for ParamSpec and forwarding typevars to wrapped methods #144

Closed freider closed 2 months ago

freider commented 4 months ago

TLDR: this will allow Modal's func.remote(...) and func.remote.aio(...) to autocomplete the arguments and return types of the underlying function wrapped by @app.function

Details:

A little bit of 🍝 code to get Generic typevars/paramspecs properly forwarded to the inner Protocols used for wrapped async methods, resulting in the type stub for this class:

T = TypeVar("T")
class Foo(Generic[T]):
     def method(self, t: T):
         ...

Resulting in code similar to this:

T = TypeVar("T")
T_INNER = TypeVar("T_INNER")
class Foo(Generic[T]):
    class MethodProtocol(typing.Protocol[T_INNER]):
        def __call__(self, t: T_INNER):
            ...
        def aio(self, t: T_INNER):
            ...

    method: MethodProtocol[T]