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

Add support for inspect.iscoroutinefunction() for Coroutine provider #830

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/dependency_injector/providers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,22 @@ except ImportError:
# Python 2.7
import __builtin__ as builtins

try:
from inspect import _is_coroutine_marker
except ImportError:
_is_coroutine_marker = True

try:
import asyncio
except ImportError:
asyncio = None
_is_coroutine_marker = None
_is_coroutine = None
else:
if sys.version_info >= (3, 5, 3):
import asyncio.coroutines
_is_coroutine_marker = asyncio.coroutines._is_coroutine
_is_coroutine = asyncio.coroutines._is_coroutine
else:
_is_coroutine_marker = True
_is_coroutine = True

try:
import ConfigParser as iniconfigparser
Expand Down Expand Up @@ -1434,7 +1439,8 @@ cdef class Coroutine(Callable):
some_coroutine.add_kwargs(keyword_argument1=3, keyword_argument=4)
"""

_is_coroutine = _is_coroutine_marker
_is_coroutine_marker = _is_coroutine_marker # Python >=3.12
_is_coroutine = _is_coroutine # Python <3.16

def set_provides(self, provides):
"""Set provider provides."""
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/providers/coroutines/test_coroutine_py35.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Coroutine provider tests."""
import sys

from dependency_injector import providers, errors
from pytest import mark, raises
Expand Down Expand Up @@ -208,3 +209,17 @@ def test_repr():
"<dependency_injector.providers."
"Coroutine({0}) at {1}>".format(repr(example), hex(id(provider)))
)


@mark.skipif(sys.version_info > (3, 15), reason="requires Python<3.16")
def test_asyncio_iscoroutinefunction() -> None:
from asyncio.coroutines import iscoroutinefunction

assert iscoroutinefunction(providers.Coroutine(example))


@mark.skipif(sys.version_info < (3, 12), reason="requires Python>=3.12")
def test_inspect_iscoroutinefunction() -> None:
from inspect import iscoroutinefunction

assert iscoroutinefunction(providers.Coroutine(example))
Loading