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
80%Fix Rate
88%Confidence
0Evidence
2024-06-18First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
95% success
>>> print(dict(sorted({'b': 2, 'a': 1}.items()))) {'a': 1, 'b': 2} -
80% success
>>> print({'a': 1, 'b': 2}) # doctest: +ELLIPSIS {...}
Dead Ends
Common approaches that don't work:
-
85% fail
Skips the test entirely, losing coverage for the function's behavior.
-
90% fail
Hash seed affects set ordering, not dict insertion order in modern Python; dicts preserve insertion order regardless.