vllm Proposal: force type hint check with mypy

tf7tbtn2  于 2个月前  发布在  其他
关注(0)|答案(3)|浏览(70)

为什么

在学习源代码的过程中,我发现理解asyncio很困难。以这个代码库为例,我花了相当长的时间研究以下方法签名:
vllm/vllm/engine/async_llm_engine.py
7c60044 的第394行到第399行
| | asyncdefgenerate( |
| | self, |
| | prompt: Optional[str], |
| | sampling_params: SamplingParams, |
| | request_id: str, |
| | prompt_token_ids: Optional[List[int]] =None) ->RequestOutput: |
我意识到返回类型提示之间存在差异。原始提示是:

-> RequestOutput:

然而,正确的提示应该是:

-> typing.AsyncIterator[RequestOutput]:

为了详细演示这个问题,请参阅下面的附录。

建议

为了增强mypy的类型检查,建议按照 this StackOverflow answer 的建议,在mypy.ini中包含以下选项:

disallow_untyped_defs
disallow_incomplete_defs
disallow_untyped_calls

附录

下面是一个完整的例子,说明如何正确使用异步迭代器的类型提示:

"""
For a full check including type hints, run the following command

mypy \
--disallow-untyped-defs \
--disallow-incomplete-defs \
--disallow-untyped-calls \
/tmp/a.py
"""
import asyncio
import typing

async def foo() -> typing.AsyncIterable[int]:
    for i in range(3):
        yield (i)

async def bar() -> typing.AsyncIterable[int]:
    f = foo()
    print(f"The type of foo is {type(foo)}")  # <class 'function'>
    print(f"The return type of foo is {type(f)}")  # <class 'async_generator'>
    async for i in f:
        yield i

async def main() -> None:
    b = bar()
    print(f"The type of bar is {type(bar)}")  # <class 'function'>
    print(f"The return type of bar is {type(b)}")  # <class 'async_generator'>
    async for i in b:
        print(i)

c = main()
print(f"The return type of main is {type(c)}")  #  <class 'coroutine'>
asyncio.run(c)
kyxcudwk

kyxcudwk1#

我们当然非常希望能够完全启用类型检查,但是我们理解这相当复杂且耗时,所以我们专注于更紧迫的问题。如果你能帮助做出贡献,那将是非常棒的!

pw9qyyiw

pw9qyyiw2#

@rkooo567,一旦你完成了与mypy的跨目录类型检查的PR,你可以关闭这个。

v2g6jxz6

v2g6jxz63#

是的,我需要创建1个PR来完成这个。

相关问题