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

functools.lru_cache wrapped function with TypeVar has wrong return type. #6987

Open
yumasheta opened this issue Jan 21, 2022 · 0 comments
Open

Comments

@yumasheta
Copy link

Hi,

I think there is a problem when using functools.lru_cache (and functools.cache). When the decorated function uses a TypeVar for typing arguments and return type, the wrapped function returend by functools.lru_cache has wrong (or unresolved) types.
This is not the case when replacing the TypeVar with an explicit type. See the mwe below. Thx.

import functools
from typing import TypeVar

_T = TypeVar("_T")


def my_func(elem: _T) -> list[_T]:
    return [elem]
    
my_func_cached = functools.lru_cache(my_func)

a = my_func("a")
reveal_type(a)  # Revealed type is "builtins.list[builtins.str*]"
a_c = my_func_cached("a")

# ERROR: this should be "builtins.list*[builtins.str]"
reveal_type(a_c)  # Revealed type is "builtins.list*[_T`-1]"


# this works

def my_func_str(elem: str) -> list[str]:
    return [elem]
    
my_func_str_cached = functools.lru_cache(my_func_str)

a_str = my_func_str("a")
reveal_type(a_str)  # Revealed type is "builtins.list[builtins.str]"
a_str_c = my_func_str_cached("a")
reveal_type(a_str_c)  # Revealed type is "builtins.list*[builtins.str]"

Running:
Python 3.10.1
mypy 0.931
Linux 5.16.0-arch1-1

@AlexWaygood AlexWaygood added the status: deferred Issue or PR deferred until some precondition is fixed label Jun 12, 2022
@AlexWaygood AlexWaygood removed the status: deferred Issue or PR deferred until some precondition is fixed label Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants