Mypy brief introduction
In this webpage we are trying to introduce Mypy to you.
What is mypy?
Mypy is a static type checker for python. If you use annotations while writing the code, mypy can check the code syntax and find common bugs. The annotations that are used in the program do not affect the work-flow of the code and can be considered as hints used in code.
Using python 3 annotation syntax or comment-based annotation syntax for python 2, we can annotate our code and use Mypy to debug common errors. Mypy also helps maintaining and understanding the program easier.
What should we know to start using mypy?
We need to install mypy on the computer first (You can find more information about installation on both Mac and Windows ).
Mypy syntax can be complex at some special situation, like this:
from typing import Callable def feeder(get_next_item: Callable[[], str]) -> None: # Body def async_query(on_success: Callable[[int], None], on_error: Callable[[int, Exception], None]) -> None: # Body
It all depends on what kind of type you are going to check. In this example we are checking a Callback Function.
But most time we don’t need to use callback function or something else complex like that. If you want to start experiencing mypy quickly, all the thing you need to know is :
- Basic syntax of mypy
- Common built-in types
- Some examples about mypy (Read only Built-in types and Functions)
After adding type hints and type annotations to your code, you can run following command in your command line to test your code.
mypy PROGRAM_NAME
For other information please go to mypy introduction .