Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Async gen function scoped provider returnes async gen object instead of a yielded value #21

Open
sergey0xff opened this issue Sep 10, 2019 · 9 comments
Labels
bug Something isn't working

Comments

@sergey0xff
Copy link

sergey0xff commented Sep 10, 2019

Expected behavior
When a value is yielded from async generator function scoped provider it's expected to be the value that will be injected to consumers

Actual behavior
Async gen object injected instead

To Reproduce

import asyncio

import aiodine

store = aiodine.Store()

DB_CONNECTION = 'db connection'


@store.provider(name='db', scope='function')
async def setup_db():
    print('acquiring connection')
    yield DB_CONNECTION
    print('releasing connection')


@store.consumer
async def worker(db):
     # expected a DB_CONNECTION, got async gen object instead
    assert db == DB_CONNECTION, f'expected {DB_CONNECTION!r}, got {db!r} instead'


async def main():
    store.freeze()

    async with store.session():
        await worker()


if __name__ == '__main__':
    asyncio.run(main())

Desktop (please complete the following information):

  • OS: Ubuntu 19.04
  • Python version: 3.7.4
  • aiodine version: 1.2.7
@sergey0xff sergey0xff added the bug Something isn't working label Sep 10, 2019
@florimondmanca
Copy link
Member

Thanks for reporting @Sergey-Tikhonov! I’ll try to reproduce soon.

As a workaround, I believe you can do:

db = await db.__anext__()

@florimondmanca
Copy link
Member

florimondmanca commented Sep 11, 2019

@Sergey-Tikhonov I was able to reproduce the issue, and pinned it to an issue in store.freeze(). Investigating now. :) It's been a while I've worked on aiodine and may need to refactor things out a bit for simplicity first.

@florimondmanca
Copy link
Member

@Sergey-Tikhonov This is still a bug so I’ll reopen so that other users can see it and the proposed workaround. :)

@sergey0xff
Copy link
Author

Due to recent changes in API design the task is no more relevant to me. Thanks for your response!

@florimondmanca
Copy link
Member

Sure thing!

By the way, out of curiosity — are you using aiodine in a Bocadillo project or as a standalone library?

@sergey0xff
Copy link
Author

I was going to use aiodine in my pet projects first and then consider using it in my production code. But with the new API it is not what I need.

@florimondmanca
Copy link
Member

Ah, so I understand you won't be using aiodine because of the new API? Is there any particular API difference or missing feature that has made the new API not fit for your purpose? Keep in mind, nothing has been released yet — if there's some critical bit of functionality that was in the previous API but is not there anymore, we can still arrange things. Thanks!

@sergey0xff
Copy link
Author

Well I need a simple dependency injection library that supports all asyncio features like coroutines, async generators and context managers. The killer feature is dependencies provided by names. The reason why I need name-based dependencies is multiple implementations/configurations of the same dependency. E.g. for production and testing I used different configurations and even implementations for db pools and some other resources like connections to other services.

@florimondmanca
Copy link
Member

Thanks for pitching in about this. Actually I can see how the proposed new API does not allow overriding providers as easily — you’d have to add conditions inside the provider or dynamically define it depending on the conditions of interest. Definitely worth thinking about that. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants