You may encounter an issue with Python 3.5+ versions while defining variables, this is because Python 3.6 introduced a new syntax under PEP526 for annotating the variables.

Issue:

>>> items = [{}]

The above line causes an error called error: Need type annotation for items this is simply because of the PEP526.

Fix Need type annotation for variable in python:

Define the type of the variables with comments or variable:type expression.

from typing import Any, List, Dict

items = [{}]  # type: List[Dict[str, Any]]
name = ''  # type: str
i = 10 # type: int

#Or

items: List[Dict[str, Any]]
name: str = ''
i: int = 10

It can be worked with class and instance variables too.

Done.

References:

Happy Learning 🙂

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment