Python最佳实践 python-best-practices

这个技能提供了Python编程的现代最佳实践指南,涵盖类型提示、数据类、异步模式、项目打包和测试等方面,旨在帮助开发者提高代码质量、可维护性和性能。关键词:Python, 类型提示, 异步编程, 数据类, 打包, 测试, 最佳实践, 项目结构, 虚拟环境。

后端开发 0 次安装 0 次浏览 更新于 3/8/2026

名称: python-最佳实践 描述: 具有现代类型提示、数据类、异步模式、打包和测试的Pythonic代码

Python最佳实践

类型提示(3.12+语法)

# 使用内置泛型(3.9+),无需typing.List, typing.Dict
def process_items(items: list[str]) -> dict[str, int]:
    return {item: len(item) for item in items}

# 使用 | 语法的联合类型(3.10+)
def find_user(user_id: int) -> User | None:
    ...

# 类型参数语法(3.12+)
type Vector[T] = list[T]
type Matrix[T] = list[Vector[T]]

def first[T](items: list[T]) -> T:
    return items[0]

# 用于结构化字典的TypedDict
from typing import TypedDict

class UserResponse(TypedDict):
    id: int
    name: str
    email: str
    active: bool

始终为函数签名添加类型提示。在CI中使用 mypy --strictpyright。谨慎使用 type: ignore 注释,并附上理由。

数据类 vs Pydantic

数据类(内部数据,无需验证)

from dataclasses import dataclass, field

@dataclass(frozen=True, slots=True)
class Point:
    x: float
    y: float

    def distance_to(self, other: "Point") -> float:
        return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5

@dataclass
class Config:
    host: str = "localhost"
    port: int = 8080
    tags: list[str] = field(default_factory=list)

使用 frozen=True 用于不可变值对象。使用 slots=True 提高内存效率。

Pydantic(外部输入,需要验证)

from pydantic import BaseModel, Field, field_validator

class CreateUserRequest(BaseModel):
    model_config = {"strict": True}

    email: str = Field(max_length=255)
    name: str = Field(min_length=1, max_length=100)
    age: int = Field(ge=13, le=150)

    @field_validator("email")
    @classmethod
    def validate_email(cls, v: str) -> str:
        if "@" not in v:
            raise ValueError("邮箱格式无效")
        return v.lower()

规则:使用数据类用于领域模型和内部结构。使用Pydantic用于API边界、配置文件和外部数据解析。

异步模式

import asyncio
import httpx

async def fetch_user(client: httpx.AsyncClient, user_id: int) -> User:
    response = await client.get(f"/users/{user_id}")
    response.raise_for_status()
    return User(**response.json())

async def fetch_all_users(user_ids: list[int]) -> list[User]:
    async with httpx.AsyncClient(base_url="https://api.example.com") as client:
        tasks = [fetch_user(client, uid) for uid in user_ids]
        return await asyncio.gather(*tasks)

async def process_with_semaphore(items: list[str], max_concurrent: int = 10):
    semaphore = asyncio.Semaphore(max_concurrent)
    async def bounded_process(item: str):
        async with semaphore:
            return await process_item(item)
    return await asyncio.gather(*[bounded_process(i) for i in items])

规则:

  • 使用 httpx 替代 requests 进行异步HTTP请求。
  • 使用 asyncio.gather 处理并发任务,asyncio.Semaphore 进行速率限制。
  • 永远不要在异步函数中调用阻塞I/O(对于旧代码,使用 asyncio.to_thread)。
  • 使用 async with 管理资源(如连接、会话)。

项目结构

my-project/
  src/
    my_project/
      __init__.py
      main.py
      models.py
      services/
        __init__.py
        user_service.py
      api/
        __init__.py
        routes.py
  tests/
    conftest.py
    test_models.py
    test_services/
      test_user_service.py
  pyproject.toml

使用 src 布局,防止从项目根目录意外导入。

pyproject.toml

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "my-project"
version = "1.0.0"
requires-python = ">=3.12"
dependencies = [
    "httpx>=0.27",
    "pydantic>=2.0",
]

[project.optional-dependencies]
dev = [
    "pytest>=8.0",
    "pytest-cov",
    "pytest-asyncio",
    "mypy",
    "ruff",
]

[project.scripts]
my-project = "my_project.main:cli"

[tool.ruff]
line-length = 100
target-version = "py312"

[tool.ruff.lint]
select = ["E", "F", "I", "N", "UP", "B", "SIM", "RUF"]

[tool.mypy]
strict = true

[tool.pytest.ini_options]
asyncio_mode = "auto"
testpaths = ["tests"]

使用 pyproject.toml 进行所有工具配置。使用Ruff替代flake8 + isort + black(单一工具,速度提升10-100倍)。

虚拟环境

# 使用uv进行快速依赖管理
uv venv
uv pip install -e ".[dev]"

# 或标准venv
python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"

始终使用虚拟环境。永远不要全局安装包。在锁文件中固定确切版本(uv.lock 或从 pip freeze 生成的 requirements.txt)。

使用pytest进行测试

import pytest
from unittest.mock import AsyncMock, patch

@pytest.fixture
def user_service(db_session):
    return UserService(session=db_session)

async def test_create_user_returns_user_with_hashed_password(user_service):
    user = await user_service.create(email="test@example.com", password="secret")
    assert user.email == "test@example.com"
    assert user.password_hash != "secret"

async def test_create_user_rejects_duplicate_email(user_service):
    await user_service.create(email="test@example.com", password="secret")
    with pytest.raises(DuplicateEmailError):
        await user_service.create(email="test@example.com", password="other")

@pytest.fixture
def mock_http_client():
    client = AsyncMock(spec=httpx.AsyncClient)
    client.get.return_value = httpx.Response(200, json={"id": 1, "name": "Alice"})
    return client

async def test_fetch_user_parses_response(mock_http_client):
    user = await fetch_user(mock_http_client, user_id=1)
    assert user.name == "Alice"
    mock_http_client.get.assert_called_once_with("/users/1")

使用 conftest.py 共享fixture。使用 pytest.mark.parametrize 处理测试变体。使用 tmp_path fixture进行文件系统测试。

Pythonic惯用法

# 解包
first, *rest = items
x, y = point

# 使用推导式替代map/filter
squares = [x**2 for x in numbers if x > 0]
lookup = {u.id: u for u in users}

# 使用上下文管理器清理资源
with open(path) as f:
    data = f.read()

# 海象运算符用于赋值和测试
if (match := pattern.search(text)) is not None:
    process(match.group(1))

# 结构模式匹配(3.10+)
match command:
    case {"action": "move", "direction": d}:
        move(d)
    case {"action": "quit"}:
        sys.exit(0)
    case _:
        raise ValueError(f"未知命令: {command}")

错误处理

class AppError(Exception):
    def __init__(self, message: str, code: str):
        super().__init__(message)
        self.code = code

class NotFoundError(AppError):
    def __init__(self, resource: str, id: str):
        super().__init__(f"{resource} {id} 未找到", "NOT_FOUND")

# 捕获特定异常,永远不要捕获所有异常
try:
    user = await get_user(user_id)
except NotFoundError:
    return {"error": "用户未找到"}, 404
except DatabaseError as e:
    logger.exception("获取用户时数据库错误")
    return {"error": "内部错误"}, 500

永远不要使用裸 except:。捕获最具体的异常。使用 logger.exception() 包含跟踪信息。为应用程序定义自定义异常层次结构。