Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions content/guides/python/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The Python language-specific guide teaches you how to containerize a Python appl

- Containerize and run a Python application
- Set up a local environment to develop a Python application using containers
- Lint, format, typing and best practices
- Configure a CI/CD pipeline for a containerized Python application using GitHub Actions
- Deploy your containerized Python application locally to Kubernetes to test and debug your deployment

Expand Down
35 changes: 32 additions & 3 deletions content/guides/python/configure-ci-cd.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
keywords: ci/cd, github actions, python, flask
description: Learn how to configure CI/CD using GitHub Actions for your Python application.
aliases:
- /language/python/configure-ci-cd/
- /guides/language/python/configure-ci-cd/
- /language/python/configure-ci-cd/
- /guides/language/python/configure-ci-cd/
---

## Prerequisites
Expand Down Expand Up @@ -75,9 +75,34 @@
push:
branches:
- main
pull_request:
branches:
- main

jobs:
lint-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'

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

- name: Run pre-commit hooks
run: pre-commit run --all-files

- name: Run pyright
run: pyright

build:
needs: lint-test
runs-on: ubuntu-latest
steps:
- name: Login to Docker Hub
Expand Down Expand Up @@ -120,7 +145,11 @@

## Summary

In this section, you learned how to set up a GitHub Actions workflow for your Python application.
In this section, you learned how to set up a GitHub Actions workflow for your Python application that includes:

- Running pre-commit hooks for linting and formatting
- Static type checking with Pyright

Check failure on line 151 in content/guides/python/configure-ci-cd.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'Pyright'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'Pyright'?", "location": {"path": "content/guides/python/configure-ci-cd.md", "range": {"start": {"line": 151, "column": 29}}}, "severity": "ERROR"}
- Building and pushing Docker images

Related information:

Expand Down
8 changes: 4 additions & 4 deletions content/guides/python/containerize.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ This utility will walk you through creating the following files with sensible de
Let's get started!

? What application platform does your project use? Python
? What version of Python do you want to use? 3.11.4
? What version of Python do you want to use? 3.12
? What port do you want your app to listen on? 8000
? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=8000
```
Expand Down Expand Up @@ -139,8 +139,8 @@ Create a file named `Dockerfile` with the following contents.

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

ARG PYTHON_VERSION=3.11.4
FROM python:${PYTHON_VERSION}-slim AS base
ARG PYTHON_VERSION=3.12
FROM python:${PYTHON_VERSION}-slim

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1
Expand Down Expand Up @@ -181,7 +181,7 @@ COPY . .
EXPOSE 8000

# Run the application.
CMD python3 -m uvicorn app:app --host=0.0.0.0 --port=8000
CMD ["python3", "-m", "uvicorn", "app:app", "--host=0.0.0.0", "--port=8000"]
```

Create a file named `compose.yaml` with the following contents.
Expand Down
8 changes: 4 additions & 4 deletions content/guides/python/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ You'll need to clone a new repository to get a sample application that includes
Let's get started!

? What application platform does your project use? Python
? What version of Python do you want to use? 3.11.4
? What version of Python do you want to use? 3.12
? What port do you want your app to listen on? 8001
? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=8001
```
Expand Down Expand Up @@ -132,8 +132,8 @@ You'll need to clone a new repository to get a sample application that includes

# Want to help us make this template better? Share your feedback here: https:// forms.gle/ybq9Krt8jtBL3iCk7

ARG PYTHON_VERSION=3.11.4
FROM python:${PYTHON_VERSION}-slim as base
ARG PYTHON_VERSION=3.12
FROM python:${PYTHON_VERSION}-slim

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1
Expand Down Expand Up @@ -174,7 +174,7 @@ You'll need to clone a new repository to get a sample application that includes
EXPOSE 8001

# Run the application.
CMD python3 -m uvicorn app:app --host=0.0.0.0 --port=8001
CMD ["python3", "-m", "uvicorn", "app:app", "--host=0.0.0.0", "--port=8001"]
```

Create a file named `compose.yaml` with the following contents.
Expand Down
122 changes: 122 additions & 0 deletions content/guides/python/lint-format-typing.md
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
weight: 25
keywords: python, linting, formatting, type checking, ruff, pyright

Check failure on line 5 in content/guides/python/lint-format-typing.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'pyright'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'pyright'?", "location": {"path": "content/guides/python/lint-format-typing.md", "range": {"start": {"line": 5, "column": 61}}}, "severity": "ERROR"}

Check failure on line 5 in content/guides/python/lint-format-typing.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'Python' instead of 'python'. Raw Output: {"message": "[Vale.Terms] Use 'Python' instead of 'python'.", "location": {"path": "content/guides/python/lint-format-typing.md", "range": {"start": {"line": 5, "column": 11}}}, "severity": "ERROR"}
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

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'Pyright'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'Pyright'?", "location": {"path": "content/guides/python/lint-format-typing.md", "range": {"start": {"line": 20, "column": 29}}}, "severity": "ERROR"}
- Automating checks with pre-commit hooks

## 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.

Check failure on line 25 in content/guides/python/lint-format-typing.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'isort'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'isort'?", "location": {"path": "content/guides/python/lint-format-typing.md", "range": {"start": {"line": 25, "column": 112}}}, "severity": "ERROR"}

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

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'Pyright'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'Pyright'?", "location": {"path": "content/guides/python/lint-format-typing.md", "range": {"start": {"line": 67, "column": 23}}}, "severity": "ERROR"}

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

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'Pyright'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'Pyright'?", "location": {"path": "content/guides/python/lint-format-typing.md", "range": {"start": {"line": 69, "column": 1}}}, "severity": "ERROR"}

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

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'Pyright'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'Pyright'?", "location": {"path": "content/guides/python/lint-format-typing.md", "range": {"start": {"line": 80, "column": 13}}}, "severity": "ERROR"}

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:

```yaml
: https: https://github.com/charliermarsh/ruff-pre-commit
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

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'Pyright'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'Pyright'?", "location": {"path": "content/guides/python/lint-format-typing.md", "range": {"start": {"line": 113, "column": 10}}}, "severity": "ERROR"}
- Automate checks with pre-commit hooks

These tools help maintain code quality and catch errors early in development.

## Next Steps

- [Configure CI/CD](configure-ci-cd.md) to run these checks automatically

Check warning on line 120 in content/guides/python/lint-format-typing.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.Acronyms] 'CD' has no definition. Raw Output: {"message": "[Docker.Acronyms] 'CD' has no definition.", "location": {"path": "content/guides/python/lint-format-typing.md", "range": {"start": {"line": 120, "column": 17}}}, "severity": "WARNING"}
- Customize linting rules to match your team's style preferences
- Explore advanced type checking features
Loading