【发布时间】:2020-09-16 00:38:48
【问题描述】:
我想为异步函数编写一个装饰器。如何注释装饰器定义的类型?
这是我想做的一个例子:
from typing import TypeVar # will Awaitable help?
AsyncFn = TypeVar('AsyncFn') # how to narrow this type definition down to async functions?
def my_decorator(to_decorate: AsyncFn) -> AsyncFn:
async def decorated(*args, **kwargs): # mypy keeps saying "Function is missing a type"
return await to_decorate(*args, **kwargs)
@my_decorator
async def foo(bar: int) -> str:
return f"async: {bar}"
@my_decorator
async def quux(spam: str, **eggs: str) -> None:
return
foo(1) # should check
foo("baz") # mypy should yell
quux("spam") # should check
quux(1) # mypy should complain
quux(spam="lovely", eggs="plentiful") # ok
quux(spam=1, eggs=0) # mypy should throw a fit
【问题讨论】:
-
我从未尝试过,但我相信
Coroutine是合适的。 -
看起来不错;当我
reveal_type(my_async_fn)我得到类似def (bar: builtins.str) -> typing.Coroutine[Any, Any, builtins.int]的东西。 -
仔细阅读mypy.readthedocs.io/en/stable/… 表明内部函数可以作为任何类型输入,只要它转换为输入函数的类型即可。
标签: python python-3.x python-asyncio mypy