Comments (4)
Put @asynccontextmanager
on the class's __call__
method, not on the class itself. That will produce a class whose instances yield async generators when they are called. If that doesn't meet your needs, can you clarify where it falls short?
@asynccontextmanager
is intended to be used as a function decorator. That is, it accepts as argument an async generator function (i.e., a function that returns an async generator iterator) and returns a function that returns an async context manager. So even if it did support being applied to a class instance, you would use async with cm() as value
, not with cm as value
. But @asynccontextmanager
does check specifically for an async generator function, not just a thing-that-when-called-returns-an-async-generator, so this won't work. You can fool it (look at the definition of isasyncgenfunction
in _impl.py
to see what you need to spoof) but decorating the __call__
method will likely work much better.
from async_generator.
I just looked at the linked issue. If you are given the class definition and can't modify it, you could do something like cm = asynccontextmanager(MyClass.__call__); async with cm(MyClass()) as value: ...
from async_generator.
Sure, you're recommendation of a workaround makes sense and that's more or less what I did to unblock the python3.6 failure on the MR that I opened for the linked project issue, but I think the functionality that I was trying to call out is slightly different.
First, the example was a typo, you are correct that it should be async with
, but the point that I was trying to show is that the TypeErrror
was thrown on the line before it. I was filing this as a bug because mostly because it differs from the behavior of the native python3.7+ function in contextlib
# python 3.6
from async_generator import asynccontextmanager
# this line throws a TypeError
cm = asynccontextmanager(GenClass())
# python 3.7
from contextlib import asynccontextmanager
# this works just fine
cm = asynccontextmanager(GenClass())
from async_generator.
@mrosales fyi this works on the contextlib2 backport:
from contextlib2 import asynccontextmanager
class GenClass:
async def __call__(self):
yield "hello"
cm = asynccontextmanager(GenClass())
async def amain():
async with cm() as value:
print(value)
import anyio
anyio.run(amain)
from async_generator.
Related Issues (14)
- Questions HOT 3
- Unexpected RuntimeError: partially-exhausted async_generator garbage collected HOT 4
- Issues from Nicolas Boulenguez HOT 3
- New Release HOT 3
- missing git tag 1.10 HOT 2
- open_cancel_scope → CancelScope HOT 2
- Abstract async context manager in Python3.6
- `asynccontextmanager` fails in `3.7-dev` HOT 1
- asynccontextmanager decorated functions return False but when using contextlib they return None HOT 3
- 1.10: pytest is failing HOT 2
- Deprecate async_generator HOT 4
- ctypes.CDLL(None) doesn't work on Windows HOT 5
- appveyor coverage isn't working HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async_generator.