Skip to content

Commit

Permalink
Raise AsyncDependencyForbiddenError on async function provided as …
Browse files Browse the repository at this point in the history
…a dependency (#11)

* Raise `AsyncDependencyProvided` on async function provided as dependency.

* `AsyncDependencyForbiddenError` on injector.register if dep is async

---------

Co-authored-by: isaev-vs <[email protected]>
  • Loading branch information
Vasiliy566 and isaev-vs authored Jul 31, 2023
1 parent 80e38a9 commit 8e56507
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions giveme/injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class DependencyNotFoundError(Exception):
pass


class AsyncDependencyForbiddenError(Exception):
pass


class DependencyNotFoundWarning(RuntimeWarning):
pass

Expand Down Expand Up @@ -83,6 +87,8 @@ def _set(self, name, factory, singleton=False, threadlocal=False):
Same functionality as ``singleton`` except :class:`Threading.local` is used
to cache return values.
"""
if iscoroutinefunction(factory):
raise AsyncDependencyForbiddenError(name)
name = name or factory.__name__
factory._giveme_registered_name = name
dep = Dependency(name, factory, singleton, threadlocal)
Expand Down
11 changes: 11 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from multiprocessing.pool import ThreadPool

from giveme import register, inject, DependencyNotFoundError
from giveme.injector import AsyncDependencyForbiddenError


def test_inject():
Expand Down Expand Up @@ -236,6 +237,10 @@ def simple_dep():
return 42


async def async_simple_dep():
return 566


def double_dep(simple_dep):
return simple_dep*2

Expand Down Expand Up @@ -451,3 +456,9 @@ def f(list_dep):

with pytest.raises(TypeError):
f()


@pytest.mark.asyncio
async def test_inject_async_dep(gm):
with pytest.raises(AsyncDependencyForbiddenError):
gm.register(async_simple_dep)

0 comments on commit 8e56507

Please sign in to comment.