In this lesson, you’ll learn about type comments. As you saw, annotations were introduced in Python 3, and they haven’t been backported to Python 2. This means that, if you’re writing code that needs to support legacy Python, then you can’t use annotations.
Instead, you can use type comments. These are specially formatted comments that can be used to add type hints compatible with older code. Type comments will not be available in the __annotations__
dictionary. To add type comments to a function, you do this:
def func(arg):
# type:(str) -> str
...
For variables, add the type comment on the same line:
my_variable = 42 # type: int
francoisg on Dec. 12, 2019
small heads up: 04:00 you call mypy on headlines instead of headline1