Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type hints cause syntax error in Python 3.5 [duplicate]

I've got a code base that I recently sprinkled liberally with type hints. It is python 3.5+ exclusively and the python 3.5 changelog claims type hinting is supported. Unfortunately all the hints raise syntax errors, likewise with simple hints in the interpreter:

$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
>>> a: int = 5
  File "<stdin>", line 1
    a: int = 5
     ^
SyntaxError: invalid syntax

Any ideas?

like image 744
user2952698 Avatar asked Jul 25 '26 11:07

user2952698


1 Answers

That's an example of a variable annotation, which wasn't introduced until Python 3.6. A variable annotation, defined by PEP-526, is distinct from a function annotation. Type hints are just one possible use for either type of annotation.

like image 184
chepner Avatar answered Jul 28 '26 16:07

chepner