python-code-quality
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePython Code Quality
Python代码质量
Quick reference for Python code quality tools: ruff (linting & formatting), mypy (type checking).
Python代码质量工具快速参考:ruff(代码检查与格式化)、mypy(类型检查)。
When This Skill Applies
本技能适用场景
- Linting Python code
- Code formatting
- Type checking
- Pre-commit hooks
- CI/CD quality gates
- Code style enforcement
- Python代码检查
- 代码格式化
- 类型检查
- Pre-commit钩子
- CI/CD质量门禁
- 代码风格强制执行
Quick Reference
快速参考
Ruff (Linter & Formatter)
Ruff(代码检查器与格式化工具)
bash
undefinedbash
undefinedLint code
检查代码
uv run ruff check .
uv run ruff check .
Auto-fix issues
自动修复问题
uv run ruff check --fix .
uv run ruff check --fix .
Format code
格式化代码
uv run ruff format .
uv run ruff format .
Check and format
检查并格式化
uv run ruff check --fix . && uv run ruff format .
uv run ruff check --fix . && uv run ruff format .
Show specific rule
查看特定规则
uv run ruff check --select E501 # Line too long
uv run ruff check --select E501 # 行过长
Ignore specific rule
忽略特定规则
uv run ruff check --ignore E501
undefineduv run ruff check --ignore E501
undefinedMypy (Type Checking)
Mypy(类型检查工具)
bash
undefinedbash
undefinedType check project
对项目进行类型检查
uv run mypy .
uv run mypy .
Type check specific file
对特定文件进行类型检查
uv run mypy src/module.py
uv run mypy src/module.py
Strict mode
严格模式
uv run mypy --strict .
uv run mypy --strict .
Show error codes
显示错误代码
uv run mypy --show-error-codes .
undefineduv run mypy --show-error-codes .
undefinedpyproject.toml Configuration
pyproject.toml配置
toml
[tool.ruff]
line-length = 88
target-version = "py311"
[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"N", # pep8-naming
"UP", # pyupgrade
"B", # flake8-bugbear
]
ignore = [
"E501", # line too long (handled by formatter)
]
[tool.ruff.lint.isort]
known-first-party = ["myproject"]
[tool.mypy]
python_version = "3.11"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true
exclude = ["tests"]toml
[tool.ruff]
line-length = 88
target-version = "py311"
[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"N", # pep8-naming
"UP", # pyupgrade
"B", # flake8-bugbear
]
ignore = [
"E501", # line too long (handled by formatter)
]
[tool.ruff.lint.isort]
known-first-party = ["myproject"]
[tool.mypy]
python_version = "3.11"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true
exclude = ["tests"]Type Hints
类型提示
python
undefinedpython
undefinedModern type hints (Python 3.10+)
现代类型提示(Python 3.10+)
def process_data(
items: list[str], # Not List[str]
config: dict[str, int], # Not Dict[str, int]
optional: str | None = None, # Not Optional[str]
) -> tuple[bool, str]: # Not Tuple[bool, str]
return True, "success"
def process_data(
items: list[str], # Not List[str]
config: dict[str, int], # Not Dict[str, int]
optional: str | None = None, # Not Optional[str]
) -> tuple[bool, str]: # Not Tuple[bool, str]
return True, "success"
Type aliases
Type aliases
from typing import TypeAlias
UserId: TypeAlias = int
UserDict: TypeAlias = dict[str, str | int]
def get_user(user_id: UserId) -> UserDict:
return {"id": user_id, "name": "Alice"}
undefinedfrom typing import TypeAlias
UserId: TypeAlias = int
UserDict: TypeAlias = dict[str, str | int]
def get_user(user_id: UserId) -> UserDict:
return {"id": user_id, "name": "Alice"}
undefinedPre-commit Configuration
Pre-commit配置
yaml
undefinedyaml
undefined.pre-commit-config.yaml
.pre-commit-config.yaml
repos:
-
repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.1.9 hooks:
- id: ruff args: [--fix]
- id: ruff-format
-
repo: https://github.com/pre-commit/mirrors-mypy rev: v1.7.1 hooks:
- id: mypy additional_dependencies: [types-requests]
undefinedrepos:
-
repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.1.9 hooks:
- id: ruff args: [--fix]
- id: ruff-format
-
repo: https://github.com/pre-commit/mirrors-mypy rev: v1.7.1 hooks:
- id: mypy additional_dependencies: [types-requests]
undefinedCommon Ruff Rules
常见Ruff规则
- E501: Line too long
- F401: Unused import
- F841: Unused variable
- I001: Import not sorted
- N806: Variable should be lowercase
- B008: Function call in argument defaults
- E501: 行过长
- F401: 未使用的导入
- F841: 未使用的变量
- I001: 导入未排序
- N806: 变量应小写
- B008: 参数默认值中的函数调用
See Also
另请参阅
- - Testing code quality
python-testing - - Adding quality tools to projects
uv-project-management - - Core Python patterns
python-development
- - 测试代码质量
python-testing - - 为项目添加质量工具
uv-project-management - - 核心Python模式
python-development
References
参考资料
- Ruff: https://docs.astral.sh/ruff/
- Mypy: https://mypy.readthedocs.io/
- Detailed guide: See REFERENCE.md
- Ruff: https://docs.astral.sh/ruff/
- Mypy: https://mypy.readthedocs.io/
- 详细指南:See REFERENCE.md