python config_error ai_generated true

失败示例: print({'a': 1, 'b': 2}) 期望: {'a': 1, 'b': 2} 实际: {'a': 1, 'b': 2} 1 项存在失败

Failed example: print({'a': 1, 'b': 2}) Expected: {'a': 1, 'b': 2} Got: {'a': 1, 'b': 2} 1 items had failures

ID: python/pytest-doctest-optionflags-mismatch

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

版本兼容性

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

根因分析

字典顺序在不同 Python 版本间不同,或输出存在尾随空白/顺序差异。未启用 NORMALIZE_WHITESPACE 或 ELLIPSIS 时,doctest 要求输出逐字节一致。

English

Dict ordering differs across Python versions or the output has trailing whitespace/ordering that differs. Without NORMALIZE_WHITESPACE or ELLIPSIS, doctest requires byte-exact output.

generic

解决方案

  1. 90% 成功率
    Enable normalization in pytest.ini:
      [tool.pytest.ini_options]
      doctest_optionflags = NORMALIZE_WHITESPACE ELLIPSIS
    Or in docstring:
      >>> print(d)  # doctest: +NORMALIZE_WHITESPACE
  2. 92% 成功率
    Sort dict output explicitly in the docstring example:
      >>> sorted(d.items())
      [('a', 1), ('b', 2)]

无效尝试

常见但无效的做法:

  1. 75% 失败

    Breaks on other Python versions where dict ordering or repr differ.

  2. 50% 失败

    Loses documentation test coverage; hides real regressions.