This purpose of this project is to demonstrate Python type annotations and static type checking, using MyPy, in common sitautions - e.g. defining and using functions and classes. It consists of a Python 3 package with 3 modules, each of which demonstrates type annotation for a specific situation:
- function definition and usage;
- class definition and usage; and,
- named tuple definition and usage.
Extensive commentary has been included in the docstrings for each function as the aim is to read the code of a Python module that passes a static type check.
Analagous to a linter or a unit testing framwork, MyPy can be run from the command line as follows,
pipenv run python -m mypy examples/*.py
MyPy options for this project are kept in the mypy.ini
file that MyPy will look for by default. For more information on the fulls set of options, see the mypy documentation.
This project uses pipenv for managing dependencies (i.e. installing mypy) and Python environments (i.e. virtual environments).
To get started with Pipenv, first of all download it - assuming that there is a global version of Python available on your system and on the PATH, then this can be achieved by running the following command,
pip3 install pipenv
For more information, including advanced configuration options, see the official pipenv documentation.
Make sure that you're in the project's root directory (the same one in which Pipfile
resides), and then run,
pipenv install --dev
This will install all of the direct project dependencies as well as the development dependencies (the latter a consequence of the --dev
flag).