All submitted code must pass the local pre-commit checks. This document describes how to use pre-commit and the rules of each check.
Perform the following steps to ensure that checks run automatically on every git commit.
pip install "pre-commit>=4.0.0"Python 3.10 is recommended.
Run the following command once in the repository root directory:
pre-commit installAfter that, each git commit automatically triggers the checks.
First run git add to stage the files to be committed, and then run the following command:
pre-commit runThis command checks only the staged files, which is consistent with the scope automatically triggered by git commit. If the check fails, fix the issues according to the prompts, run git add again, and then rerun the command.
To check only specified files that have not been staged, run the following command:
pre-commit run --files <file path>git commit -m "your message"If the check passes, the commit succeeds; if it fails, the commit is blocked.
The following table lists the content checked by pre-commit. If a check fails, fix the issue according to the prompt.
| Check Item (Hook Name) | Purpose |
|---|---|
| trailing-whitespace | Removes trailing whitespace. |
| end-of-file-fixer | Ensures a newline at the end of the file. |
| check-yaml | Validates YAML syntax. |
| check-json | Validates JSON syntax. |
| check-added-large-files | Prevents committing oversized files. |
| check-merge-conflict | Detects unresolved merge conflict markers. |
| detect-private-key | Detects private keys or sensitive credential leaks. |
| codespell | Checks spelling (for comments, documentation, etc.). |
| Check Item | Purpose |
|---|---|
| ruff-check / ruff-format | Checks code style check and automatic formatting (line width 120). |
| pylint | Checks logic defects such as undefined variables and incorrect imports. |
| bandit | Checks security vulnerability scanning (such as hardcoded passwords and dangerous functions). |
| typos | Checks spelling (covers all source files, including Python code). |
When a check tool falsely reports a word as a spelling error, you can add it to the whitelist.
Note
Whitelist changes must be submitted with the PR, and the reason must be explained in the PR description.
-
typos whitelist: edit
pre-commit/typos.toml -
codespell whitelist: in
.pre-commit-config.yaml, locate-Lof the codespell hook and append the falsely reported word
Run git add again on the modified files, and then run git commit.
This occurs because pymdownx uses non-standard tags such as !!python/name. You can temporarily skip the YAML check (do not use --no-verify to skip all checks):
SKIP=check-yaml git commit -m "your message"Handle them according to the "Whitelist Configuration" section by adding the words to the corresponding whitelist.
pre-commit downloads the environment for each hook (such as Ruff and Pylint), and subsequent runs use the cache.
After installing hooks for the first time, or when you need to troubleshoot legacy issues, you can run pre-commit run --all-files to scan the entire repository. A full check is not required for routine commits.