Python 3.5 introduced optional type annotation for functions, and that functionality was extended in Python 3.6.
The mypy static type checker can use this annotation to detect type errors.
Type checking can be done using mypy.
mypy.ini: mypy configuration file.correct.py: code that has type annotations, and no type errors.incorrect_01.py: code that has type annotations, and passes a string to a function that expects anint.incorrect_02.py: code that has type annotations, and the result of a function that returns anintis assigned to astrvariable.incorrect_03.py: code that has type annotations, and the result of a function that returns anint, assigns it to a variable that is later used as astr.dict_correct.py: code that counts the words in a text read from standard input.dict_incorrect.py: code that counts the words in a text read from standard input. The counts are subsequently normalized tofloat, which is a type error.dict_correct_type_statement.py: same code asdict_correct.py, but with a type variable as type statement.people_incorrect.py: code that defines aPeopleclass, stores some in a list with mistakes.duck_typing.py: example code illustrating duck typing.duck_typing_incorrect.py: example code illustrating duck typing, but with an error.typed_duck_typing.py: example code illustrating duck typing using type hints.typed_duck_typing_clean.py: example code illustrating duck typing using type hints with a factory function.typed_duck_typing_incorrect.py: example code illustrating duck typing using type hints with an error.numpy_typing.py: illustration of a script using both numpy and matplotlib with type hints.classes.py: illustration of using type hints with a user-defined class.classes_incorrect.py: illustration of using type hints with a user-defined class with errors.tree.py: illustration of using type hints on more sophisticated classes, as well as generic types.new_types.py: illustration of usingNewTypeto create specific types with specific semantics.