Skip to main content

AST-based checker for Google-style Python docstrings.

Project description

python-docstring-checker

[!WARNING]
这是一个 Python Google 风格 docstring 检查器的实验项目,用于验证和落地相关检查规则,并非官方 Google docstring 规范或官方工具。

python-docstring-checker 是一个基于标准库 ast 的 Python Google 风格 docstring 检查器。它不导入、不执行被检查代码,只静态解析源码,并检查函数参数、返回值、生成器、属性文档和类型注解是否一致。

适合用于:

  • 在本地或 CI 中检查 Google 风格 docstring 是否和真实签名同步。
  • 发现参数漏写、多写、改名、类型漂移,以及 docstring 有类型但签名缺少注解的问题。
  • 发现返回值缺少 Returns:、生成器误用 Returns:、明确抛出的异常缺少 Raises:、属性文档类型不一致。
  • 识别把返回值类型误写进 Args: 的常见错误,并给出迁移到 Returns: / Yields: 的提示。
  • 在真实项目中使用默认降噪策略,同时保留严格模式。

安装

从 GitHub 安装:

pip install git+https://github.com/licyk/python-docstring-checker.git

指定分支或提交安装:

pip install git+https://github.com/licyk/python-docstring-checker.git@main

本地开发安装:

git clone https://github.com/licyk/python-docstring-checker.git
cd python-docstring-checker
pip install -e .[dev]

安装后可使用脚本入口:

python-docstring-checker path/to/file.py

也可以直接使用模块入口:

python -m python_docstring_checker path/to/file.py

快速开始

扫描单个文件:

python -m python_docstring_checker examples/sd_webui_all_in_one/downloader/aria2_server.py

扫描目录:

python -m python_docstring_checker src tests

默认输出为适合阅读的 text 格式,包含摘要和按文件分组的问题列表。发现问题时退出码为 1,没有问题时退出码为 0

示例输出:

Docstring Check Report
Total issues: 1
Files affected: 1
By code: ARG003=1
By confidence: high=1

examples/sd_webui_all_in_one/downloader/aria2_server.py
  L101: ARG003 [high] Aria2RpcServer.__init__
    Parameter 'log_file' type mismatch: docstring has 'Path | None', signature has 'Path | str | None'.

常用命令

显示源码上下文:

python -m python_docstring_checker --show-source --source-context 2 src

输出 JSON:

python -m python_docstring_checker --format json src

输出一行一个 JSON,适合日志采集:

python -m python_docstring_checker --format json-lines src

使用旧式紧凑输出,适合 grep 或简单脚本:

python -m python_docstring_checker --format compact src

切换检查策略:

python -m python_docstring_checker --strictness strict src
python -m python_docstring_checker --strictness public src

指定配置文件:

python -m python_docstring_checker --config pyproject.toml src

最小配置

pyproject.toml 中添加:

[tool.python-docstring-checker]
strictness = "balanced"
format = "text"
show-source = false
source-context = 1
exclude = ["build/*", "dist/*"]
ignore-codes = []

命令行参数优先级高于 pyproject.toml。列表型选项会在默认值和配置值基础上继续追加命令行参数。

文档

当前约束

  • 目标 Python 版本为 3.10+
  • 第一版只使用标准库实现,不引入运行时代码执行。
  • Google 风格解析以 Args: / Returns: / Yields: / Raises: / Attributes: 为主要格式。
  • 检查器只覆盖模块、类、函数、方法、属性等结构,不检查函数内部局部变量的文档。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_docstring_checker-0.0.1.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_docstring_checker-0.0.1-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

Details for the file python_docstring_checker-0.0.1.tar.gz.

File metadata

  • Download URL: python_docstring_checker-0.0.1.tar.gz
  • Upload date:
  • Size: 25.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for python_docstring_checker-0.0.1.tar.gz
Algorithm Hash digest
SHA256 65b9d3181a13978f8d5d5528cc25363ccba162820454c96661c722b7ae5a85b0
MD5 1d44a722d9a9e25dd8692cbda80b5c5c
BLAKE2b-256 cba84e4ab07bf25dce0f73598561edbbc9d239b6960a247f6d393b9c0673bbbb

See more details on using hashes here.

File details

Details for the file python_docstring_checker-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_docstring_checker-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bbf3e63a4faaa5996192cc49ea180363f17ac65d0227a3c911773cf1389b71ae
MD5 a2727b7f278b8db294c3b50c9c96f4b5
BLAKE2b-256 1c9ae60fa3d40b027c4b34fdc866639c159e0a2bf89ae31c9f5bf9ba9e7e6a17

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page