DEV Community

Ricardo Baltazar Chaves
Ricardo Baltazar Chaves

Posted on

Python Lint with GitHub Actions

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:

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:

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.

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:

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

    - 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.

Top comments (1)

Collapse
 
sobolevn profile image
Nikita Sobolev • Edited

You can also use wemake-python-styleguide as a pre-build github action.
It is the strictest python linter out there. It has a huge amount of style, semantic, and consistency rules inside.

Usage:

- name: wemake-python-styleguide
  uses: wemake-services/wemake-python-styleguide

Link: github.com/marketplace/actions/wem...
Docs: wemake-python-stylegui.de/en/lates...

GitHub logo wemake-services / wemake-python-styleguide

The strictest and most opinionated python linter ever!

wemake-python-styleguide

wemake.services Supporters Build Status codecov Python Version wemake-python-styleguide


Welcome to the strictest and most opinionated python linter ever.

wemake-python-styleguide logo

wemake-python-styleguide is actually a flake8 plugin with some other plugins as dependencies.

Quickstart

pip install wemake-python-styleguide

You will also need to create a setup.cfg file with the configuration.

We highly recommend to also use:

  • flakehell for easy integration into a legacy codebase
  • nitpick for sharing and validating configuration across multiple projects

Running

flake8 your_module.py

This app is still just good old flake8 And it won't change your existing workflow.

invocation results

See "Usage" section in the docs for examples and integrations.

We also support GitHub Actions as first class-citizens Try it out!

What we are about

The ultimate goal of this project is to make all people write exactly the same python code.

flake8 pylint black mypy wemake-python-styleguide
Formats code?
Finds style issues? 🤔 🤔
Finds bugs? 🤔

Give it a try!