Python Lint with GitHub Actions
Tags: python, github, pylint, mypy
Published: 2019-12-02 21:46:34

There are a lot of ways to control the code quality, you have an option to put a hook on save file event and then apply black, isort, pylint or mypy, you can use a pre-commit on git to validate or fix the file on the fly too.

The problem is: it is very hard to maintain all your develop team  having all of this configured. This will create the following situation: You get a task, change the code and put the famous if, simple thing, two lines of code, but when you apply your lint you change all the file. It happens because the guy who changed the file before you did not apply the lint and the last PR reviewer didn’t see the missing lint.

Now you have to open a PR with a lot of lints changes, your PR is more complex now.

You can solve this by creating a GitHub Action that triggers for all PRs and after that you apply the lints which you think necessary.

Let's create an Action to apply the following libs:

- [black](https://github.com/psf/black)
- [pylint](https://www.pylint.org/)
- [isort](https://github.com/timothycrosley/isort)
- [pycodestyle](https://pycodestyle.readthedocs.io)
- [flake8](http://flake8.pycqa.org)
- [mypy](http://mypy-lang.org/)

If you use a GitHub suggestion, which is used for preparing the pipe for Python, to install and to execute every lib, it will be more or less like this:

```yaml
name: Python application

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Set up Python 3.7
      uses: actions/setup-python@v1
      with:
        python-version: 3.7
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Lint with flake8
      run: |
        pip install flake8
        # stop the build if there are Python syntax errors or undefined names
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

    - name: Lint with pylint
      run: |
        pip install pylint
        pylint .
        ...

    - name: Lint with black
      run: |
        pip install black
        black .
        ...

    - name: Lint with isort
      run: |
        pip install isort
        isort .
        ...

    - name: Lint with pycodestyle
      run: |
        pip install pycodestyle
        pycodestyle .
        ...

    - name: Lint with mypy
      run: |
        pip install mypy
        mypy .
        ...

```

This was my first try, when I understood that I would need to create all the configuration for every microservice project, I realized that I would need to create my own action to abstract all those things.

## Solution

To you apply the lint today, just use this action: [python-lint](https://github.com/ricardochaves/python-lint).

On your project root dir, create a directory with the name .github, inside this create another directory with the name workflow and inside this last one create one file with the name lint.yml (you can use any name here), with the following content:


```yml
name: Python Lint

on: [pull_request]

jobs:
  lint:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - uses: ricardochaves/python-lint@test
```

### How it works

- `name` - Just the action name in your project
- `on` - A list with all events that will trigger your action.
- `runs-on` - It's the image where your job will run
- `steps` - It is what the job will execute. Here we will execute two steps, one for checkout your code and the other to apply the lint

### Options

There are some options to use with this action

```yml
    - uses: ricardochaves/python-lint@test
      with:
        python-root-list: 'python_alelo tests'
        use-pylint: false
        use-pycodestyle: false
        use-flake8: false
        use-black: false
        use-mypy: false
        use-isort: false
```

You can pass a list with all directories where the files need to be tested and you can disable each lib.

## Real World

Check [python-alelo](https://github.com/ricardochaves/python-alelo/blob/master/.github/workflows/lint.yml).