|
| 1 | +--- |
| 2 | +title: Linting, formatting, and type checking for Python |
| 3 | +linkTitle: Linting and typing |
| 4 | +weight: 25 |
| 5 | +keywords: Python, linting, formatting, type checking, ruff, pyright |
| 6 | +description: Learn how to set up linting, formatting and type checking for your Python application. |
| 7 | +aliases: |
| 8 | + - /language/python/lint-format-typing/ |
| 9 | +--- |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +Complete [Develop your app](develop.md). |
| 14 | + |
| 15 | +## Overview |
| 16 | + |
| 17 | +In this section, you'll learn how to set up code quality tools for your Python application. This includes: |
| 18 | + |
| 19 | +- Linting and formatting with Ruff |
| 20 | +- Static type checking with Pyright |
| 21 | +- Automating checks with pre-commit hooks |
| 22 | + |
| 23 | +## Linting and formatting with Ruff |
| 24 | + |
| 25 | +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. |
| 26 | + |
| 27 | +Create a `pyproject.toml` file: |
| 28 | + |
| 29 | +```toml |
| 30 | +[tool.ruff] |
| 31 | +target-version = "py312" |
| 32 | + |
| 33 | +[tool.ruff.lint] |
| 34 | +select = [ |
| 35 | + "E", # pycodestyle errors |
| 36 | + "W", # pycodestyle warnings |
| 37 | + "F", # pyflakes |
| 38 | + "I", # isort |
| 39 | + "B", # flake8-bugbear |
| 40 | + "C4", # flake8-comprehensions |
| 41 | + "UP", # pyupgrade |
| 42 | + "ARG001", # unused arguments in functions |
| 43 | +] |
| 44 | +ignore = [ |
| 45 | + "E501", # line too long, handled by black |
| 46 | + "B008", # do not perform function calls in argument defaults |
| 47 | + "W191", # indentation contains tabs |
| 48 | + "B904", # Allow raising exceptions without from e, for HTTPException |
| 49 | +] |
| 50 | +``` |
| 51 | + |
| 52 | +### Using Ruff |
| 53 | + |
| 54 | +Run these commands to check and format your code: |
| 55 | + |
| 56 | +```bash |
| 57 | +# Check for errors |
| 58 | +ruff check . |
| 59 | + |
| 60 | +# Automatically fix fixable errors |
| 61 | +ruff check --fix . |
| 62 | + |
| 63 | +# Format code |
| 64 | +ruff format . |
| 65 | +``` |
| 66 | + |
| 67 | +## Type checking with Pyright |
| 68 | + |
| 69 | +Pyright is a fast static type checker for Python that works well with modern Python features. |
| 70 | + |
| 71 | +Add `Pyright` configuration in `pyproject.toml`: |
| 72 | + |
| 73 | +```toml |
| 74 | +[tool.pyright] |
| 75 | +typeCheckingMode = "strict" |
| 76 | +pythonVersion = "3.12" |
| 77 | +exclude = [".venv"] |
| 78 | +``` |
| 79 | + |
| 80 | +### Running Pyright |
| 81 | + |
| 82 | +To check your code for type errors: |
| 83 | + |
| 84 | +```bash |
| 85 | +pyright |
| 86 | +``` |
| 87 | + |
| 88 | +## Setting up pre-commit hooks |
| 89 | + |
| 90 | +Pre-commit hooks automatically run checks before each commit. The following `.pre-commit-config.yaml` snippet sets up Ruff: |
| 91 | + |
| 92 | +```yaml |
| 93 | + https: https://github.com/charliermarsh/ruff-pre-commit |
| 94 | + rev: v0.2.2 |
| 95 | + hooks: |
| 96 | + - id: ruff |
| 97 | + args: [--fix] |
| 98 | + - id: ruff-format |
| 99 | +``` |
| 100 | +
|
| 101 | +To install and use: |
| 102 | +
|
| 103 | +```bash |
| 104 | +pre-commit install |
| 105 | +git commit -m "Test commit" # Automatically runs checks |
| 106 | +``` |
| 107 | + |
| 108 | +## Summary |
| 109 | + |
| 110 | +In this section, you learned how to: |
| 111 | + |
| 112 | +- Configure and use Ruff for linting and formatting |
| 113 | +- Set up Pyright for static type checking |
| 114 | +- Automate checks with pre-commit hooks |
| 115 | + |
| 116 | +These tools help maintain code quality and catch errors early in development. |
| 117 | + |
| 118 | +## Next steps |
| 119 | + |
| 120 | +- [Configure GitHub Actions](configure-github-actions.md) to run these checks automatically |
| 121 | +- Customize linting rules to match your team's style preferences |
| 122 | +- Explore advanced type checking features |
0 commit comments