Add Provider.async_() method + tests

This commit is contained in:
Roman Mogylatov 2021-01-02 22:47:59 -05:00
parent 141843e62f
commit 227d3494da
3 changed files with 4847 additions and 4714 deletions

File diff suppressed because it is too large Load Diff

View File

@ -277,6 +277,23 @@ cdef class Provider(object):
self.__overridden = tuple() self.__overridden = tuple()
self.__last_overriding = None 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): def delegate(self):
"""Return provider's delegate. """Return provider's delegate.

View File

@ -707,3 +707,35 @@ class TestAsyncModeApi(unittest.TestCase):
self.assertFalse(self.provider.is_async_mode_enabled()) self.assertFalse(self.provider.is_async_mode_enabled())
self.assertFalse(self.provider.is_async_mode_disabled()) self.assertFalse(self.provider.is_async_mode_disabled())
self.assertTrue(self.provider.is_async_mode_undefined()) 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)