type提示:如何定义lru_cache的类型?

Dav*_*ang 7 python mypy

鉴于以下功能

@lru_cache(maxsize=1)
def get_response_from_api() -> List[ApiObject]:
    url = _get_api_url()
    response = requests.get(url).text
    return json.loads(response, object_hook=_create_api_obj)
Run Code Online (Sandbox Code Playgroud)

跑步的时候

mypy predictor --ignore-missing-imports --strict

我收到错误消息:

错误:无类型装饰器使函数"get_response_from_api"无类型化

我怎样才能解决这个问题 ?

我应该如何注释lru_cache函数?

Dav*_*ang 4

这实际上是 mypy 中的一个错误:https ://github.com/python/mypy/issues/5107

根据Ethan Smith(mypy 的核心开发人员)的说法:由于该get_response_from_api函数不带参数,因此类型被推断为lru_cache[Any]。这应该得到修复