Skip to content

Manual Flake8 Python Linting

Bryce Salmi edited this page Jan 25, 2018 · 2 revisions

faradayio is automatically linted during continuous integration testing on Travis CI. However, before committing code to the repository you should manually check that you will not have any failures when linting. This saves time and prevents unneeded discourse to resolve preventable linting failures.

Linting With flake8

We use the flake8 tool to lint both locally and on Travis CI. You can read the flake8 documentation for more information. Below, we will walk through a faradayio specific linting example.

Linting faradayio With flake8

Installing faradayio also installs flake8 per the requirements.txt file. So you should be ready to go given you've followed the installation instructions. We use the virtual environment installation of flake8 to run our code but you could just use a system-wide installation.

To lint the code, navigate to the root directory of the faradayio repository and run the following command (note the virtual environment use):

(.venv)$ .venv/bin/python3 -m flake8 --exclude=.venv/,docs/

This code runs python3 from our virtual environment and calls up the command flake8. Running python3 directly in command mode ensures we are using the intended installation instead of one installed system wide. We also specified the --exclude option to ignore both our virtual environment folder (.venv) and our documentation folder (docs/) since those are not going to pass python linting and should not be checked. If you have any other virtual environment folders or test files not intended to be linted then add them to the comma separated list relative to the root directory of the repository.

When Linting Catches an Error

Below is an example of a failing test

(.env)$ .venv/bin/python3 -m flake8 --exclude=.venv/,docs/
./faradayio/faraday.py:112:43: E231 missing whitespace after ','

This caught a missing space in a function declaration on line 112 of faraday.py. The code is shown below:

self._TUN = TunnelServer(name=name,addr=addr, dstaddr=dstaddr)

Notice the missing space after the comma separating the name=name and addr=addr. This is the power of linting. It helps us keep code consistency and make our code easier to read.

We require all Pull Requests to the faradayio project to pass linting unless specific exceptions are made. It's easy and should be performed periodically during development to keep the number of errors reported manageable.