Flake8 With Django

unsplash

I’m working on an Django project. I want to add flake8 to check my django codes today, and make code changes must be checked before commit.

Here’s what I did.

Introduction

I found all my needed from the official Flake8 document, and below is what I did:

  • must check before Git commit
  • custome flake8 config file, due to django allow line length longer than 80 characters.

Installation

Use pip can install flake8 quickly by: pip install flake8

Config Flake8

Create a file named .flake8 in Django’s root directory, the .flake8 will be used when call flake8 to check.

My custome config file is like below:

1
2
3
4
5
6
7
8
9
10
11
12
[flake8]
ignore = D203
exclude =
.git,
__pycache__,
docs/source/conf.py,
old,
build,
dist,
*migrations*
max-complexity = 10
max-line-length = 119

My intention is:

  • ignore D203
  • exclude files include .git, pycache, old, build, and migrations. All the exclude is not python files, or files not need to be checked
  • set max-complexity to 10
  • expand man line lengh to 119, PEP8 only allow 80 length

Call Flake8

Flake8 can be called by flake8 <path>

Must pass Flake8 before git commit

I want to ask git check all my code changes to satisfy Flake8 standards, below command is ask git to do so:

1
2
flake8 --install-hook git  # install git hook
git config --bool flake8.strict true # config git hook

references

0%