Giter VIP home page Giter VIP logo

Comments (4)

oremanj avatar oremanj commented on June 13, 2024

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.

oremanj avatar oremanj commented on June 13, 2024

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.

mrosales avatar mrosales commented on June 13, 2024

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.

graingert avatar graingert commented on June 13, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.