async-lru icon indicating copy to clipboard operation
async-lru copied to clipboard

[cpython 3.9] Decorating a classmethod doesn't work

Open daviddavis opened this issue 2 years ago • 6 comments

It appears that alru_cache does not seem to work for classmethods. Here's a contrived example.

import asyncio

from async_lru import alru_cache


class Incrementer:
    inc = 1

    @classmethod
    @alru_cache
    async def increment(cls, num):
        return cls.inc + num


async def main():
    print(await Incrementer.increment(5))

asyncio.run(main())

Error:

TypeError: increment() missing 1 required positional argument: 'num'

I would expect it to work since lru_cache works for classmethods.

daviddavis avatar Jul 31 '23 16:07 daviddavis

Umm, when I run your example, the output is:

6

I don't see any errors.

Dreamsorcerer avatar Jul 31 '23 22:07 Dreamsorcerer

Appears to be a problem with Python 3.9: #512 A little odd, because it works correctly in 3.8, 3.10, 3.11 and 3.12...

Dreamsorcerer avatar Jul 31 '23 23:07 Dreamsorcerer

I don't have any time to look into this, so feel free to debug and come up with a fix, or just move to another version of Python.

Dreamsorcerer avatar Jul 31 '23 23:07 Dreamsorcerer

It even works on Pypy 3.9, so it's only cpython 3.9 that fails...

Judging by the codecov change, it would appear to be this code which should be run with a classmethod and makes it work normally: https://github.com/aio-libs/async-lru/blob/565711c9b2740d522e1a0852205a63a54aa216ab/async_lru/init.py#L232-L233

Dreamsorcerer avatar Jul 31 '23 23:07 Dreamsorcerer

I occur this error early today, and I'm sure alru_cache is available when async-lru version < 2.0.0.


class Incrementer:
    inc = 1

    @classmethod
    @alru_cache
    async def increment(cls, num):
        return cls.inc + num

wangyuhuiever avatar Apr 27 '24 03:04 wangyuhuiever

Response is still the same as above. Either debug and come up with a fix or stop using cpython 3.9. We'll stop supporting 3.9 next year anyway, so it's unlikely we'll look into this.

Dreamsorcerer avatar Apr 27 '24 15:04 Dreamsorcerer