]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Allow non-yielding functions in `tornado.gen.coroutine`'s type hint (#2909)
authorjack1142 <6032823+jack1142@users.noreply.github.com>
Sun, 13 Sep 2020 15:01:31 +0000 (17:01 +0200)
committerGitHub <noreply@github.com>
Sun, 13 Sep 2020 15:01:31 +0000 (11:01 -0400)
`@gen.coroutine` deco allows non-yielding functions, so I reflected that in the type hint.

Requires usage of `@typing.overload` due to python/mypy#9435

tornado/gen.py

index c58884cc934de631572159d12e1b46d84c1bf578..4e0cab22de0eac45b2c82737bd8dab46837a09d1 100644 (file)
@@ -91,7 +91,7 @@ from tornado.log import app_log
 from tornado.util import TimeoutError
 
 import typing
-from typing import Union, Any, Callable, List, Type, Tuple, Awaitable, Dict
+from typing import Union, Any, Callable, List, Type, Tuple, Awaitable, Dict, overload
 
 if typing.TYPE_CHECKING:
     from typing import Sequence, Deque, Optional, Set, Iterable  # noqa: F401
@@ -153,8 +153,20 @@ def _create_future() -> Future:
     return future
 
 
+@overload
 def coroutine(
     func: Callable[..., "Generator[Any, Any, _T]"]
+) -> Callable[..., "Future[_T]"]:
+    ...
+
+
+@overload
+def coroutine(func: Callable[..., _T]) -> Callable[..., "Future[_T]"]:
+    ...
+
+
+def coroutine(
+    func: Union[Callable[..., "Generator[Any, Any, _T]"], Callable[..., _T]]
 ) -> Callable[..., "Future[_T]"]:
     """Decorator for asynchronous generators.