Django Series Part 3 - Linting and Code Formatting In Django Project
Published: December 20, 2019 Time: 4 min read
One of the most important things in software development is time. Linting saves so much time. In this post we will learn about how to setup linting for your django projects or python as general. If you have been following this django series, here is the code that we wrote so far. Each post will have it’s own branch, for this one the branch name is
What is Linting?
According to Wikipedia lint or a linter, is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs.
Simple explanation: it is a tool that goes through source code with the goal of finding issues that can cause errors or disobey the code style you choose. For instance:
title = "linting and formatting" print(titel)
Clearly you can see that this program will output error when you try to run it because the variable
"titel" we are trying to print is not defined. At time you realize the program is not functioning correctly you already lost few seconds! Linters run in the background and can detect these errors and let you know about them without you leaving your code editor.
There are two types of linters:
- Logical linters : These linters caught the logical bugs like undefined variables as we see above.
- Stylistic linters: These linters look for stylistic issues like number of characters in a line or indentation.
Every programming language has linters and Python isn’t an exception. To name a few of the popular linter in python:
In this tutorial we will use flake8, because it is beginner friendly and has powerful features.
Setting up Linting
Although linters can be used as CLI tools, it is a pain in the rare to run the linting command every time you want to commit your code to source control or it can be forgotten entirely. To avoid this we will setup our linting process to automatically run when we run the git commit command. We will use git hooks and pre-commit package to achieve this, lets install and setup pre-commit.
pipenv install pre-commit
In the root project directory create
.pre-commit-config.yaml a config file which configures the sources where the hooks will be taken from. In our case,
black which has already been included in pre-commit framework so we just need to specify their ids.
repos: - repo: https://github.com/ambv/black rev: stable hooks: - id: black language_version: python3.6 - repo: https://github.com/pre-commit/pre-commit-hooks rev: v1.2.3 hooks: - id: flake8
Now run below command to initialize pre-commit in your git repo.
Code Formatting: Black
Black is an opinionated tool that formats your code in the best way possible. You can check it’s design decisions in this repo. One of the notable formatting decisions is:
- Line length is 88 characters, not 119 as Django recommends in their contributor style guide or 79 as PEP8.
Good thing, they have an option to overwrite the rules. we just need to create and put our configuration in
pyproject.toml file as mentioned below and everything is all set.
[tool.black] line-length = 119 include = '\.pyi?$' exclude = ''' /( \.git | \.hg | \.mypy_cache | \.tox | \.venv | _build | buck-out | build | dist )/ '''
Note: If you are not a fan of black, there’s always autopep8 a formatter more faithful to PEP8.
Flake8 is a powerful tool that checks our code’s compliance to PEP8. In order for black to work nicely with flake8, we need to list down some error codes to ignore. Create
.flake8 in the root project directory and put in configuration below:
[flake8] ignore = E203, E266, E501, W503, F403, F401 max-line-length = 119 max-complexity = 18 select = B,C,E,F,W,T4,B9
Now try to add and commit your code to git. You should see that both black and flake8 are running against your code.
I hope you enjoyed, If you have any questions, use the comments section below, we will help you. If you liked this post get the word out to your friends and consider joining us. We will notify you for freebies and more posts. Click here to be part of us