Using type hint Any in Django – NameError: name 'Any' is not defined

Issue

When I use the autocomplete suggestion in VSCode to make a get_context_data() function:

    def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
        return super().get_context_data(**kwargs)

I get a NameError:
NameError: name 'Any' is not defined

I am new to using type hints in Python – do I need to import something for type Any?

Solution

Any in this context is a type annotation. You need to import it from the typing module for it to be recognized.

from typing import Any

Should solve your problem.

Answered By – pgrad

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published