指导新手通过修复文档 typo 提交第一个 NumPy PR 并体验 CI 流程
解读
面试官问这道题,并不是想听你背 Git 命令,而是考察三件事:
- 你是否真正“用过”NumPy 社区的工作流,而不是只在本地 pip install;
- 你对开源协作规范(fork、分支命名、commit message、PR 模板、CI 结果阅读)是否熟悉;
- 你能否把一件看似简单的“改错别字”做成符合社区标准的可合并补丁,体现你对“工程质量”的理解。
在国内面试场景里,很多候选人简历写“参与开源”,但追问细节就露馅:分支名随便叫、commit 写“fix”、CI 红了不管。这道题能把这类“伪贡献”直接筛掉。
知识点
- NumPy 文档源码位置:主仓库 docs/ 子目录,.rst 格式,使用 Sphinx 构建; typo 可能在 docstring、rst 文件或 tutorials 里。
- 开发环境:fork → clone → 创建 conda 环境 → pip install -e .[doc] → 安装 graphviz、texlive 等可选依赖,确保 make html 无警告。
- 分支策略:main 只读,从 upstream/main 切出分支,命名规范 docs/typo-xxx 或 maintenance/typo-xxx,禁止叫“patch-1”。
- 提交信息:遵循 NumPy 规范,首行 50 字内前缀 DOC:,正文空一行,结尾加 Closes #xxxxx(如果已开 issue)。
- PR 模板:必须勾选“我已阅读贡献者协议(CLA)”“我已运行生成文档并检查输出”,否则 bot 会标记 invalid。
- CI 流水线:
- Lint:pre-commit 钩子检查 trailing whitespace、black、ruff;
- Docs:circleCI 生成 html 与 pdf,artifact 链接在 PR 右下角;
- Azure Pipelines:多平台单元测试,文档 PR 会跳过测试阶段,但会跑 doctests;
- 结果判读:红叉先看“Details”,若是 circleCI 报“WARNING: undefined label”说明 rst 交叉引用写错;若是 lint 失败本地运行 pre-commit run --all-files 即可。
- 二次修改:reviewer 可能要求 squash commits、rebase 到最新 main,需熟悉 git rebase -i 与 force push 流程。
- 合并后:GitHub 会发 squash 后的邮件,确认自己的 commit 出现在 main 分支,并在 release note 的“Improved Documentation”小节留名,可写进简历。
答案
下面给出一步不漏、可直接落地的完整流程,假设发现 docs/source/user/absolute-beginners.rst 里把 “broadcasting” 写成了 “brodcasting”。
步骤 1:准备账号与权限
浏览器登录 GitHub,进入 numpy/numpy,点右上角 Fork 到自己的命名空间;本地 SSH 已配置好,git 版本 ≥ 2.30。
步骤 2:克隆与添加上游
git clone git@github.com:<你的ID>/numpy.git
cd numpy
git remote add upstream https://github.com/numpy/numpy.git
git remote -v # 确认 origin 与 upstream 指向正确
步骤 3:创建隔离环境
conda create -n numpy-doc python=3.11 -y
conda activate numpy-doc
pip install -e .[doc]
sudo apt-get install graphviz texlive-latex-recommended # Ubuntu 示例
cd docs
make clean && make html # 首次构建 5~10 min,确保无 ERROR
步骤 4:定位并修复
git fetch upstream
git checkout -b docs/typo-broadcasting upstream/main
vim source/user/absolute-beginners.rst # 搜索 brodcasting 改为 broadcasting
make html 再次生成,浏览器打开 build/html/user/absolute-beginners.html 肉眼确认。
步骤 5:提交
git add docs/source/user/absolute-beginners.rst
git commit -m "DOC: fix typo in absolute-beginners.rst
Correct 'brodcasting' to 'broadcasting'.
Closes #24123."
git push origin docs/typo-broadcasting
步骤 6:开 PR
浏览器进入自己 fork 页面,点击 “Compare & pull request”,标题自动继承 commit 首行,正文按模板填写:
- 勾选 “I have signed the CLA” (首次贡献需去 https://cla-assistant.io/numpy/numpy 电子签名);
- 勾选 “I have rendered the documentation”;
- 在评论框贴一张本地生成页面的截图,证明已肉眼检查。
步骤 7:阅读 CI 结果
约 3 分钟后,circleCI 开始跑,点 “Details” 进入 artifact 页面,确认 html 已更新且 typo 消失;若 lint 失败,本地运行 pre-commit run --all-files,自动修正后 git commit --amend && git push --force-with-lease。
步骤 8:响应 review
Reviewer 可能留言 “LGTM, could you squash the two commits into one?”
执行:
git rebase -i upstream/main # 把第二条 pick 改为 squash,保存退出,编辑合并后的 commit 信息
git push --force-with-lease
PR 页面会自动更新,CI 重新跑一遍绿灯后,maintainer 会 squash merge,GitHub 自动关闭 PR 并发送祝贺邮件。
步骤 9:后续
在本地执行 git tag --sort=-version:refname | head 查看是否已合入 main;
更新简历:Contributed to NumPy official documentation (PR #24124, merged in v1.26.0)。
拓展思考
- 如果 typo 在 docstring 而非 rst 文件,流程几乎相同,但需额外运行 python -m pytest --doctest-modules numpy/lib/stride_tricks.py 确保示例输出正确。
- 国内网络经常拉不动 circleCI artifact,可配置代理或在 PR 评论里 @numpy-bot 请求生成 pdf 预览链接。
- 若 CI 报 Sphinx 插件版本冲突,优先查看 docs/requirements.txt 是否已锁定版本,本地用 pip install -r docs/requirements.txt 而非最新版。
- 想从“改 typo”进阶到“加示例”,需阅读 NumPy Enhancement Proposal(NEP) 44 文档风格指南,学会写 alt text、交叉引用、math directive,并保证新增的示例在 32 位 Windows 也能通过。
- 面试加分项:主动提到自己用 git notes 给这次 commit 加了中文注释,方便日后回溯;或者提到把整个过程写成博客,帮助更多国内开发者迈出第一步,体现社区影响力意识。