Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: force type hint check with mypy #1780

Closed
wangkuiyi opened this issue Nov 25, 2023 · 5 comments
Closed

Proposal: force type hint check with mypy #1780

wangkuiyi opened this issue Nov 25, 2023 · 5 comments

Comments

@wangkuiyi
Copy link

wangkuiyi commented Nov 25, 2023

Why

While I am learning the source code, I found it challenging to understand asyncio. Taking this codebase as an example, I spend quite some time with the following method signature:

async def generate(
self,
prompt: Optional[str],
sampling_params: SamplingParams,
request_id: str,
prompt_token_ids: Optional[List[int]] = None) -> RequestOutput:

I realized there was a discrepancy in the return type hint. The original hint was:

-> RequestOutput:

However, the correct hint should be:

-> typing.AsyncIterator[RequestOutput]:

For a detailed demonstration of this issue, please see the appendix below.

Proposal

To enhance type checking with mypy, it's advisable to include the following options in mypy.ini, as suggested in this StackOverflow answer:

disallow_untyped_defs
disallow_incomplete_defs
disallow_untyped_calls

Appendix

Below is a complete example that illustrates the correct use of type hints for async iterators:

"""
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)
@simon-mo
Copy link
Collaborator

we would definitely love to fully enable type checking, however, we understand it is fairly involved and time consuming so we are focusing on more pressing issue. if you can help contribute that would be amazing!

@DarkLight1337
Copy link
Member

@rkooo567 you may close this once you've completed the PR for cross-directory type checking with mypy.

@rkooo567
Copy link
Collaborator

rkooo567 commented May 9, 2024

Yeah I will need to create 1 more PR to finish this

Copy link

This issue has been automatically marked as stale because it has not had any activity within 90 days. It will be automatically closed if no further activity occurs within 30 days. Leave a comment if you feel this issue should remain open. Thank you!

@github-actions github-actions bot added the stale label Oct 30, 2024
@DarkLight1337
Copy link
Member

Closing this as mypy has been enabled to some extent - please refer to #3680 for further developments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants