]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
concurrent: Update type hint on chain_future to match implementation 3393/head
authorBen Darnell <ben@bendarnell.com>
Fri, 7 Jun 2024 19:54:08 +0000 (15:54 -0400)
committerBen Darnell <ben@bendarnell.com>
Fri, 7 Jun 2024 19:54:08 +0000 (15:54 -0400)
This method has always accepted both asyncio and concurrent futures,
but the type hint incorrectly indicated that it only accepted asyncio
futures.

Fixes #3314

tornado/concurrent.py
tornado/test/concurrent_test.py

index 5047c5389fd6aa16dac74c66eba625825de4f7d9..e98093f2158ceef58f1760e8ddfdbd3e7e179429 100644 (file)
@@ -145,7 +145,10 @@ def run_on_executor(*args: Any, **kwargs: Any) -> Callable:
 _NO_RESULT = object()
 
 
-def chain_future(a: "Future[_T]", b: "Future[_T]") -> None:
+def chain_future(
+    a: Union["Future[_T]", "futures.Future[_T]"],
+    b: Union["Future[_T]", "futures.Future[_T]"],
+) -> None:
     """Chain two futures together so that when one completes, so does the other.
 
     The result (success or failure) of ``a`` will be copied to ``b``, unless
index 33fcb6505e6311ffb44cb1e3346446cfe4b7f04c..009d6ed43f09a0d07545b0a778e2a23167136a92 100644 (file)
@@ -21,6 +21,7 @@ import unittest
 
 from tornado.concurrent import (
     Future,
+    chain_future,
     run_on_executor,
     future_set_result_unless_cancelled,
 )
@@ -47,6 +48,31 @@ class MiscFutureTest(AsyncTestCase):
             self.assertEqual(fut.result(), 42)
 
 
+class ChainFutureTest(AsyncTestCase):
+    @gen_test
+    async def test_asyncio_futures(self):
+        fut: Future[int] = Future()
+        fut2: Future[int] = Future()
+        chain_future(fut, fut2)
+        fut.set_result(42)
+        result = await fut2
+        self.assertEqual(result, 42)
+
+    @gen_test
+    async def test_concurrent_futures(self):
+        # A three-step chain: two concurrent futures (showing that both arguments to chain_future
+        # can be concurrent futures), and then one from a concurrent future to an asyncio future so
+        # we can use it in await.
+        fut: futures.Future[int] = futures.Future()
+        fut2: futures.Future[int] = futures.Future()
+        fut3: Future[int] = Future()
+        chain_future(fut, fut2)
+        chain_future(fut2, fut3)
+        fut.set_result(42)
+        result = await fut3
+        self.assertEqual(result, 42)
+
+
 # The following series of classes demonstrate and test various styles
 # of use, with and without generators and futures.