Having greater type safety around the edges of a python service can really help improve the quality of error messages and the general design of the system. This is a small refactor/bug fixing exercise to try and demonstrate how pydantic, mypy and types in general can help with this.
In the example code we've setup a system that imports entries from an external diary API and does the following:
- indexes them by title,
- indexes them by category
- stores an ordered list of entries.
The code that calls the API uses a Pydantic model to represent the shape we think the data has.
This tutorial assumes you have at least python 3.6 and have pipenv.
Run the following to prepare the environment and get the dependencies:
make setupYou can validate everything is installed by running:
make testTake a look around the code in diary. You'll be free to change
and add anything in this directory.
We've been given a dev server to test our code against to validate everything works correctly run
make run-0you should see output like:
Successfully indexed data
1000 entries
2 unique titles
1 unique categories
So our first job is complete we'r ready to go.
The software is now live. Run :
make run-1The system will catch the first validation error and display it. There's also some helpful text about why this error is now happening (what fact has been discovered).
Start altering the code to fix this bug. You can change
anything in diary and add any tests you need. Running
make test regularly will help guide you.
Once you're happy run make run-1 again. If it completes successfully
then move on to the next day.
- Focus on the class
DiaryEntrythis encodes our assumptions about the API data. - Make use of types and mypy - this is the point of the exercise after all.