Search code examples
pythonpython-typing

Type hints cause syntax error in Python 3.5


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?


Solution

  • 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.