Linting, formatting, and type checking for Python
Prerequisites
Complete Develop your app. This topic requires a local Python installation because the tools and Git hooks introduced here run on your host. If you don't want to install Python locally, skip this topic. The same checks run in CI in the next topic.
Overview
Linting, formatting, and type checking are automated ways to catch bugs, enforce style, and spot type errors before code runs. Running them on every commit, in CI, and in your editor catches problems early when they're cheap to fix.
In this section, you'll configure three tools for your Python application. Ruff handles linting and formatting in a single fast pass. Pyright statically checks your code for type errors. Pre-commit hooks run both of these automatically before each Git commit so problems are caught locally before they're committed.
Linting and formatting with Ruff
Ruff is an extremely fast Python linter and formatter written in Rust. It replaces multiple tools like flake8, isort, and black with a single unified tool.
Create a pyproject.toml file in your python-docker-example directory:
Install Ruff:
$ pip install ruff
If you're using a virtual environment, make sure it is activated so the ruff
command is available.
Run these commands to check and format your code:
# Check for errors
$ ruff check .
# Automatically fix fixable errors
$ ruff check --fix .
# Format code
$ ruff format .
Type checking with Pyright
Pyright is a fast static type checker for Python that works well with modern Python features.
Update pyproject.toml to add the Pyright configuration at the bottom.
Install Pyright and run it:
$ pip install pyright
$ pyright
Setting up pre-commit hooks
Pre-commit hooks run checks automatically before each commit on your local
machine. Create a .pre-commit-config.yaml file in your python-docker-example
directory to set up Ruff hooks:
To install and use:
$ pip install pre-commit
$ pre-commit install
$ git commit -m "Test commit" # Automatically runs checks
Summary
In this section, you learned how to:
- Configure and use Ruff for linting and formatting
- Set up Pyright for static type checking
- Automate checks with pre-commit hooks
These tools help maintain code quality and catch errors early in development.
Related information:
Next steps
- Configure GitHub Actions to run these checks automatically
- Customize linting rules to match your team's style preferences
- Explore advanced type checking features