We have recommended a set of static code analysis python rules, primarily targeting repositories with Python code. For details on the checks performed by each rule and troubleshooting guidance, please refer to the table below.
Rule | Description | Debug | Notes | Ignore Example |
---|---|---|---|---|
black | Python code formatter | single-line format:# fmt: skip |
||
flake8 | Check the style and quality of some python code | single-line format:# noqa: F401 command args: --ignore=F401 |
||
isort | Python utility / library to sort imports | Sort rules: sort imports alphabetically and automatically separate into sections and by type | single-line format:# isort:skip |
|
codespell | Check code for common misspellings | single-line format:# codespell:ignore <words> command args: --skip=doc/en/usage.md |
||
mdformat | CommonMark compliant Markdown formatter | |||
pymarkdown | Markdown linter | You can use args: [fix] to fix errors automatically. |
There will be a conflict between mdformat and pymarkdown when an empty YAML file exists. |
single-line format:(Must be at line start) <!-- pyml disable-next-line --> command args: - entry: "pymarkdown -d MD013,MD041,MD010" |
pylint | Python code analysis tool | Python 3.12 unsupported | single-line format:# pylint: disable=W0401,W0614 command args: --disable=E0402,E0401 |
|
gitleaks | Detect secrets (passwords, API keys, tokens) in git repos/files | If you configure .gitleaks.toml, then the demo configuration in .pre-commit-config.yaml would be as follows:- repo: https://github.com/gitleaks/gitleaks |
single-line format: # gitleaks:allow (Though offical example is gitleaks,this way can avoid of confliction with flake8)configure way:Use disabledRules = [ "generic-api-key"] in .gitleaks.toml |
|
markdown-link-check | checks all of the hyperlinks in a markdown text to determine if they are alive or dead | Current version mistake /A/B format as an error. |
single-line format:single-line format: command args: -i http://example.net |
-
Install pre-commit
pip install pre-commit
For detailed installation instructions, refer to the pre-commit -
Configuration
-
.pre-commit-config.yaml already exists
Select hooks not already in use by your project and add them to your .pre-commit-config.yaml file
-
if not
Copy the configuration file to your project root:
cp python/pre-commit-config.yaml <your-project-root>/.pre-commit-config.yaml
-
-
Install the git hook scripts
pre-commit install
-
Check Code
- run all hooks:
pre-commit run --all-files
- run one hook:
pre-commit run <hook id> --all-files
- run all hooks:
Our configuration incorporates examples from lmdeploy .