2

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?

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Also needed to import Dict
Note that with Python 3.10+ the typing versions of List, Dict, Set and Frozenset are being deprecated. For more information on typing in general you should read over the official documentation. link
More detail needed. What replaces them?
@pgrad starting from python 3.9, actually

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.