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

bpo-38415 @asynccontextmanager as decorators like @contextmanager #16667

Merged
merged 1 commit into from Sep 23, 2021

Conversation

@fried
Copy link
Contributor

@fried fried commented Oct 8, 2019

I assumed at that an asynccontextmanager would some time be available in the stdlib when we first got async generators. Before it had been released I cooked up an internal version at facebook whiched mapped all the features of @contextmanager but as an async variant. This one feature seems to be missing from the stdlib.

I would like to stop using my version and just use the stdlib version, can we get this feature parity with contextmanager?

@asynccontextmanager
async def our_context(...):
     ...
     yield
     ...

@our_context(...)
async def some_function(...):
   # we are inside the context of our_context now
    ...

@our_context(...)
async def some_other_function(...):
   ...

https://bugs.python.org/issue38415

@1st1
Copy link
Member

@1st1 1st1 commented Oct 8, 2019

Awesome! Please create a bpo issue and generate a NEWS file using the blurb tool.

1st1
1st1 approved these changes Oct 8, 2019
@fried fried changed the title bpo: 38415 @asynccontextmanager as decorators like @contextmanager bpo:38415 @asynccontextmanager as decorators like @contextmanager Oct 8, 2019
@fried fried force-pushed the asynccontextmanager_decorator branch from b6fad49 to cd79d6c Oct 8, 2019
@fried fried changed the title bpo:38415 @asynccontextmanager as decorators like @contextmanager bpo-38415 @asynccontextmanager as decorators like @contextmanager Oct 8, 2019
@fried
Copy link
Contributor Author

@fried fried commented Oct 8, 2019

alright

@1st1
Copy link
Member

@1st1 1st1 commented Oct 8, 2019

@ncoghlan Nick, do you want to take a look at this? Looks good to me.

@ambv ambv force-pushed the asynccontextmanager_decorator branch from cd79d6c to 8890d0e Sep 23, 2021
@ambv
Copy link
Contributor

@ambv ambv commented Sep 23, 2021

Rebased on current main (3.11).

@ambv ambv merged commit 86b833b into python:main Sep 23, 2021
12 checks passed
def __call__(self, func):
@wraps(func)
async def inner(*args, **kwds):
async with self.__class__(self.func, self.args, self.kwds):
return await func(*args, **kwds)

return inner

Copy link
Contributor

@graingert graingert Dec 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function is now redundant - support for asynccontextmanagers as decorators was added in #20516

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants