从另一个函数复制类型签名

Copy type signature from another function

假设我有一组如下所示的函数。 foo 有很多不同类型的参数,bar 将其所有参数传递给另一个函数。有没有办法让 mypy 理解 barfoo 具有相同的类型,而无需显式复制整个参数列表?

def foo(a: int, b: float, c: str, d: bool, *e: str, f: str = "a", g: str = "b") -> str:
    ...

def bar(*args, **kwargs):
    val = foo(*args, **kwargs)
    ...
    return val

关于添加此功能的讨论很多here. For the straightforward case of passing all arguments you can use the recipe from this comment

F = TypeVar('F', bound=Callable[..., Any])

class copy_signature(Generic[F]):
    def __init__(self, target: F) -> None: ...
    def __call__(self, wrapped: Callable[..., Any]) -> F: ...

def f(x: bool, *extra: int) -> str: ...

@copy_signature(f)
def test(*args, **kwargs):
    return f(*args, **kwargs)

reveal_type(test)  # Revealed type is 'def (x: bool, *extra: int) -> str'