python data_error ai_generated true

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

ID: python/pytest-doctest-output-mismatch

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-06-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active — — —
3.10 active — — —
3.12 active — — —

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 85% fail

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

  2. 90% fail

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