Skip to content

#3142: Improve usability of multi() and multi_future() type annotations#3478

Merged
bdarnell merged 2 commits intotornadoweb:masterfrom
jrheard-seesaw:3142-gen-multi-typing
Apr 28, 2025
Merged

#3142: Improve usability of multi() and multi_future() type annotations#3478
bdarnell merged 2 commits intotornadoweb:masterfrom
jrheard-seesaw:3142-gen-multi-typing

Conversation

@jrheard-seesaw
Copy link
Contributor

This diff resolves #3142 .

Minimal repro:

from tornado import gen


async def foo() -> int:
    return 1


async def bar() -> str:
    return "hello"


awaitables = [foo(), bar()]

gen.multi(awaitables)

Before patch:

🚂  mypy repro.py
/Users/jrheard/dev/tornado/tornado/tcpclient.py:274: error: Argument 1 to "_create_stream" of "TCPClient" has incompatible type "int | None"; expected "int"  [arg-type]
/Users/jrheard/dev/tornado/tornado/curl_httpclient.py:366: error: Argument 2 to "_curl_header_callback" of "CurlAsyncHTTPClient" has incompatible type "Callable[[str], None] | None"; expected "Callable[[str], None]"  [arg-type]
repro.py:14: error: Argument 1 to "multi" has incompatible type "list[Coroutine[Any, Any, object]]"; expected "list[Awaitable[Any] | list[Awaitable[Any]] | dict[Any, Awaitable[Any]] | Future[Any] | None] | dict[Any, Awaitable[Any] | list[Awaitable[Any]] | dict[Any, Awaitable[Any]] | Future[Any] | None]"  [arg-type]
repro.py:14: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
repro.py:14: note: Consider using "Sequence" instead, which is covariant
Found 3 errors in 3 files (checked 1 source file)

After patch:

🚂  mypy repro.py
/Users/jrheard/dev/tornado/tornado/tcpclient.py:274: error: Argument 1 to "_create_stream" of "TCPClient" has incompatible type "int | None"; expected "int"  [arg-type]
/Users/jrheard/dev/tornado/tornado/curl_httpclient.py:366: error: Argument 2 to "_curl_header_callback" of "CurlAsyncHTTPClient" has incompatible type "Callable[[str], None] | None"; expected "Callable[[str], None]"  [arg-type]
Found 2 errors in 2 files (checked 1 source file)

I'm not sure what to make of those two remaining type errors - they appear to me to be pre-existing. LMK if I'm confused about that please!

CCing @bdarnell because I notice you commented on the original issue :)

Thanks for your work on this project!

@bdarnell
Copy link
Member

bdarnell commented Apr 3, 2025

I'm not sure what to make of those two remaining type errors - they appear to me to be pre-existing. LMK if I'm confused about that please!

I'd bet it's a mypy version issue. We're clean with the version of mypy in tornado's requirements.txt but we don't upgrade very frequently.

@jrheard-seesaw
Copy link
Contributor Author

That makes sense, thanks! When I pin my repro repo to use the same mypy version as tornado, I now get this output:

🚂  mypy repro.py
Success: no issues found in 1 source file

Added a commit to fix an issue where I had mistakenly imported Sequence within the TYPE_CHECKING check - my mistake!

@bdarnell
Copy link
Member

Thanks!

@bdarnell bdarnell merged commit 5639d49 into tornadoweb:master Apr 28, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tornado.gen.multi() results in mypy error

2 participants