Skip to content

Commit ea8cdcd

Browse files
committed
nit(twisted/consumer, is_coro): add backwards compatibility for Python 3.9-
1 parent 5053de5 commit ea8cdcd

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

fedora_messaging/twisted/consumer.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,16 @@ def is_coro(
4646
func_or_obj: config.CallbackType,
4747
) -> "TypeGuard[Callable[..., Coroutine[Any, Any, Any]]]":
4848
"""Tests if a function is a coroutine function or a callable coroutine object."""
49-
# Until Python 3.10, inspect.iscoroutinefunction() will fail to identify AsyncMocks
50-
# as coroutines: https://github.com/python/cpython/issues/84753
51-
# Use asyncio.iscoroutinefunction() instead.
49+
# Workaround for CPython issue #84753 in Python 3.9 and older
50+
# c.f.: https://github.com/python/cpython/issues/84753
51+
if sys.version_info < (3, 10):
52+
from unittest.mock import AsyncMock
53+
54+
if isinstance(func_or_obj, AsyncMock) or (
55+
callable(func_or_obj) and isinstance(getattr(func_or_obj, "__call__", None), AsyncMock)
56+
):
57+
return True
58+
5259
return inspect.iscoroutinefunction(func_or_obj) or (
5360
callable(func_or_obj) and inspect.iscoroutinefunction(func_or_obj.__call__) # type: ignore
5461
)

0 commit comments

Comments
 (0)