Python

[Fixed] – Need type annotation for variable in Python

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 :)

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

Ask a question or leave a comment