Merge branch 'release/4.10.2' into master

This commit is contained in:
Roman Mogylatov 2021-01-19 17:51:00 -05:00
commit 0cc63cd075
5 changed files with 2264 additions and 1418 deletions

View File

@ -7,6 +7,13 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_
4.10.2
------
- Fix a bug in ``Resource`` that cause failure when async resource depends on
another async resource.
See issue `#361 <https://github.com/ets-labs/python-dependency-injector/issues/361>`_.
Thanks `@kolypto <https://github.com/kolypto>`_ for the bug report.
4.10.1
------
- Fix a Python 3.9 specific bug in ``wiring`` module: introspection doesn't work for

View File

@ -1,6 +1,6 @@
"""Top-level package."""
__version__ = '4.10.1'
__version__ = '4.10.2'
"""Version number.
:type: str

File diff suppressed because it is too large Load Diff

View File

@ -2967,7 +2967,7 @@ cdef class Resource(Provider):
self.__kwargs_len,
)
self.__initialized = True
return self._create_init_future(initializer.__anext__(), initializer.asend)
return self._create_async_gen_init_future(initializer)
elif callable(self.__initializer):
self.__resource = __call(
self.__initializer,
@ -2995,6 +2995,18 @@ cdef class Resource(Provider):
return future
def _create_async_gen_init_future(self, initializer):
if inspect.isasyncgen(initializer):
return self._create_init_future(initializer.__anext__(), initializer.asend)
future = asyncio.Future()
create_initializer = asyncio.ensure_future(initializer)
create_initializer.add_done_callback(functools.partial(self._async_create_gen_callback, future))
self.__resource = future
return future
def _async_init_callback(self, initializer, shutdowner=None):
try:
resource = initializer.result()
@ -3005,6 +3017,14 @@ cdef class Resource(Provider):
self.__resource = resource
self.__shutdowner = shutdowner
def _async_create_gen_callback(self, future, initializer_future):
initializer = initializer_future.result()
init_future = self._create_init_future(initializer.__anext__(), initializer.asend)
init_future.add_done_callback(functools.partial(self._async_trigger_result, future))
def _async_trigger_result(self, future, future_result):
future.set_result(future_result.result())
def _create_shutdown_future(self, shutdown_future):
future = asyncio.Future()
shutdown_future = asyncio.ensure_future(shutdown_future)

View File

@ -461,6 +461,43 @@ class AsyncResourceTest(AsyncTestCase):
self.assertFalse(provider.initialized)
self.assertTrue(provider.is_async_mode_enabled())
def test_init_with_dependency_to_other_resource(self):
# See: https://github.com/ets-labs/python-dependency-injector/issues/361
async def init_db_connection(db_url: str):
await asyncio.sleep(0.001)
yield {'connection': 'ok', 'url': db_url}
async def init_user_session(db):
await asyncio.sleep(0.001)
yield {'session': 'ok', 'db': db}
class Container(containers.DeclarativeContainer):
config = providers.Configuration()
db_connection = providers.Resource(
init_db_connection,
db_url=config.db_url,
)
user_session = providers.Resource(
init_user_session,
db=db_connection
)
async def main():
container = Container(config={'db_url': 'postgres://...'})
try:
return await container.user_session()
finally:
await container.shutdown_resources()
result = self._run(main())
self.assertEqual(
result,
{'session': 'ok', 'db': {'connection': 'ok', 'url': 'postgres://...'}},
)
def test_init_and_shutdown_methods(self):
async def _init():
await asyncio.sleep(0.001)