Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alru_cache decorator on a class method causes mypy error: Missing positional argument "self" #104

Closed
atusy opened this issue Mar 20, 2023 · 6 comments · Fixed by #105
Closed
Labels
typing bug Some type hints aren't working

Comments

@atusy
Copy link

atusy commented Mar 20, 2023

Thank you for the amazing project.

I'm trying to apply alru_cache on a class method, but mypy raises an error and a note:

error: Missing positional argument "self" in call to "f" of "X"  [call-arg]
note: "__call__" is considered instance variable, to make it class variable use ClassVar[...]

below is the reproducible example tested with mypy 1.1.1 and asyncstdlib 3.10.5

from asyncstdlib.functools import lru_cache as alru_cache

class X:
    @alru_cache()
    async def f(self) -> int:
        return 1

    async def g(self) -> int:
        return await self.f()
@maxfischer2781 maxfischer2781 added the typing bug Some type hints aren't working label Mar 20, 2023
@maxfischer2781
Copy link
Owner

This might take a bit. The standard library type hints just ignore all arguments, making the implicit-self case trivially covered. I need to check if it is possible to cover the self case explicitly or disable argument checking only for methods.

@maxfischer2781
Copy link
Owner

It looks like it is not possible to stitch things together using ParamSpec: python/mypy#13222

@maxfischer2781
Copy link
Owner

Turns out in principle it is possible to annotate a descriptor (__get__ method) to have ParamSpec stitch together instance/class and other parameters. However, it doesn't seem possible to break up a captured "async callable" TypeVar this way. So it is not clear how the function and method annotations can co-exist cleanly.

For the time being, #105 will implement a middle ground: Full type support on functions, parameter erasure on methods.

@atusy
Copy link
Author

atusy commented Apr 24, 2023

Wow! Thank you for your hard work.

@maxfischer2781
Copy link
Owner

Looks like I found out how to stitch together self and other parameters. Method parameters should fully work with #129.

@atusy
Copy link
Author

atusy commented Mar 3, 2024

How nice!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typing bug Some type hints aren't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants