-
Notifications
You must be signed in to change notification settings - Fork 7.7k
EDIT: Update python code guide and include linting, formatting and type checking #22737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
7aeb66a
83ea065
142f127
a078a99
ff99867
6973c8d
a4e4a5e
4113619
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
--- | ||
title: Linting, Formatting and Type Checking for Python | ||
linkTitle: Linting and Typing | ||
estebanx64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
weight: 25 | ||
keywords: Python, linting, formatting, type checking, ruff, pyright | ||
description: Learn how to set up linting, formatting and type checking for your Python application. | ||
aliases: | ||
- /language/python/lint-format-typing/ | ||
--- | ||
|
||
## Prerequisites | ||
|
||
Complete [Develop your app](develop.md). | ||
|
||
## Overview | ||
|
||
In this section, you'll learn how to set up code quality tools for your Python application. This includes: | ||
|
||
- Linting and formatting with Ruff | ||
- Static type checking with Pyright | ||
Check failure on line 20 in content/guides/python/lint-format-typing.md
|
||
- Automating checks with pre-commit hooks | ||
|
||
## Linting and Formatting with Ruff | ||
estebanx64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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: | ||
|
||
```toml | ||
[tool.ruff] | ||
target-version = "py312" | ||
|
||
[tool.ruff.lint] | ||
select = [ | ||
"E", # pycodestyle errors | ||
"W", # pycodestyle warnings | ||
"F", # pyflakes | ||
"I", # isort | ||
"B", # flake8-bugbear | ||
"C4", # flake8-comprehensions | ||
"UP", # pyupgrade | ||
"ARG001", # unused arguments in functions | ||
] | ||
ignore = [ | ||
"E501", # line too long, handled by black | ||
"B008", # do not perform function calls in argument defaults | ||
"W191", # indentation contains tabs | ||
"B904", # Allow raising exceptions without from e, for HTTPException | ||
] | ||
``` | ||
|
||
### Using Ruff | ||
|
||
Run these commands to check and format your code: | ||
|
||
```bash | ||
# Check for errors | ||
ruff check . | ||
|
||
# Automatically fix fixable errors | ||
ruff check --fix . | ||
|
||
# Format code | ||
ruff format . | ||
``` | ||
|
||
## Type Checking with Pyright | ||
Check failure on line 67 in content/guides/python/lint-format-typing.md
|
||
estebanx64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Pyright is a fast static type checker for Python that works well with modern Python features. | ||
Check failure on line 69 in content/guides/python/lint-format-typing.md
|
||
|
||
Add `Pyright` configuration in `pyproject.toml`: | ||
|
||
```toml | ||
[tool.pyright] | ||
typeCheckingMode = "strict" | ||
pythonVersion = "3.12" | ||
exclude = [".venv"] | ||
``` | ||
|
||
### Running Pyright | ||
Check failure on line 80 in content/guides/python/lint-format-typing.md
|
||
|
||
To check your code for type errors: | ||
|
||
```bash | ||
pyright | ||
``` | ||
|
||
## Setting up pre-commit hooks | ||
|
||
Pre-commit hooks automatically run checks before each commit. in `.pre-commit-config.yaml` sets up Ruff: | ||
estebanx64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```yaml | ||
: https: https://github.com/charliermarsh/ruff-pre-commit | ||
estebanx64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rev: v0.2.2 | ||
hooks: | ||
- id: ruff | ||
args: [--fix] | ||
- id: ruff-format | ||
``` | ||
|
||
To install and use: | ||
|
||
```bash | ||
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 | ||
Check failure on line 113 in content/guides/python/lint-format-typing.md
|
||
- Automate checks with pre-commit hooks | ||
|
||
These tools help maintain code quality and catch errors early in development. | ||
|
||
## Next Steps | ||
estebanx64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- [Configure Github Actions](configure-github-actions.md) to run these checks automatically | ||
Check failure on line 120 in content/guides/python/lint-format-typing.md
|
||
estebanx64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- Customize linting rules to match your team's style preferences | ||
- Explore advanced type checking features |
Uh oh!
There was an error while loading. Please reload this page.