mcore-linting-and-formatting

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Linting and Formatting

代码检查与格式化



Running the Formatter

运行格式化工具

Run before opening a PR:
bash
undefined
在提交PR前运行以下命令:
bash
undefined

Check mode (no changes applied)

检查模式(不应用任何修改)

BASE_REF=main CHECK_ONLY=true SKIP_DOCS=false bash tools/autoformat.sh
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`.

---
BASE_REF=main CHECK_ONLY=false bash tools/autoformat.sh

调用的工具:`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

在编辑任何Python文件的导入语句后,提交前务必对这些文件运行
uv run isort
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.

在容器内执行:
bash
uv sync --locked --only-group linting
这会安装
ruff
,
black
,
isort
,
pylint
—— 与
tools/autoformat.sh
及CI的
linting
任务使用的工具一致。

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.
  • 类型提示:所有公共API函数必须添加类型提示。使用
    X | None
    ,而非
    Optional[X]
  • 文档字符串:所有公共类和函数采用Google风格的文档字符串。
  • 命名规范:遵循Python约定 —— 函数和变量使用
    snake_case
    ,类使用
    PascalCase
  • 行长度:119个字符(在
    pyproject.toml
    中配置)。
  • 禁止裸
    except
    :始终捕获特定的异常类型。