我如何注释Python函数以暗示它接受与另一个函数相同的参数?

问题描述 投票:3回答:1

是否有任何Python类型提示语法说明一个函数采用与另一个函数相同的参数(和参数类型?特别是在包装时很有用,例如

async def do_stuff(
        param1: str,
        param2: int,
        param3: int,
):
    ...

def run_async_thing(*args, **kwargs):  # <--- What can I put here to say 'takes args like `do_stuff`'?
    return asyncio.get_event_loop().run_until_complete(do_stuff(*args, **kwargs))

在这种情况下,我想向run_async_thing函数添加类型提示以标识它期望与do_stuff函数具有相同的参数类型。

这是否可能,如果可能,如何?

python python-3.x type-hinting python-typing
1个回答
0
投票

明确定义参数。您不必要泛化run_asyn_thing的签名:

def run_async_thing(p1: str, p2: int, p3: int):
    return asyncio.get_event_loop().run_until_complete(do_stuff(p1, p2, p3))
© www.soinside.com 2019 - 2024. All rights reserved.