Skip to content

Commit 23acf01

Browse files
authored
Add support for inspect.iscoroutinefunction() in Coroutine provider (#830)
1 parent 0d6fdb5 commit 23acf01

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/dependency_injector/providers.pyx

+13-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import absolute_import
44

5+
import asyncio
56
import copy
67
import errno
78
import functools
@@ -27,17 +28,19 @@ except ImportError:
2728
import __builtin__ as builtins
2829

2930
try:
30-
import asyncio
31+
from inspect import _is_coroutine_mark as _is_coroutine_marker
3132
except ImportError:
32-
asyncio = None
33-
_is_coroutine_marker = None
34-
else:
35-
if sys.version_info >= (3, 5, 3):
36-
import asyncio.coroutines
37-
_is_coroutine_marker = asyncio.coroutines._is_coroutine
38-
else:
33+
try:
34+
# Python >=3.12.0,<3.12.5
35+
from inspect import _is_coroutine_marker
36+
except ImportError:
3937
_is_coroutine_marker = True
4038

39+
try:
40+
from asyncio.coroutines import _is_coroutine
41+
except ImportError:
42+
_is_coroutine = True
43+
4144
try:
4245
import ConfigParser as iniconfigparser
4346
except ImportError:
@@ -1475,7 +1478,8 @@ cdef class Coroutine(Callable):
14751478
some_coroutine.add_kwargs(keyword_argument1=3, keyword_argument=4)
14761479
"""
14771480

1478-
_is_coroutine = _is_coroutine_marker
1481+
_is_coroutine_marker = _is_coroutine_marker # Python >=3.12
1482+
_is_coroutine = _is_coroutine # Python <3.16
14791483

14801484
def set_provides(self, provides):
14811485
"""Set provider provides."""

tests/unit/providers/coroutines/test_coroutine_py35.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Coroutine provider tests."""
2+
import sys
23

34
from dependency_injector import providers, errors
45
from pytest import mark, raises
@@ -208,3 +209,17 @@ def test_repr():
208209
"<dependency_injector.providers."
209210
"Coroutine({0}) at {1}>".format(repr(example), hex(id(provider)))
210211
)
212+
213+
214+
@mark.skipif(sys.version_info > (3, 15), reason="requires Python<3.16")
215+
def test_asyncio_iscoroutinefunction() -> None:
216+
from asyncio.coroutines import iscoroutinefunction
217+
218+
assert iscoroutinefunction(providers.Coroutine(example))
219+
220+
221+
@mark.skipif(sys.version_info < (3, 12), reason="requires Python>=3.12")
222+
def test_inspect_iscoroutinefunction() -> None:
223+
from inspect import iscoroutinefunction
224+
225+
assert iscoroutinefunction(providers.Coroutine(example))

0 commit comments

Comments
 (0)