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

Type checking problems. "Awaitable" vs "Future" #3093

Closed
dettonijr opened this issue Dec 15, 2021 · 2 comments
Closed

Type checking problems. "Awaitable" vs "Future" #3093

dettonijr opened this issue Dec 15, 2021 · 2 comments

Comments

@dettonijr
Copy link

import threading
import tornado.ioloop
import time
from tornado.concurrent import Future

ioloop = tornado.ioloop.IOLoop.current()

def callback(future: Future[int]) -> None:
    print(f"Callback {future.result()} {threading.current_thread().name}")
    ioloop.stop()

def function() -> int:
    print(f"Function {threading.current_thread().name}")
    time.sleep(1)
    return 1

future = ioloop.run_in_executor(None, function)
ioloop.add_future(future, callback)
ioloop.start()

It does what's supposed to, runs a callback in the main thread after the the function runs in the executor. And everything is typed. However, using mypy:

$ mypy script.py 
script.py:17: error: Argument 1 to "add_future" of "IOLoop" has incompatible type "Awaitable[int]"; expected "Union[asyncio.futures.Future[int], concurrent.futures._base.Future[int]]"

I see the type annotations were all added for both functions on the same commit afe7dc5

Is there a reason run_in_executor returns Awaitable[_T] and not Future[_T]?

@bdarnell
Copy link
Member

run_in_executor returns Awaitable because that's what the corresponding asyncio method is typed to return. If I'm following the various threads correctly (starting with python/typeshed#3999), now that https://bugs.python.org/issue40782 has been fixed in cpython, we need a typeshed PR to change the return type of AbstractEventLoop.run_in_executor from Awaitable to Future, and then we can make the same change in Tornado.

@bdarnell
Copy link
Member

Typeshed now has this method returning a Future, so this is a change we could make next time we upgrade mypy/typeshed.

bdarnell added a commit to bdarnell/tornado that referenced this issue Jul 27, 2023
This required a recent update to typeshed/mypy.

Fixes tornadoweb#3093
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

2 participants