Skip to content

Possible bug: @lru_cache causes an error on generic functions #5123

Answered by erictraut
dmontagu asked this question in Q&A
Discussion options

You must be logged in to vote

This unfortunate behavior is due to the way that typeshed defines lru_cache.

def lru_cache(maxsize: Callable[..., _T], typed: bool = False) -> _lru_cache_wrapper[_T]: ...

Where _lru_cache_wrapper is a class defined as:

@final
class _lru_cache_wrapper(Generic[_T]):
    __wrapped__: Callable[..., _T]
    def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T: ...
    ...

Because the typeshed definition uses a ..., all of the input parameters of the wrapped call are lost, and there's no way to "solve" the return type for a wrapped generic function.

To fix this, _lru_cache_wrapper could be rewritten to use a ParamSpec.

def lru_cache(maxsize: Callable[_P, _T], typed: bool = False) -> _l…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dmontagu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants