python data_error ai_generated true

AssertionError: assert nan == 1.0

ID: python/pytest-approx-nan

Also available as: JSON · Markdown · 中文
80%Fix Rate
81%Confidence
0Evidence
2025-11-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.6 active

Root Cause

使用pytest.approx比较时,实际值为NaN,导致比较失败。

generic

中文

pytest.approx比较时,实际值为NaN,导致失败。

Workarounds

  1. 85% success
    import math
    if math.isnan(actual):
        pytest.fail('Actual value is NaN')
    else:
        assert actual == pytest.approx(expected)
  2. 90% success
    def assert_float_equal(actual, expected):
        if math.isnan(actual) or math.isnan(expected):
            assert math.isnan(actual) == math.isnan(expected)
        else:
            assert actual == pytest.approx(expected)

Dead Ends

Common approaches that don't work:

  1. 60% fail

    pytest.approx不支持NaN,需单独处理。

  2. 90% fail

    可能掩盖计算错误。