mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Add full support for functools.lru_cache

Open gandhis1 opened this issue 7 months ago • 0 comments

I don't think there's anything actionable for mypy here in any case.

functools.partial was recently special-cased, is there any appetite to do the same for lru_cache, given it's comparably widespread usage?

Originally posted by @gandhis1 in #5107

In the above issue it was described how Mypy's support for lru_cache, which depends on typeshed, can only support enforcement of parameter hashability and the available methods on the LRU cache wrapper like cache_clear.

It can verify positional argument types, but it does not support verifying the number of arguments, or keyword arguments. Here is an example:

https://mypy-play.net/?mypy=latest&python=3.12&flags=strict&gist=5a93906d553a71d7920aafb250f2773f

This is a significant limitation, for what I would say is a very commonly-used standard library function.

In https://github.com/python/mypy/pull/16939 special support was added for functools.partial for comparable limitations in the typeshed definition. Can the same be done for functools.lru_cache?

gandhis1 avatar Jun 13 '25 14:06 gandhis1