python data_error ai_generated true

AssertionError: assert {'a': 1} == {'a': 1} 省略 1 个相同项,使用 -vv 显示 完整差异: - {'a': 1} + {'a': 1} ? ^ ^ + {'a': 1} ? ^ ^

AssertionError: assert {'a': 1} == {'a': 1} Omitting 1 identical items, use -vv to show Full diff: - {'a': 1} + {'a': 1} ? ^ ^ + {'a': 1} ? ^ ^

ID: python/pytest-assertion-rewrite-failed

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

版本兼容性

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

根因分析

断言比较的对象在 repr 中看似相等,但在类型或嵌套标识上不同(如 int 与 bool、numpy 数组与 list,或自定义 __eq__ 返回非布尔值)。pytest 的断言重写隐藏了真实差异。

English

The assertion compares objects that look equal in repr but differ in type or nested identity (e.g., int vs bool, numpy array vs list, or a custom __eq__ that returns a non-bool). pytest's assertion rewriting hides the real difference.

generic

解决方案

  1. 85% 成功率
    Print the types and use a type-strict comparison:
    
    def test_x():
        actual = get()
        assert type(actual) is dict and actual == {'a': 1}, f"{type(actual)=} {actual=}"
  2. 92% 成功率
    For numpy/pandas, use the dedicated assertion helpers:
    
    import numpy as np
    np.testing.assert_array_equal(actual, expected)
    import pandas as pd
    pd.testing.assert_frame_equal(df1, df2)

无效尝试

常见但无效的做法:

  1. 85% 失败

    Identity comparison almost always fails for equal-but-distinct objects and produces a more confusing error.

  2. 90% 失败

    Only changes the error formatting; the underlying inequality is still there.