yield and custom __await__ results in builtins.RuntimeError: Task got bad yield

Viewed 18

I am writing a class that has its own implemented __await__ with a calling function to retrieve the value. The __await__ is intentionally including a yield and a return but I am continuously thrown a builtins.RuntimeError: Task got bad yield error.

class Async():
    
    def __await__(self):
        yield self
        return self.get()
    
    def get():
        return "A return value"

async def get_result():
    _async = Async()
    return await _async

async def main():
    print("start main")
    await get_result()
    print("end main")

import asyncio
asyncio.run(main())
0 Answers
Related