小编Wil*_*ner的帖子

为什么在mypy中使用await表达式会得到无效的语法?

# file test.py
import asyncio
from inspect import iscoroutine
from typing import Any


async def a():
    print('run a')
    asyncio.sleep(1)
    return 1


async def b():
    # type: () -> Any
    print('run b')
    return a()


async def g():
    # type: () -> Any
    print('run g')
    s = b()
    while iscoroutine(s):
        print('in g', s)
        s = await s
    return s


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    a = loop.run_until_complete(g())
    print('finish ', a)
Run Code Online (Sandbox Code Playgroud)

当使用python3.6.6运行以上代码时,我得到:

run g
in g <coroutine object b at 0x10a980938> …
Run Code Online (Sandbox Code Playgroud)

python python-asyncio mypy

5
推荐指数
1
解决办法
485
查看次数

标签 统计

mypy ×1

python ×1

python-asyncio ×1