mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-29 13:04:02 +03:00
Add Provider.async_() method + tests
This commit is contained in:
parent
141843e62f
commit
227d3494da
File diff suppressed because it is too large
Load Diff
|
@ -277,6 +277,23 @@ cdef class Provider(object):
|
|||
self.__overridden = tuple()
|
||||
self.__last_overriding = None
|
||||
|
||||
def async_(self, *args, **kwargs):
|
||||
"""Return provided object asynchronously.
|
||||
|
||||
This method is a synonym of __call__().
|
||||
It provides typing stubs for correct type checking with
|
||||
`await` expression:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
database_provider: Provider[DatabaseConnection] = Resource(init_db_async)
|
||||
|
||||
async def main():
|
||||
db: DatabaseConnection = await database_provider.async_()
|
||||
...
|
||||
"""
|
||||
return self.__call__(*args, **kwargs)
|
||||
|
||||
def delegate(self):
|
||||
"""Return provider's delegate.
|
||||
|
||||
|
|
|
@ -707,3 +707,35 @@ class TestAsyncModeApi(unittest.TestCase):
|
|||
self.assertFalse(self.provider.is_async_mode_enabled())
|
||||
self.assertFalse(self.provider.is_async_mode_disabled())
|
||||
self.assertTrue(self.provider.is_async_mode_undefined())
|
||||
|
||||
|
||||
class AsyncTypingStubTests(AsyncTestCase):
|
||||
|
||||
def test_async_(self):
|
||||
container = Container()
|
||||
|
||||
client1 = self._run(container.client.async_())
|
||||
client2 = self._run(container.client.async_())
|
||||
|
||||
self.assertIsInstance(client1, Client)
|
||||
self.assertIs(client1.resource1, RESOURCE1)
|
||||
self.assertIs(client1.resource2, RESOURCE2)
|
||||
|
||||
self.assertIsInstance(client2, Client)
|
||||
self.assertIs(client2.resource1, RESOURCE1)
|
||||
self.assertIs(client2.resource2, RESOURCE2)
|
||||
|
||||
service1 = self._run(container.service.async_())
|
||||
service2 = self._run(container.service.async_())
|
||||
|
||||
self.assertIsInstance(service1, Service)
|
||||
self.assertIsInstance(service1.client, Client)
|
||||
self.assertIs(service1.client.resource1, RESOURCE1)
|
||||
self.assertIs(service1.client.resource2, RESOURCE2)
|
||||
|
||||
self.assertIsInstance(service2, Service)
|
||||
self.assertIsInstance(service2.client, Client)
|
||||
self.assertIs(service2.client.resource1, RESOURCE1)
|
||||
self.assertIs(service2.client.resource2, RESOURCE2)
|
||||
|
||||
self.assertIsNot(service1.client, service2.client)
|
||||
|
|
Loading…
Reference in New Issue
Block a user