This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author uranusjr
Recipients uranusjr
Date 2022-02-03.07:28:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1643873308.65.0.92890586202.issue46622@roundup.psfhosted.org>
In-reply-to
Content
Currently, decorating a coroutine with cached_property would cache the coroutine itself. But this is not useful in any way since a coroutine cannot be awaited multiple times.

Running this code:

    import asyncio
    import functools

    class A:
        @functools.cached_property
        async def hello(self):
            return 'yo'

    async def main():
        a = A()
        print(await a.hello)
        print(await a.hello)

    asyncio.run(main())

produces:

    yo
    Traceback (most recent call last):
      File "t.py", line 15, in <module>
        asyncio.run(main())
      File "/lib/python3.10/asyncio/runners.py", line 44, in run
        return loop.run_until_complete(main)
      File "/lib/python3.10/asyncio/base_events.py", line 641, in run_until_complete
        return future.result()
      File "t.py", line 12, in main
        print(await a.hello)
    RuntimeError: cannot reuse already awaited coroutine

The third-party cached_property package, on the other hand, detects a coroutine and caches its result instead. I feel this is a more useful behaviour. https://github.com/pydanny/cached-property/issues/85
History
Date User Action Args
2022-02-03 07:28:28uranusjrsetrecipients: + uranusjr
2022-02-03 07:28:28uranusjrsetmessageid: <1643873308.65.0.92890586202.issue46622@roundup.psfhosted.org>
2022-02-03 07:28:28uranusjrlinkissue46622 messages
2022-02-03 07:28:28uranusjrcreate