Skip to content

Commit 7aeb66a

Browse files
committed
EDIT: Update python code guide and include linting, formatting and type checking
1 parent 3a609cd commit 7aeb66a

File tree

5 files changed

+163
-11
lines changed

5 files changed

+163
-11
lines changed

content/guides/python/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The Python language-specific guide teaches you how to containerize a Python appl
1919

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

content/guides/python/configure-ci-cd.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ weight: 40
55
keywords: ci/cd, github actions, python, flask
66
description: Learn how to configure CI/CD using GitHub Actions for your Python application.
77
aliases:
8-
- /language/python/configure-ci-cd/
9-
- /guides/language/python/configure-ci-cd/
8+
- /language/python/configure-ci-cd/
9+
- /guides/language/python/configure-ci-cd/
1010
---
1111

1212
## Prerequisites
@@ -75,9 +75,34 @@ to Docker Hub.
7575
push:
7676
branches:
7777
- main
78+
pull_request:
79+
branches:
80+
- main
7881

7982
jobs:
83+
lint-test:
84+
runs-on: ubuntu-latest
85+
steps:
86+
- uses: actions/checkout@v4
87+
88+
- name: Set up Python
89+
uses: actions/setup-python@v5
90+
with:
91+
python-version: '3.12'
92+
93+
- name: Install dependencies
94+
run: |
95+
python -m pip install --upgrade pip
96+
pip install -r requirements.txt
97+
98+
- name: Run pre-commit hooks
99+
run: pre-commit run --all-files
100+
101+
- name: Run pyright
102+
run: pyright
103+
80104
build:
105+
needs: lint-test
81106
runs-on: ubuntu-latest
82107
steps:
83108
- name: Login to Docker Hub
@@ -120,7 +145,11 @@ Save the workflow file and run the job.
120145

121146
## Summary
122147

123-
In this section, you learned how to set up a GitHub Actions workflow for your Python application.
148+
In this section, you learned how to set up a GitHub Actions workflow for your Python application that includes:
149+
150+
- Running pre-commit hooks for linting and formatting
151+
- Static type checking with Pyright
152+
- Building and pushing Docker images
124153

125154
Related information:
126155

content/guides/python/containerize.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ This utility will walk you through creating the following files with sensible de
5858
Let's get started!
5959

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

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

142-
ARG PYTHON_VERSION=3.11.4
143-
FROM python:${PYTHON_VERSION}-slim AS base
142+
ARG PYTHON_VERSION=3.12
143+
FROM python:${PYTHON_VERSION}-slim
144144

145145
# Prevents Python from writing pyc files.
146146
ENV PYTHONDONTWRITEBYTECODE=1
@@ -181,7 +181,7 @@ COPY . .
181181
EXPOSE 8000
182182

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

187187
Create a file named `compose.yaml` with the following contents.

content/guides/python/develop.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ You'll need to clone a new repository to get a sample application that includes
5151
Let's get started!
5252

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

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

135-
ARG PYTHON_VERSION=3.11.4
136-
FROM python:${PYTHON_VERSION}-slim as base
135+
ARG PYTHON_VERSION=3.12
136+
FROM python:${PYTHON_VERSION}-slim
137137

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

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

180180
Create a file named `compose.yaml` with the following contents.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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. in `.pre-commit-config.yaml` 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 CI/CD](configure-ci-cd.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

Comments
 (0)