python
data_error
ai_generated
true
AssertionError: assert nan == 1.0
ID: python/pytest-approx-nan
80%Fix Rate
81%Confidence
0Evidence
2025-11-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.6 | active | — | — | — |
Root Cause
使用pytest.approx比较时,实际值为NaN,导致比较失败。
generic中文
pytest.approx比较时,实际值为NaN,导致失败。
Workarounds
-
85% success
import math if math.isnan(actual): pytest.fail('Actual value is NaN') else: assert actual == pytest.approx(expected) -
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:
-
60% fail
pytest.approx不支持NaN,需单独处理。
-
90% fail
可能掩盖计算错误。