Is there any way for me to apply a transformation on a ParamSpec? I can illustrate the problem with an example:
from typing import Callable
def as_upper(x: str):
return x.upper()
def eventually(f: Callable[P, None], *args: P.args, **kwargs: P.kwargs):
def inner():
def transform(a):
return a() if isinstance(a, Callable) else a
targs = tuple(transform(a) for a in args)
tkwargs = {k: transform(v) for k,v in kwargs.items()}
return f(*targs, **tkwargs)
return inner
eventually(as_upper, lambda: "hello") # type checker complains here
This type checker (pyright in my case) will complain about this. The function eventually received a callable () -> str and not a str which was expected. My question is: is there some way for me to specify that it should expect () -> str and not the str itself? And in general, if a function expects a type T I can transform it to (say) () -> T?
I'm basically asking if it's possible to transform the ParamSpec in some way so that a related function does not expect the same parameters, but "almost" the same parameters.
I don't really expect this to be possible, but maybe some with more experience with type checking know a potential solution to this problem. :)