Home > Net >  Using type hint Any in Django - NameError: name 'Any' is not defined
Using type hint Any in Django - NameError: name 'Any' is not defined

Time:06-11

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?

CodePudding user response:

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.

  • Related