Styling and Formatting Code
Code is read more often than it is written. -- Guido Van Rossum (author of Python)
When we write a piece of code, it's almost never the last time we see it or the last time it's edited. So we need to both explain what's going on (documentation but also make it easy to read. One of the easiest ways to make code more readable is to follow consistent style and formatting conventions.
There are many options when it comes to Python style conventions to adhere to, but most are based on PEP8 (link walks through the different components (blank lines, imports, etc.) that conventions were written for). You'll notice that different teams will default to different conventions and that's ok. The most important aspects are that everybody is consistently following the same convection and that there are pipelines in place to ensure that consistency. Let's see what this looks like in our application.
We will be using a very popular blend of style and formatting conventions that makes some very opinionated decisions on our behalf (with configurable options).
Black: an in-place reformatter that adheres to PEP8. We can explore examples of the formatting adjustments that Black makes in this demo.
isort: sorts and formats import statements inside Python scripts.
flake8: a code linter that with stylistic conventions that adhere to PEP8.
We installed all of these as they were defined in out
setup.py file under
1 2 3
Before we can properly use these tools, we'll have to configure them because they may have some discrepancies amongst them since they follow slightly different conventions that extend from PEP8. To configure Black, we could just pass in options using the CLI method, but it's much more efficient (especially so others can easily find all our configurations) to do this through a file. So we'll need to create a pyproject.toml file and place the following configurations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
The pyproject.toml was created to establish a more human-readable configuration file that is meant to replace a
setup.cfg file and is increasingly widely adopted by many open-source libraries.
Here we're telling Black that our maximum line length should be 79 characters and to include and exclude certain file extensions. We're going to follow the same configuration steps in our pyproject.toml file for configuring isort as well. Place the following configurations right below Black's configurations:
20 21 22 23 24 25 26 27
Though there is a complete list of configuration options for isort, we've decided to set these explicitly so it works well with Black.
Lastly, we'll set up flake8 but this time we need to create a separate
.flake8 file and place the following configurations:
1 2 3 4 5 6 7 8
Here we setting up some configurations like before but we're including an
ignore option to ignore certain flake8 rules so everything works with our Black and isort configurations.
Besides defining configuration options here, which are applied globally, we can also choose specifically ignore certain conventions on a line-by-line basis. Here are a few example in our code of where we utilized this method:
1 2 3 4 5 6 7
By placing the
# NOQA: <error-code> on a line, we're telling flake8 to do NO Quality Assurance for that particular error on this line.
To use these tools that we've configured, we could run these commands individually (the
. signifies that the configuration file for that package is in the current directory) but we can also use the
style target command from our
1 2 3
black . All done! ✨ 🍰 ✨ 9 files left unchanged. flake8 isort . Fixing ...
We may sometimes forget to run these style checks after we finish development. We'll cover how to automate this process using pre-commit so that these checks are automatically executed whenever we want to commit our code.
To cite this lesson, please use:
1 2 3 4 5 6