Need type annotation for variable in python 3.5 code

Yuval Pruss picture Yuval Pruss · Jun 18, 2017 · Viewed 12.5k times · Source

I am using mypy on my python 3.5 code, and I got a lot of messages which look like this:

file:line number: error: Need type annotation for variable

But I read about the new features in python 3.6 that it introduced the syntax for variable annotations only in python 3.6:

PEP 484 introduced the standard for type annotations of function parameters, a.k.a. type hints. This PEP adds syntax to Python for annotating the types of variables including class variables and instance variables...

And if I am trying to add variable type annotations to my variables in the python 3.5 program, It throws SyntaxError.

What should I do? Ignore this messages? Update to python 3.6? Why the mypy compiles my code like it's written in python 3.6?

Answer

jOOsko picture jOOsko · Jun 18, 2017

Use comments to annotate variable type

x = 5 # type: int
my_list = [] # type: List[str]

Check cheat sheet

https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html