Skip to content

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

@Taytay

Description

@Taytay

@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. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions