| 名称 | mcore-linting-and-formatting |
| 描述 | 用于 Megatron-LM 的代码检查与格式化。涵盖运行 autoformat.sh、工具(ruff、black、isort、pylint、mypy)以及代码风格规则。 |
| 开源协议 | Apache-2.0 when_to_use: 运行代码检查或自动格式化;在提交PR前修复风格违规;‘pre-commit 失败’、‘ruff 错误’、‘isort’、‘mypy’、‘风格违规’、‘如何格式化’、‘autoformat.sh’。 metadata: |
| 作者 | Philip Petrakian ppetrakian@nvidia.com |
代码检查与格式化
运行格式化工具
在提交 PR 之前运行:
# 检查模式(不应用更改)
BASE_REF=main CHECK_ONLY=true SKIP_DOCS=false bash tools/autoformat.sh
# 修复模式
BASE_REF=main CHECK_ONLY=false bash tools/autoformat.sh
调用的工具:black、isort、pylint、ruff、mypy。
导入排序
在编辑任何 Python 文件中的导入后,务必在提交前对这些文件运行 uv run isort:
uv run isort <file1>.py <file2>.py
设置代码检查(Linting)组
在容器内:
uv sync --locked --only-group linting
这会安装 ruff、black、isort、pylint —— 与 tools/autoformat.sh 和 CI 的 linting 任务所使用的工具相同。
代码风格规则
- 类型提示:所有公共 API 函数上都必须有。使用
X | None,而不是Optional[X]。 - 文档字符串:所有公共类和函数使用 Google 风格。
- 命名:遵循 Python 约定 —— 函数和变量使用
snake_case,类使用PascalCase。 - 行长度:119 个字符(在
pyproject.toml中配置)。 - 不要使用裸
except:始终捕获具体的异常类型。