12

Given the following function

@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)

when running

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

I am getting the error message:

error: Untyped decorator makes function "get_response_from_api" untyped

How can i fix this ?

How should i annotate the lru_cache function?

David Michael Gang
  • 7,107
  • 8
  • 53
  • 98
  • @NPE - i don't get how this link answers my question. I already annotated my function but the lru_cache could somehow change the return value. Which type annotation should i put in the lru_cache line to tell that i return List[ApiObject]? – David Michael Gang May 23 '18 at 19:29
  • It looks like Mypy includes an annotation for `lru_cache` [1](https://github.com/python/typeshed/blob/8b84e9cf13ee20afd801e3d03b7a6700fea69222/stdlib/3/functools.pyi#L23) but it does not work for me either. – Nova May 23 '18 at 22:31

1 Answers1

4

This is actually a bug in mypy: https://github.com/python/mypy/issues/5107

According to Ethan Smith (which is a core developer of mypy): as the get_response_from_api function takes no arguments, the type is inferred as lru_cache[Any]. This should be fixed

Paolo
  • 20,112
  • 21
  • 72
  • 113
David Michael Gang
  • 7,107
  • 8
  • 53
  • 98