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: 的提示。
  • 在真实项目中使用默认降噪策略,同时保留严格模式。

安装

从 PyPI 安装:

pip install python-docstring-checker

升级到最新版本:

pip install -U python-docstring-checker

如需安装仓库中的最新代码:

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

未传文件或目录时,默认扫描当前目录。也可以在配置中用 include 指定默认扫描路径。

默认输出为适合阅读的 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-docstring-checker --version

显示源码上下文:

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

指定默认扫描路径,仅在未传位置路径时生效:

python -m python_docstring_checker --include src --include tests

最小配置

pyproject.toml 中添加:

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

命令行参数优先级高于 pyproject.toml。传入位置路径时会忽略 include;未传位置路径时,配置中的 include 会和 CLI 的 --include 追加合并。列表型选项会在默认值和配置值基础上继续追加命令行参数。

文档

当前约束

  • 目标 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.2.tar.gz (26.4 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.2-py3-none-any.whl (21.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_docstring_checker-0.0.2.tar.gz
  • Upload date:
  • Size: 26.4 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.2.tar.gz
Algorithm Hash digest
SHA256 3a22f4f6d78ac44ebe8c1235209da0fb304f92ce696b95350d3d1a618d756172
MD5 4c971f3267c524bc1026c3b6600819bc
BLAKE2b-256 0b50a6e01b6ab698bba678c87a2e5fe97f3cd40f500321016bdb6495a5e5438a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_docstring_checker-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5c2ee94821e6897aaef67502a169d7d2c86e2e2e47359eea2d8c98e88463f14c
MD5 c3d9b83be40a0fa11e6558b524ab725c
BLAKE2b-256 9418b4d75f4ef7a478486ec171836f7f2475c16d90a82a4c5e7fd6c8c6c0147e

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