Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need type annotation for variable in python 3.5 code

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?

like image 278
Yuval Pruss Avatar asked Jun 18 '17 13:06

Yuval Pruss


People also ask

Should I use type annotations in Python?

Type hints work best in modern Pythons. Annotations were introduced in Python 3.0, and it's possible to use type comments in Python 2.7. Still, improvements like variable annotations and postponed evaluation of type hints mean that you'll have a better experience doing type checks using Python 3.6 or even Python 3.7.

How do you annotate a type in Python?

To annotate return value type, add -> immediately after closing the parameter parentheses, just before the function definition colon( : ): def announcement(language: str, version: float) -> str: ... The function now has type hints showing that it receives str and float arguments, and returns str .

Can you type variables in Python?

Specify a Variable TypeCasting in python is therefore done using constructor functions: int() - constructs an integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number)

What are variable annotations in Python?

Variable Annotation is basically an enhancement of type hinting, which was introduced in Python 3.5. The full explanation behind Variable Annotation is explained in PEP 526. In this article, we give have a quick refresher on type hinting and then introduce the new Variable Annotation syntax.


2 Answers

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

like image 53
jOOsko Avatar answered Sep 20 '22 19:09

jOOsko


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.

like image 45
Dimitris Fasarakis Hilliard Avatar answered Sep 22 '22 19:09

Dimitris Fasarakis Hilliard