How to write good quality Python code with GitHub Actions

In this blog post I want to share with you how you can set up GitHub Actions workflows in your Python project to make sure that the code you write is elegant, aligned with all best practices and it’s well tested.

  • set up wemake-python-styleguide GitHub Actions workflow that checks if your code is aligned with PEP 8,
  • run unit tests and report its’ coverage with Codecov.

CodeFactor

It’s a very simple tool that check the quality of you code. It’s free of charge for all public repositories and for 1 private.

wemake-python-styleguide

Let’s move on to the second tool which will check if a code is written accordingly to the official Python style guide.

  • Set up Python that uses actions/setup-python@master configure a Python version, in our case it’s python-version: 3.8 .
  • Run unit tests runs all unit tests located in the project. For that I’m using pytest which first needs to be installed pip install pytest so the next command pytest can be run. If any test fails on this step the next one will not run.
  • Wemake Python Styleguide step is the one that we’re the most interested in. It uses the wemake-services/wemake-python-styleguide@0.13.4 action which are the atomic building blocks of the workflows. You can found them on the GitHub Marketplace, like mentioned action. This one is configured (with clause) to use github-pr-review reporter which enables the inline comments in the code review. More supported reporter options could be found on the official website. Finally this workflow requires to pass your GIHUB_TOKEN and that’s why the env clause is added.

Codecov

And finally we want to have a test report with coverage. For that we again use pytest library which will generate it for us and then we upload it to the Codecov which will take care of visualizing it.

  • uses: actions/setup-python@master — this one was also mentioned before, here we set up a Python version, which is 3.8 ,
  • then there is a new step, which is responsible for Generate coverage report; it’s a series of scripts which cover installing pytests (pip install pytest), pytest-cov (pip install pytest-cov) and running actual tests (pytest — cov=./ — cov-report=xml),
  • and finally the generated test coverage report can be uploaded to the Codecov (Upload coverage to Codecov). Here we uses: codecov/codecov-action@v1 GitHub Action available on the Marketplace. In it we provide 3 arguments: token: ${{ secrets.CODECOV_TOKEN }} which value is taken from the GitHub Secrets vault where we put it, file: ./coverage.xml is location of the test coverage report (generated step before) and flags: unittests is a flag that groups our unit tests.

Conclusion

With this blog post I hope I’ve gave you an idea of how you can set up your GitHub repository to make sure that code you write has good quality. With such tools set you can find all the bugs and vulnerabilities, but keep in mind that not every bug, not every issue is worth to be taken care of. Sometimes it’s better to focus on the real work, not on tidying smelly code base 😉.

Java Software Developer, DevOps newbie, constant learner, podcast enthusiast.