python-code-quality

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Python 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
undefined
bash
undefined

Lint 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
undefined
uv run ruff check --ignore E501
undefined

Mypy (Type Checking)

Mypy(类型检查工具)

bash
undefined
bash
undefined

Type 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 .
undefined
uv run mypy --show-error-codes .
undefined

pyproject.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
undefined
python
undefined

Modern 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"}
undefined
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"}
undefined

Pre-commit Configuration

Pre-commit配置

yaml
undefined
yaml
undefined

.pre-commit-config.yaml

.pre-commit-config.yaml

repos:
undefined
repos:
undefined

Common 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

另请参阅

  • python-testing
    - Testing code quality
  • uv-project-management
    - Adding quality tools to projects
  • python-development
    - Core Python patterns
  • python-testing
    - 测试代码质量
  • uv-project-management
    - 为项目添加质量工具
  • python-development
    - 核心Python模式

References

参考资料