Code Quality Checking with Flake8
Tags: tutorial, programming, python, productivity
Published: 2023-12-21 22:55:27

Flake8 is a very popular tool in the Python community it's very useful for checking the style and quality of your Python code. It's amazing for ensuring that your code adheres to `PEP 8` guidelines and as well as for finding bugs.

## The Basics

You can use Flake8 inside of your Django application to clean up your code as well as making sure it adheres to the `PEP 8` guidelines.

**Installation**

First we'll need to install Flake8. We'll do this using pip. Run the command below to install flake8

```
pip3 install flake8
```
after installing flake8, we can check if flake8 has been successfully installed. By running the command.

```
python3 install -m flake8 --version
```
You should see a version number along with some other packages alongside it. It might look something like this.

```
6.1.0 (mccabe: 0.7.0, pycodestyle: 2.11.1, pyflakes: 3.1.0) CPython 3.12.0 on
Darwin
```

We're going configure Flake8 to ignore certain files or errors, or to adjust its complexity thresholds. Go into your projects root and create a .flake8 file with these configurations.

```
[flake8]
ignore = E501, W503
exclude = .git,__pycache__,old,build,dist
max-complexity = 10

```
These configuration's tell flake8 a few things:

`ignore`: Is a list of error codes to ignore.

`exclude`: Directories or files to exclude from any checks.

`max-complexity`: Sets the maximum allowed McCabe complexity for your functions.

If you want to know more about you can learn more here [McCabe Complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity).

## Executing Flake8

Time to run `Flake8` manually by executing the flake8 command in your project's root directory. This will check all Python files in your project.

```
flake8 path/to/your/project
```

**Automating Flake8**

To automate the execution of flake8 process, you can integrate `Flake8` checks into your development workflow. You can be do this in a few ways.

You can use something such as Pre-commit Hooks, which are tools like `pre-commit` to run Flake8 before each commit.

You can also run Flake8 as part of your CI pipeline using tools like GitHub Actions, GitLab CI, or Jenkins.

Flake8 will report back any stylistic errors or code smells that it finds. Each issue is reported with a code that you can look up in the Flake8 documentation to understand more about the issue and how to fix it.

**Common Error Codes**
There could be a few error codes that come out when executing flake8. Here are some of the most common ones along with an explanation.

**Pyflakes**

PyFlakes checks for logical errors within your code.

```
F401: 'module' imported but unused
F821: undefined name 'name'
F403: 'from module import *' used; unable to detect undefined names
```
**pycodestyle**

pycodestyle focuses on style according to the `PEP 8` guidelines.

```
E501: Line too long (more than 79 characters)
E302: Expected 2 blank lines, found 1
E305: Expected 2 blank lines after class or function definition, found 1
W291: Trailing whitespace
E303: Too many blank lines
```

**McCabe**
Finally, McCabe checks the complexity of your code.

C901: 'function' is too complex (the score)

After identifying issues, you should fix them to improve the quality and readability of your code. This might involve reformatting code, reducing complexity, or adhering more closely to PEP 8 guidelines.

**IDE Integration**

Many IDEs like PyCharm, VSCode have Flake8 integration or plugins, which can help you identify issues as you code.

**Optionally**
Flake8 can be used in conjunction with other tools like Black (for code formatting) and isort (for import sorting) to further enhance code quality.

**Final Notes**

While Flake8 is a very valuable tool for maintaining code quality, it's not a substitute for good coding practices or code reviews. It's best used as part of a broader quality assurance strategy.