Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Improved type hints for map, starmap, for_each #2696

Open
Taytay opened this issue Dec 19, 2024 · 0 comments
Open

Feature request: Improved type hints for map, starmap, for_each #2696

Taytay opened this issue Dec 19, 2024 · 0 comments

Comments

@Taytay
Copy link

Taytay commented Dec 19, 2024

@freider , you seem to know more about Python type hints that well...anyone. I appreciate your work in PRs like
#2200

I was trying to declare new type hints for map, starmap, and for_each, because it bugs me that all function type information is lost if I call my modal functions in this way. I ran into some issues (that are no doubt the same issues you ran into) when trying to tell Python that a function like map takes "an iterator of the same type as the Params of the underlying function". The ParamSpec type appears to be very special in the way it is used, and I can't seem to coerce the P_INNER or P_INNER.args into anything useful for type hints on these.

The return-values for functions like map appear fixable though. It currently has:

    class __map_spec(typing_extensions.Protocol):
        def __call__(
            self, *input_iterators, kwargs={}, order_outputs: bool = True, return_exceptions: bool = False
        ) -> modal._utils.async_utils.AsyncOrSyncIterable: ...
        def aio(
            self,
            *input_iterators: typing.Union[typing.Iterable[typing.Any], typing.AsyncIterable[typing.Any]],
            kwargs={},
            order_outputs: bool = True,
            return_exceptions: bool = False,
        ) -> typing.AsyncGenerator[typing.Any, None]: ...

    map: __map_spec

But I think you could make use of the ReturnType for both of those functions (although it would likely require making AsyncOrSyncIterable a generic as well).

So, I don't know if it's possible to have type hints for these map-like functions, but boy it would be cool. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant