Skip to content

TypeVarTuple and generic ParamSpec #1135

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

Closed
cdce8p opened this issue Apr 15, 2022 · 1 comment
Closed

TypeVarTuple and generic ParamSpec #1135

cdce8p opened this issue Apr 15, 2022 · 1 comment
Labels
topic: other Other topics not covered

Comments

@cdce8p
Copy link
Contributor

cdce8p commented Apr 15, 2022

I've recently been trying to add better type hints to call_job below. The Job class is basically a wrapper for a callable with some small additions.

from typing import Callable, Generic, Any
from typing_extensions import ParamSpec

_P = ParamSpec("_P")

class Job(Generic[_P]):
    def __init__(self, target: Callable[_P, None]) -> None:
        self.target = target

def call_job(
    job: Job[...],
    *args: Any
) -> None:
    job.target(*args)

My first Idea was to use only _P.args for *args, but that isn't supported (yet).

In the discussion, Eric suggested TypeVarTuple for a basic callable example.

Ts = TypeVarTuple("Ts")

def call_soon(self, callback: Callable[[*Ts], Any], *args: *Ts) -> Handle:
    ...

--
I was wondering if that extends to generic ParamSpec as well. AFAICT the PEP doesn't mention it directly. Logically it would make sense IMO.

_Ts = TypeVarTuple("_Ts")

def call_job(
    job: Job[[*_Ts]],
    *args: *_Ts
) -> None:
    job.target(*args)


def f(x: int) -> None: ...

call_job(Job(f), 42)
call_job(Job(f), "Hello")  # this should be an error

A related discussion in pyright: microsoft/pyright#3310

@cdce8p cdce8p added the topic: other Other topics not covered label Apr 15, 2022
@cdce8p
Copy link
Contributor Author

cdce8p commented May 24, 2022

Discussions might be a better place for it. I've created a new topic with a better issue description. Maybe that will help in getting some feedback. #1194

@cdce8p cdce8p closed this as completed May 24, 2022
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

1 participant