linting-and-formatting
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLinting and Formatting
代码检查与格式化
Single source of truth for code style in Megatron Bridge. Read this before
writing new code or reviewing PRs.
Megatron Bridge代码风格的唯一权威参考。编写新代码或审查PR前请阅读本文。
Style Guides
风格指南
- Python: Google Python Style Guide
- Shell: Google Shell Style Guide
Target Python 3.10+.
- Python:Google Python风格指南
- Shell:Google Shell风格指南
目标Python版本为3.10+。
Formatting and Linting
格式化与代码检查
Run before every commit:
bash
uv run ruff check --fix .
uv run ruff format .Pre-commit hooks run these automatically. If hooks auto-fix files, re-stage
and re-run until clean:
bash
git add -u
pre-commit run每次提交前请运行以下命令:
bash
uv run ruff check --fix .
uv run ruff format .预提交钩子会自动执行这些操作。如果钩子自动修复了文件,请重新暂存并再次运行直到无问题:
bash
git add -u
pre-commit runif it auto-fixed files:
如果自动修复了文件:
git add -u
pre-commit run
undefinedgit add -u
pre-commit run
undefinedRuff Rules (from ruff.toml
)
ruff.tomlRuff规则(来自ruff.toml
)
ruff.toml| Rule | ID | Description |
|---|---|---|
| Line length | — | 119 characters (formatter) |
| Quote style | — | Double quotes |
| f-string without placeholders | F541 | Error |
| Unused local variable | F841 | Auto-removed by |
| Unused import | F401 | Auto-removed by |
| Ambiguous variable name | E741 | Error (e.g., |
| Undefined name | F821 | Error |
| Block comment format | E266 | Error (too many |
| Import sorting | I | isort-compatible, auto-fixed |
| Public class docstring | D101 | Warning (ignored in test files) |
| Public function docstring | D103 | Warning (ignored in test files) |
Per-file overrides:
- : F401 and F403 ignored (re-exports expected).
__init__.py - ,
test_*.py,*_test.py: D101 and D103 ignored.tests/*.py
| 规则 | ID | 描述 |
|---|---|---|
| 行长度 | — | 119个字符(格式化工具) |
| 引号风格 | — | 双引号 |
| 无占位符的f-string | F541 | 错误 |
| 未使用的局部变量 | F841 | 由 |
| 未使用的导入 | F401 | 由 |
| 模糊变量名 | E741 | 错误(例如 |
| 未定义名称 | F821 | 错误 |
| 块注释格式 | E266 | 错误(过多 |
| 导入排序 | I | 与isort兼容,自动修复 |
| 公共类文档字符串 | D101 | 警告(测试文件中忽略) |
| 公共函数文档字符串 | D103 | 警告(测试文件中忽略) |
按文件覆盖规则:
- :忽略F401和F403(允许重新导出)。
__init__.py - 、
test_*.py、*_test.py:忽略D101和D103。tests/*.py
Naming Conventions
命名约定
| Kind | Convention | Example |
|---|---|---|
| Files | snake_case | |
| Classes | PascalCase | |
| Functions/methods | snake_case | |
| Local variables | snake_case | |
| Variables starting with digit | prefix | |
| Global variables | UPPER_SNAKE + prefix | |
| Constants | UPPER_SNAKE | |
- Avoid shadowing variables from an outer scope.
- Initialize all externally visible class members in the constructor.
| 类型 | 约定 | 示例 |
|---|---|---|
| 文件 | snake_case | |
| 类 | PascalCase | |
| 函数/方法 | snake_case | |
| 局部变量 | snake_case | |
| 以数字开头的变量 | 前缀 | |
| 全局变量 | UPPER_SNAKE + 前缀 | |
| 常量 | UPPER_SNAKE | |
- 避免遮蔽外部作用域的变量。
- 所有对外可见的类成员需在构造函数中初始化。
Import Order
导入顺序
- imports
__future__ - Standard library
- Third-party (,
megatron.core,torch, etc.)transformers - First-party ()
megatron.bridge.* - Local folder imports
Separate groups with blank lines. ruff auto-fixes via the rule.
I- 导入
__future__ - 标准库
- 第三方库(、
megatron.core、torch等)transformers - 第一方库()
megatron.bridge.* - 本地文件夹导入
不同组之间用空行分隔。ruff会通过规则自动修复导入顺序。
IType Hints
类型提示
Required on all public API functions and methods.
- Use instead of
T | NoneOptional[T] - Use instead of
X | YUnion[X, Y] - Use built-in generics (,
list,dict) instead oftupleequivalentstyping
python
def get_module_by_name(
model: torch.nn.Module,
name: str,
default: torch.nn.Module | None = None,
) -> torch.nn.Module | None:
...所有公共API函数和方法都必须添加类型提示。
- 使用替代
T | NoneOptional[T] - 使用替代
X | YUnion[X, Y] - 使用内置泛型(、
list、dict)替代tuple模块中的等效类型typing
python
def get_module_by_name(
model: torch.nn.Module,
name: str,
default: torch.nn.Module | None = None,
) -> torch.nn.Module | None:
...Mypy
Mypy
Run on changed files before submitting:
bash
uv run mypy --strict path/to/file.pyKey rules:
- No leaks — use
Anyfor unknown types or aobjectfor generics.TypeVar - No untyped defs — every function must have parameter and return annotations.
- No implicit — write
Optional, neverx: int | None = None.x: int = None - Explicit casts — use only when inference fails; add a comment.
typing.cast() - Typed dictionaries — prefer over
TypedDictfor structured dicts.dict[str, Any] - Callable signatures — use or
Callable[[ArgType], ReturnType].Protocol - Ignore sparingly — must include the error code and justification.
# type: ignore[code]
提交前请对修改的文件运行:
bash
uv run mypy --strict path/to/file.py核心规则:
- 禁止类型泄漏 —— 未知类型使用
Any,泛型使用object。TypeVar - 禁止无类型定义 —— 每个函数必须有参数和返回值注解。
- 禁止隐式—— 需写成
Optional,绝不能写成x: int | None = None。x: int = None - 显式类型转换 —— 仅当类型推断失败时使用,并添加注释说明。
typing.cast() - 类型化字典 —— 结构化字典优先使用而非
TypedDict。dict[str, Any] - 可调用签名 —— 使用或
Callable[[ArgType], ReturnType]。Protocol - 谨慎忽略类型错误 —— 必须包含错误代码和理由。
# type: ignore[code]
Keyword-Only Arguments for Ambiguous Parameters
模糊参数使用仅关键字参数
When a function has multiple parameters of the same type that could be swapped
by mistake, use to force keyword-only arguments.
*python
undefined当函数存在多个同类型参数,容易被误交换时,使用强制使用仅关键字参数。
*python
undefinedDon't
不推荐
def scatter_weights(tensor: Tensor, tp_group: ProcessGroup, ep_group: ProcessGroup): ...
def scatter_weights(tensor: Tensor, tp_group: ProcessGroup, ep_group: ProcessGroup): ...
Do
推荐
def scatter_weights(tensor: Tensor, *, tp_group: ProcessGroup, ep_group: ProcessGroup): ...
undefineddef scatter_weights(tensor: Tensor, *, tp_group: ProcessGroup, ep_group: ProcessGroup): ...
undefinedDocstrings
文档字符串
Google-style for public classes and functions:
python
def convert_weights(
source_model: torch.nn.Module,
target_model: torch.nn.Module,
mapping: MegatronParamMapping,
) -> dict[str, torch.Tensor]:
"""Convert weights from source to target model format.
Args:
source_model: The source model containing weights to convert.
target_model: The target model that will receive converted weights.
mapping: Parameter mapping defining the conversion rules.
Returns:
Dictionary mapping parameter names to converted weight tensors.
Raises:
ValueError: If source and target models have incompatible shapes.
"""公共类和函数使用Google风格:
python
def convert_weights(
source_model: torch.nn.Module,
target_model: torch.nn.Module,
mapping: MegatronParamMapping,
) -> dict[str, torch.Tensor]:
"""将权重从源模型格式转换为目标模型格式。
参数:
source_model: 包含待转换权重的源模型。
target_model: 将接收转换后权重的目标模型。
mapping: 定义转换规则的参数映射。
返回:
参数字典,键为参数名称,值为转换后的权重张量。
异常:
ValueError: 如果源模型和目标模型的形状不兼容。
"""Comments
注释
- Commented-out code must have a comment explaining why. Otherwise remove it.
- Comments explain non-obvious intent, trade-offs, or constraints — not what the code does.
- 被注释掉的代码必须附带说明原因的注释,否则请删除。
- 注释用于解释非显而易见的意图、权衡或约束,而非代码的功能。
Logging
日志
Use for module-level loggers. Use
/ for user-facing messages in distributed contexts.
logging.getLogger(__name__)print_rank_0warn_rank_0python
undefined模块级日志使用。在分布式环境中,面向用户的消息使用/。
logging.getLogger(__name__)print_rank_0warn_rank_0python
undefinedDon't
不推荐
print(f"Loading weights for {model_name}")
print(f"Loading weights for {model_name}")
Do
推荐
logger = logging.getLogger(name)
logger.info("Loading weights for %s", model_name)
undefinedlogger = logging.getLogger(name)
logger.info("Loading weights for %s", model_name)
undefinedError Handling
错误处理
Use specific exceptions. Keep try bodies minimal.
python
try:
state_dict = torch.load(path)
except FileNotFoundError:
raise ValueError(f"Checkpoint not found at {path}") from None
else:
result = convert(state_dict)使用具体的异常类型。try代码块应尽可能精简。
python
try:
state_dict = torch.load(path)
except FileNotFoundError:
raise ValueError(f"Checkpoint not found at {path}") from None
else:
result = convert(state_dict)Avoid Reflection
避免反射
python
undefinedpython
undefinedDon't
不推荐
def make_config(*args):
x, y = args
return dict(**locals())
def make_config(*args):
x, y = args
return dict(**locals())
Do
推荐
def make_config(x, y):
return {"x": x, "y": y}
undefineddef make_config(x, y):
return {"x": x, "y": y}
undefinedConfiguration and Dataclasses
配置与数据类
- Use or
dataclassesfor configuration objects.NamedTuple - Do not add arbitrary defaults — be explicit about required vs optional fields.
- 使用或
dataclasses作为配置对象。NamedTuple - 不要添加任意默认值——明确区分必填字段和可选字段。
NVIDIA Copyright Header
NVIDIA版权头
Add to all new Python files and shell scripts (not test files). Use the current year.
python
undefined所有新的Python文件和Shell脚本(测试文件除外)都需添加。使用当前年份。
python
undefinedCopyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
undefinedundefinedCode Review Checklist
代码审查检查清单
- Copyright header present on new Python files (not test files)
- Type hints on public functions and methods
- Docstrings on public classes and functions (Google style)
- Specific exceptions in try-except blocks
- No bare — use
print()orloggerprint_rank_0 - No hidden defaults in config function parameters
- Keyword-only args for ambiguous same-type parameters
- Double quotes for strings
- Import order follows the 5-group convention
- No commented-out code without explanation
- Mypy clean — no untyped defs, no in public APIs, no bare
Any# type: ignore
- 版权头:新Python文件(测试文件除外)需包含版权头
- 类型提示:公共函数和方法需添加类型提示
- 文档字符串:公共类和函数需添加Google风格的文档字符串
- 具体异常:try-except块中使用具体异常类型
- 禁止裸:使用
print()或loggerprint_rank_0 - 无隐藏默认值:配置函数参数中无隐藏默认值
- 仅关键字参数:模糊的同类型参数使用仅关键字参数
- 双引号:字符串使用双引号
- 导入顺序:遵循5组约定
- 无无注释的注释代码:无注释说明的注释代码需删除
- Mypy检查通过:无未类型化定义、公共API中无类型、无裸
Any# type: ignore