Streamlining Your Python Workflow with Black, Ruff, GitHub Actions, and Pre-Commit Hooks
Tags: githubactions, python, productivity, testing
Published: 2023-03-26 08:34:27

In today's fast-paced software development world, maintaining code quality and consistency is of utmost importance. Python, being one of the most popular programming languages, has several tools to help developers achieve this goal. Black, a Python code formatter, and Ruff, a Python linter, are two such tools that can help ensure that your Python code is formatted correctly and follows best practices. 

In this dev blog, we'll explore how to use Black and Ruff to automatically format and lint your Python code, and integrate them seamlessly into your workflow using GitHub Actions and pre-commit hooks. By the end of this blog, you'll have a better understanding of how to use these tools to improve the quality of your Python code and streamline your development process.

## Black ✨
Black is an open-source Python code formatter that is designed to make your code more readable and maintainable by enforcing a consistent style. It is a fast and efficient tool that automatically reformats your code to adhere to the official Python style guide, PEP 8. Check out the repo [here.](https://github.com/psf/black#installation-and-usage)

## Ruff ⚡
Ruff is a Python linter and code formatter tool. It is designed to help developers maintain consistent and clean Python code by providing automated code analysis and formatting. Some of its key features include support for various Python versions, integration with popular IDEs and editors, and customisable configuration options. Check out the well written documentation [here.](https://beta.ruff.rs/docs/installation/)

## Integrating the tools in workflow

To keep your code in top notch shape, ready for production deployment with minimal requirements, it is important to have these checks present at all touch points of the software development lifecycle.

Pre-commit Hooks and Github Actions come into picture to ensure just that.

## Pre-commit hooks

Pre-commit hooks are automated checks that run before code changes are committed. They enforce code style consistency and prevent issues, such as poorly formatted code, through checks like formatting, linting, unit testing, and security scanning. Pre-commit hooks catch problems early and prevent them from causing larger issues.

## Github Actions

GitHub Actions is a built-in automation tool in GitHub that allows developers to automate tasks like building, testing, and deploying code. It has a flexible YAML-based syntax, integrates with various tools, and offers pre-built actions in a marketplace.

---

Pre-commit hooks and Github Actions work in sync with each other. One ensures the code committed to version control meets defined standards, the other ensures those high standards are maintained in collaborative settings.

Now, we will see how to install, configure and implement these tools to achieve our productivity goals.

## Workflow

To better understand the implementation details, we will refer to a live example, which relies on Black for code formatting, on Ruff for linting, pre-commit hooks to ensure only PEP8 grade code is committed and Github Actions to ensure these standards across the code lifecycle.

### Installation 

```
pip install git+https://github.com/psf/black
pip install ruff
pip install pre-commit
```

### Configuring pre-commit hooks

1. Create a `.pre-commit-config.yaml` file at the root directory of your project folder.
2. Initialise the following sample hooks:

```
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.3.0
    hooks:
      - id: check-yaml
      - id: end-of-file-fixer
      - id: trailing-whitespace
  - repo: https://github.com/psf/black
    rev: 22.10.0
    hooks:
      - id: black
  - repo: https://github.com/charliermarsh/ruff-pre-commit
    # Ruff version.
    rev: "v0.0.257"
    hooks:
      - id: ruff
```
3. Install the hooks using `pre-commit install`
4. Do a test run using `pre-commit run --all-files`

### Configuring Github Actions

We don't have dedicated testing actions for Black and Ruff so we will write the actions in the standard `.github/workflows` directory.

```
name: Python Linting and Formatting

on: [push, pull_request]

jobs:
  lint:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: "3.x"

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Lint with Ruff
        run: ruff .

      - name: Format with Black
        run: black .
```

Just commit your changes and experience the magic of automated testing ✨✨✨