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

Support list comprehension for TypeVarTuple #1745

Closed
zen-xu opened this issue May 23, 2024 · 2 comments
Closed

Support list comprehension for TypeVarTuple #1745

zen-xu opened this issue May 23, 2024 · 2 comments
Labels
topic: other Other topics not covered

Comments

@zen-xu
Copy link

zen-xu commented May 23, 2024

Imagine there's a scenario, I need to accept a bunch of different type objects, and then return these objects boxed in a tuple of a generic type. Currently, this can only be achieved by manually writing overload, is there a more pythonic solution?

from typing import Generic
from typing import TypeVar
from typing import overload


_T = TypeVar("_T")
_T0 = TypeVar("_T0")
_T1 = TypeVar("_T1")
_T2 = TypeVar("_T2")


class ObjectRef(Generic[_T]): ...


@overload
def put(items: tuple[_T0]) -> tuple[ObjectRef[_T0]]: ...


@overload
def put(items: tuple[_T0, _T1]) -> tuple[ObjectRef[_T0], ObjectRef[_T1]]: ...


@overload
def put(
    items: tuple[_T0, _T1, _T2],
) -> tuple[ObjectRef[_T0], ObjectRef[_T1], ObjectRef[_T2]]: ...


@overload
def get(refs: tuple[ObjectRef[_T0]]) -> tuple[_T0]: ...


@overload
def get(refs: tuple[ObjectRef[_T0], ObjectRef[_T1]]) -> tuple[_T0, _T1]: ...


@overload
def get(
    refs: tuple[ObjectRef[_T0], ObjectRef[_T1], ObjectRef[_T2]],
) -> tuple[_T0, _T1, _T2]: ...
@zen-xu zen-xu added the topic: other Other topics not covered label May 23, 2024
@zen-xu
Copy link
Author

zen-xu commented May 23, 2024

If TypeVarTuple supports list comprehension, that would be great

from typing import Generic
from typing import TypeVar
from typing import TypeVarTuple


_T = TypeVar("_T")
_Ts = TypeVarTuple("_Ts")


class ObjectRef(Generic[_T]): ...

def put(items: tuple[*_Ts]) -> tuple[ObjectRef[_T] for _T in _Ts]: ...

# backport to old python version
# def put(items: tuple[*_Ts]) -> tuple["ObjectRef[_T] for _T in _Ts"]: ...


def get(refs: tuple[*_Ts]) -> tuple[_T for ObjectRef[_T] in _Ts]: ...

We can do more things like.

from typing import Any
from typing import Callable
from typing import Generic
from typing import TypeVar
from typing import TypeVarTuple


_Callable_co = TypeVar("_Callable_co", covariant=True, bound=Callable)
_Ts = TypeVarTuple("_Ts")
_T = TypeVar("_T")


class ObjectRef(Generic[_T]): ...


class RemoteCallable(Generic[_Callable_co]):
    def __call__(self: RemoteCallable[Callable[[*_Ts], Any]], *args: *tuple[ObjectRef[_T] for _T in _Ts]): ...

For a more realistic scenario, you can refer to https://github.com/zen-xu/sunray/blob/main/sunray/_internal/callable.py

@zen-xu zen-xu changed the title How to box different tuple item into another generic type Support list comprehension for TypeVarTuple May 23, 2024
@carljm
Copy link
Member

carljm commented May 23, 2024

I think this is a duplicate of #1216

@carljm carljm closed this as not planned Won't fix, can't repro, duplicate, stale May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: other Other topics not covered
Projects
None yet
Development

No branches or pull requests

2 participants