python data_error ai_generated true

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

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

ID: python/pytest-doctest-output-mismatch

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

版本兼容性

版本状态引入弃用备注
3.8 active — — —
3.10 active — — —
3.12 active — — —

根因分析

Doctest 比较精确的字符串输出。Python 3.7+ 中字典按插入顺序排列,因此期望输出必须与实际插入顺序一致,或使用排序输出。

English

Doctests compare exact string output. Dict ordering in Python 3.7+ is insertion order, so the expected output must match the actual insertion order, or use sorted output.

generic

解决方案

  1. 95% 成功率
    >>> print(dict(sorted({'b': 2, 'a': 1}.items())))
    {'a': 1, 'b': 2}
  2. 80% 成功率
    >>> print({'a': 1, 'b': 2})  # doctest: +ELLIPSIS
    {...}

无效尝试

常见但无效的做法:

  1. 85% 失败

    Skips the test entirely, losing coverage for the function's behavior.

  2. 90% 失败

    Hash seed affects set ordering, not dict insertion order in modern Python; dicts preserve insertion order regardless.