-
Notifications
You must be signed in to change notification settings - Fork 263
Closed
Labels
topic: otherOther topics not coveredOther topics not covered
Description
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
povilasb
Metadata
Metadata
Assignees
Labels
topic: otherOther topics not coveredOther topics not covered
Projects
Milestone
Relationships
Development
Select code repository
Activity
cdce8p commentedon 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