python data_error ai_generated true

ValueError: mypkg.mod 的文档字符串第 1 行示例后缺少空行

ValueError: line 1 of the docstring for mypkg.mod lacks blank line after the examples

ID: python/pytest-doctest-module-not-found

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
0证据数
2025-02-08首次发现

版本兼容性

版本状态引入弃用备注
7.x active — — —
8.x active — — —

根因分析

pytest --doctest-modules 将文档字符串解析为 doctest;'>>>' 块后缺少空行或缩进错误会在收集阶段触发此 ValueError。

English

pytest --doctest-modules parses docstrings as doctests; missing blank line after '>>>' block or malformed indentation triggers this ValueError during collection.

generic

解决方案

  1. 94% 成功率
    def add(a, b):
        """Return a + b.
    
        >>> add(1, 2)
        3
    
        """
        return a + b
  2. 88% 成功率
    # pyproject.toml
    [tool.pytest.ini_options]
    addopts = "--doctest-modules"
    doctest_optionflags = ["NORMALIZE_WHITESPACE", "ELLIPSIS"]

无效尝试

常见但无效的做法:

  1. 70% 失败

    Skips legitimate doctests and hides the formatting bug; other modules with the same issue still fail.

  2. 75% 失败

    Loses documentation and breaks help() output; doesn't address the formatting pattern for future modules.