python data_error ai_generated true

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-09-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active — — —
8.x active — — —

Root Cause

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

中文

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

Workarounds

  1. 85% success
    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% success
    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)

Dead Ends

Common approaches that don't work:

  1. 85% fail

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

  2. 90% fail

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