What are type hints in Python?

I’ve heard of things called type hints but I’m not sure what they are. Can anyone help?

1 Like

Type hints are a cool new feature of Python that add optional static typing to Python. This can make your IDE more useful, it can help you reason about your code better and it makes your code more self-documenting.

You can add type hints to any variable, but usually they’re added to function definitions.

Some type hints, like List need to be imported from the typing library before you can use them.

Here are some examples of type hints:

name :str = "Francis"

from typing import List
list_of_ints :List[int] = [1,2,3,4,5]

def hello(name :str) -> str:
    return "hello!" + name
1 Like