Python3 typing: extract argument list from Callable

Consider a function that wraps another function and outputs the output to a singleton list:

def listify(func):
    return lambda *kargs, **kwargs: [func(*kargs, **kwargs)]

How would you type - call this function in python3? This is my best attempt:

from typing import Callable, TypeVar
T = TypeVar('T')

def listify(func: Callable[..., T]) -> Callable[..., List[T]]:
    return lambda *kargs, **kwargs: [func(*kargs, **kwargs)]

But I'm not glad that the returned one Callabledoes not inherit the signature of the input argument types Callable. Is there a way to do this without making assumptions about the number of arguments func?

+4
source share
1 answer

You cannot do what you want with a hint of type. There is no syntax for writing part of arguments in TypeVargeneric.

Callable , . , , ( ...). ArgumentsVar, , .

, .

+1

Source: https://habr.com/ru/post/1677928/


All Articles