Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync context managers are not entered in aget #87

Closed
adrianschneider94 opened this issue May 23, 2024 · 0 comments · Fixed by #93
Closed

Sync context managers are not entered in aget #87

adrianschneider94 opened this issue May 23, 2024 · 0 comments · Fixed by #93
Labels
bug Something isn't working

Comments

@adrianschneider94
Copy link
Contributor

To reproduce:

import asyncio

import svcs
from svcs import Registry

registry = Registry()


def sync_generator() -> int:
    yield 3


registry.register_factory(int, sync_generator)


async def main():
    async with svcs.Container(registry=registry) as container:
        print(await container.aget(int))
        # Prints <contextlib._GeneratorContextManager object at 0x0000024E4FACACF0>
        # instead of 3
        


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

Adding

class Container:
    ...

    async def aget(self, *svc_types: type) -> object:
        """
        Same as :meth:`get` but instantiates asynchronously, if necessary.

        Also works with synchronous services, so in an async application, just
        use this.
        """
        rv = []
        for svc_type in svc_types:
            cached, svc, name, enter = self._lookup(svc_type)
            if cached:
                rv.append(svc)
                continue

            if enter and isinstance(svc, AbstractAsyncContextManager):
                self._on_close.append((name, svc))
                svc = await svc.__aenter__()
                
            elif isawaitable(svc):
                svc = await svc
            
           # New =>
            elif enter and isinstance(svc, AbstractContextManager):
                self._on_close.append((name, svc))
                svc = svc.__enter__()
           # <= New

            self._instantiated[svc_type] = svc

            rv.append(svc)

        if len(rv) == 1:
            return rv[0]

        return rv

works.

@adrianschneider94 adrianschneider94 added the bug Something isn't working label May 23, 2024
hynek added a commit that referenced this issue Jul 2, 2024
Fixes #87

Co-authored-by: Adrian Schneider
<[email protected]>
hynek added a commit that referenced this issue Jul 2, 2024
Fixes #87

Co-authored-by: Adrian Schneider <[email protected]>
hynek added a commit that referenced this issue Jul 3, 2024
Fixes #87

Co-authored-by: Adrian Schneider <[email protected]>
hynek added a commit that referenced this issue Jul 3, 2024
Fixes #87

Co-authored-by: Adrian Schneider <[email protected]>
hynek added a commit that referenced this issue Jul 3, 2024
Fixes #87

Co-authored-by: Adrian Schneider <[email protected]>
@hynek hynek closed this as completed in #93 Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant