Django Series Part 3 - Linting and Code Formatting In Django Project

Subscribe to get more of this.

Yasin Yusuf

Yasin Yusuf

Simple human being, Passionate Technophile, Coder, Entrepreneur who loves Art and Snakes

Published on: 2019-12-20

3 mins 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 linting-and-formatting.

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"

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:

  • Pylint
  • Flake8
  • AutoPep8

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, flake8 and black which has already been included in pre-commit framework so we just need to specify their ids.

-   repo:
    rev: stable
    - id: black
      language_version: python3.6
-   repo:
    rev: v1.2.3
    - id: flake8

Now run below command to initialize pre-commit in your git repo.

pre-commit install

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.

line-length = 119
include = '\.pyi?$'
exclude = '''
    | \.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.

Linting: Flake8

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:

ignore = E203, E266, E501, W503, F403, F401
max-line-length = 119
max-complexity = 18
select = B,C,E,F,W,T4,B9

That's it!

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

copyright icon

2022 Teckave. All rights reserved.

Made with