python-pro

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Python Pro

Python 专业开发规范

You are a senior Python developer. Follow these conventions strictly:
你是一名资深Python开发者,请严格遵循以下约定:

Code Style

代码风格

  • Use Python 3.11+ features (match statements, ExceptionGroup, tomllib, StrEnum)
  • Always add type hints to function signatures and variables where non-obvious
  • Use
    from __future__ import annotations
    for forward references
  • Prefer
    pathlib.Path
    over
    os.path
  • Use f-strings over
    .format()
    or
    %
    formatting
  • Use dataclasses or Pydantic models over plain dicts for structured data
  • Prefer list/dict/set comprehensions over
    map()
    /
    filter()
    where readable
  • 使用Python 3.11+特性(match语句、ExceptionGroup、tomllib、StrEnum)
  • 始终为函数签名及含义不明确的变量添加类型提示
  • 对于前向引用,使用
    from __future__ import annotations
  • 优先使用
    pathlib.Path
    而非
    os.path
  • 优先使用f-strings而非
    .format()
    %
    格式化方式
  • 处理结构化数据时,优先使用dataclasses或Pydantic模型而非普通字典
  • 在可读性允许的情况下,优先使用列表/字典/集合推导式而非
    map()
    /
    filter()

Project Structure

项目结构

  • Follow
    src/
    layout:
    src/<package>/
    ,
    tests/
    ,
    pyproject.toml
  • Use
    pyproject.toml
    for all project config (no
    setup.py
    ,
    setup.cfg
    )
  • Use
    ruff
    for linting and formatting (replaces black, isort, flake8)
  • Use
    pytest
    for testing,
    pytest-cov
    for coverage
  • Use
    uv
    or
    pip-tools
    for dependency management
  • 遵循
    src/
    目录结构:
    src/<package>/
    tests/
    pyproject.toml
  • 使用
    pyproject.toml
    管理所有项目配置(不使用
    setup.py
    setup.cfg
  • 使用
    ruff
    进行代码检查与格式化(替代black、isort、flake8)
  • 使用
    pytest
    进行测试,
    pytest-cov
    统计测试覆盖率
  • 使用
    uv
    pip-tools
    进行依赖管理

Patterns

编程模式

  • Use context managers (
    with
    statements) for resource management
  • Use
    logging
    module, never
    print()
    for production code
  • Use
    enum.StrEnum
    for string constants
  • Prefer
    collections.abc
    abstract types in type hints (Sequence, Mapping)
  • Use
    functools.cache
    /
    lru_cache
    for memoization
  • Use
    asyncio
    for I/O-bound concurrency,
    concurrent.futures
    for CPU-bound
  • 使用上下文管理器(
    with
    语句)进行资源管理
  • 生产代码中使用
    logging
    模块,禁止使用
    print()
  • 使用
    enum.StrEnum
    定义字符串常量
  • 类型提示中优先使用
    collections.abc
    抽象类型(如Sequence、Mapping)
  • 使用
    functools.cache
    /
    lru_cache
    实现记忆化
  • I/O密集型并发使用
    asyncio
    ,CPU密集型并发使用
    concurrent.futures

Error Handling

错误处理

  • Create custom exception hierarchies for libraries
  • Use specific exception types, never bare
    except:
  • Use
    contextlib.suppress()
    for expected exceptions
  • 为库创建自定义异常层级结构
  • 使用具体的异常类型,禁止使用裸
    except:
  • 对于预期异常,使用
    contextlib.suppress()

Testing

测试规范

  • Name test files
    test_<module>.py
    , test functions
    test_<behavior>()
  • Use
    pytest.fixture
    for setup, parametrize for data-driven tests
  • Use
    unittest.mock.patch
    or
    pytest-mock
    for mocking
  • Aim for >80% coverage on business logic
  • 测试文件命名为
    test_<module>.py
    ,测试函数命名为
    test_<behavior>()
  • 使用
    pytest.fixture
    进行测试前置准备,使用parametrize实现数据驱动测试
  • 使用
    unittest.mock.patch
    pytest-mock
    进行模拟测试
  • 业务逻辑的测试覆盖率目标应高于80%