python data_error ai_generated true

断言错误:断言nan等于1.0

AssertionError: assert nan == 1.0

ID: python/pytest-approx-nan

其他格式: JSON · Markdown 中文 · English
80%修复率
81%置信度
0证据数
2025-11-05首次发现

版本兼容性

版本状态引入弃用备注
3.6 active

根因分析

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

English

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

generic

解决方案

  1. 85% 成功率
    import math
    if math.isnan(actual):
        pytest.fail('Actual value is NaN')
    else:
        assert actual == pytest.approx(expected)
  2. 90% 成功率
    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)

无效尝试

常见但无效的做法:

  1. 60% 失败

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

  2. 90% 失败

    可能掩盖计算错误。