Need type annotation for variable in python 3.5 code
If you are having blank value, you must define the type of variable. For example:
my_val: str = ""
my_val1: dict = {}
my_val2: list = []
etc. In your case, I will consider changing the version of python to 3.6 and update code is required.
Your code is confusing the type inference that mypy
tries to do. For example, redefining a name as in the following snippet, doesn't allow mypy to deduce the type of f
:
f = []
f = {}
Since it can't understand what the type of f
is supposed to be, it complains and tells you that it needs an annotation for the variable. You can explicitly provide a type-hint with:
- A type comment for Python 3.5.
- A variable annotation for Python 3.6
mypy
isn't compiling in 3.6
, this error exists in both versions. The difference is in how you can tackle it.
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