mcore-linting-and-formatting
NVIDIA/skills
使用 ruff、black、isort、pylint 和 mypy 对 Megatron-LM 的 Python 代码进行 Lint 和格式化,并提供自动格式化和导入排序的命令。
...展开全部代码检查与格式化
运行格式化工具
在提交拉取请求(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
</file2></file1>设置代码检查组
在容器内部:
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:必须始终捕获特定的异常类型。
---
name: mcore-linting-and-formatting
description: Lint and format Python code for Megatron-LM using ruff, black, isort, pylint, and mypy, with commands for autoformatting and import ordering.
license: Apache-2.0
---
# Linting and Formatting
---
## Running the Formatter
Run before opening a PR:
```bash
# Check mode (no changes applied)
BASE_REF=main CHECK_ONLY=true SKIP_DOCS=false bash tools/autoformat.sh
# Fix mode
BASE_REF=main CHECK_ONLY=false bash tools/autoformat.sh
```
Tools invoked: `black`, `isort`, `pylint`, `ruff`, `mypy`.
---
## Import Ordering
After editing imports in any Python files, always run `uv run isort` on those
files before committing:
```bash
uv run isort <file1>.py <file2>.py
```
---
## Setting Up the Linting Group
Inside the container:
```bash
uv sync --locked --only-group linting
```
This installs `ruff`, `black`, `isort`, `pylint` — the same tools used by
`tools/autoformat.sh` and CI's `linting` job.
---
## Code Style Rules
- **Type hints**: required on all public API functions. Use `X | None`, not `Optional[X]`.
- **Docstrings**: Google-style on all public classes and functions.
- **Naming**: follow Python conventions — `snake_case` for functions and variables, `PascalCase` for classes.
- **Line length**: 119 characters (configured in `pyproject.toml`).
- **No bare `except`**: always catch specific exception types.





首页
