写得清楚,不如写得规范。本文带你搭建一套现代化、自动化、可维护的 Python 项目开发规范体系。
在团队协作中,代码不仅是给机器执行的指令,更是给人阅读和维护的文档。如果没有统一规范,就会出现:
解决方案:用工具链 + 流程约束,把“好习惯”自动化。
pip install black但随着生态演进,一个更强大的工具出现了——
Ruff 是当前 Python 社区最热门的工具之一,它能一站式替代多个传统工具:
black | ruff format | |
isort | ruff check --select I | |
flake8 | ruff check |
✅ 优势:
--fix);# 推荐使用 uv(更快)uv add --dev ruff# 检查代码问题ruff check .# 格式化代码ruff format .# 检查并自动修复问题ruff check --fix .Python 的类型注解是静态提示,运行时不生效。这意味着:
defgreet(name: str) -> str:returnf"Hello, {name}"greet(123) # 运行不会报错!必须配合静态类型检查工具:
pyproject.toml 配置 mypy;pyproject.toml[tool.mypy]python_version = "3.10"strict = truewarn_unused_configs = true💡 关键认知:类型注解的核心价值是——让 IDE 更聪明、让代码自文档化、让错误提前暴露。
pre-commit 是 Git 提交前的“质量守门员”,确保只有合格的代码才能进入仓库。
开发者写代码 ↓保存时自动格式化(Ruff / Black+isort+flake8) ↓git commit 触发 pre-commit ↓自动运行:格式化 + lint + 类型检查 + 文件校验 ↓全部通过 → 允许提交;任一失败 → 阻止提交.pre-commit-config.yamlrepos:# Ruff:格式化 + lint-repo:https://github.com/astral-sh/ruff-pre-commitrev:v0.15.5hooks:-id:ruff-format-id:ruffargs:[--fix,--exit-non-zero-on-fix]# MyPy:类型检查-repo:https://github.com/pre-commit/mirrors-mypyrev:v1.19.1hooks:-id:mypyadditional_dependencies:[types-requests]# 通用钩子:尾随空格、YAML/TOML 校验等-repo:https://github.com/pre-commit/pre-commit-hooksrev:v6.0.0hooks:-id:trailing-whitespace-id:end-of-file-fixer-id:check-yaml-id:check-tomlpip install pre-commitpre-commit install✅ 最佳实践:
轻量检查放 pre-commit(快速反馈);重型测试(如完整 pytest)放 CI。
即使本地通过了 pre-commit,仍需在服务器端做最终验证。
.github/workflows/python-ci.ymlname:PythonCIon:push:branches:[main,master]pull_request:branches:[main,master]jobs:lint-fix:runs-on:ubuntu-lateststeps:-uses:actions/checkout@v4-name:SetupPythonuses:actions/setup-python@v5with:python-version:"3.13"-name:InstallRuffrun:pipinstallruff-name:RunRuffandauto-fixrun:| ruff check --fix modules/ ruff format modules/-name:Commitchangesuses:stefanzweifel/git-auto-commit-action@v5with:commit_message:"style: auto-fix linting issues"branch:${{github.head_ref}}test:runs-on:ubuntu-lateststrategy:matrix:python-version:["3.13"]steps:-uses:actions/checkout@v4-name:SetupPython${{matrix.python-version}}uses:actions/setup-python@v5with:python-version:${{matrix.python-version}}-name:Installdependenciesrun:| python -m pip install --upgrade pip pip install -e . pip install pytest mypy ruff-name:Runlintersandtypecheckerrun:| ruff format --check modules/ ruff check modules/ mypy modules/-name:Runtestsrun:| pytest🔒 核心原则:所有 PR 必须通过 CI 才能合并,杜绝“在我机器上能跑”的问题。
pyproject.toml现代 Python 项目推荐将所有工具配置集中到 pyproject.toml,作为“唯一真相源”。
[tool.ruff]target-version = "py313"line-length = 88exclude = [ ".git", ".venv", "__pycache__", ".mypy_cache", ".pytest_cache", "tmp_input", "tmp_output",][tool.ruff.lint]select = [ "E", # pycodestyle 错误 "W", # pycodestyle 警告 "F", # Pyflakes "I", # isort (导入排序) "B", # flake8-bugbear "C4", # flake8-comprehensions "UP", # pyupgrade "N", # pep8-naming "Q", # flake8-quotes "PTH", # flake8-use-pathlib (推荐使用 pathlib) "SIM", # flake8-simplify]# 忽略的规则#ignore = [# "E501", # 行太长(如果需要严格检查,可以删除这行)#]# 允许自动修复的规则fixable = ["ALL"][tool.ruff.format]# 引用风格:优先使用双引号quote-style = "double"# 缩进风格:空格indent-style = "space"# 是否跳过魔法下划线检测skip-magic-trailing-comma = false# 换行符line-ending = "auto"[tool.ruff.lint.per-file-ignores]# 测试文件可以忽略某些规则"**/tests/*" = ["PLR2004"]"**/examples/*" = ["PLR2004"]✅ 好处:
编辑器、pre-commit、CI 都读同一份配置; 无需在 .vscode/settings.json中重复定义。
在 .vscode/settings.json 中启用自动化:
{// 保存时自动格式化 + 整理 imports"editor.formatOnSave": true,"editor.codeActionsOnSave": {"source.organizeImports": "explicit" },// 明确指定 .py 文件使用 Ruff 格式化器"[python]": {"editor.defaultFormatter": "charliermarsh.ruff" },"ruff.interpreter": ["python" ],"python.testing.pytestArgs": ["." ],"python.testing.unittestEnabled": false,"python.testing.pytestEnabled": true}💡 注意:Ruff 插件会自动读取
pyproject.toml,无需额外配置参数。
# noqa: E731# type: ignore[attr-defined]# ❌ 不推荐square = lambda x: x * x# ✅ 推荐defsquare(x):return x * xs = "武沛齐"b = s.encode("utf-8")print(f"{b!r}") # 输出 b'\xe6\xad\xa6\xe6\xb2\x9b\xe9\xbd\x90'pyformat 工具已归档;| 开发时 | ||
| 提交前 | ||
| 合并前 | ||
| 配置中心 | pyproject.toml |
🌟 终极目标:让机器去做重复劳动,让人专注于逻辑与设计。
附:推荐工具栈
✍️ 本文整理自真实项目经验,所有配置均可直接复用。如果你觉得有用,欢迎点赞、转发,也欢迎在评论区分享你的规范实践!