MyPy: annotation required for variable type in python 3.5 code
I am using mypy
python 3.5 for my code and I got a lot of messages that look like this:
file: line number: error: variable type annotation required
But I read about new features in python 3.6
that he introduced the syntax for variable annotations only in python 3.6
:
PEP 484 introduced a standard for function parameter type annotations, type hints aka This PEP adds Python syntax to annotate variable types, including class variables and instance variables ...
And if I try to add type variable annotations to my variables in the program python 3.5
, it throws SyntaxError
.
What should I do? Ignore this message? Upgrade to python 3.6
? Why is it mypy
compiling my code as written in python 3.6
?
source to share
Your code obfuscates the inference of the type it is trying to execute mypy
. For example, redefining the name as in the following snippet prevents mypy from inferring the type f
:
f = []
f = {}
Since he can't figure out what the type should be f
, he complains and tells you that he needs an annotation for the variable. You can explicitly specify the hint type:
- Type comment for Python 3.5.
- Modified annotation for Python 3.6
mypy
does not compile to 3.6
, this bug exists in both versions. The difference is how you can handle it.
source to share