{"id":"6d4d170f628221b6","repo":"pytest-dev/pytest","slug":"pytest-approx-does-not-support-nan-ok-for-dateti","errorCode":null,"errorMessage":"pytest.approx() does not support nan_ok for datetime/timedelta comparisons.","messagePattern":"pytest\\.approx\\(\\) does not support nan_ok for datetime/timedelta comparisons\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/approx.py","lineNumber":652,"sourceCode":"    Requires an explicit tolerance as a timedelta for abs, or a float for rel.\n    Relative tolerance is not supported for datetime comparisons.\n    \"\"\"\n\n    def __init__(\n        self,\n        expected: datetime | timedelta,\n        rel: float | Decimal | timedelta | None,\n        abs: float | Decimal | timedelta | None,\n        nan_ok: bool,\n    ) -> None:\n        __tracebackhide__ = True\n        if isinstance(expected, datetime) and rel is not None:\n            raise TypeError(\n                \"pytest.approx() does not support relative tolerance for \"\n                \"datetime comparisons. Use abs=timedelta(...) instead.\"\n            )\n        if nan_ok:\n            raise TypeError(\n                \"pytest.approx() does not support nan_ok for \"\n                \"datetime/timedelta comparisons.\"\n            )\n        if abs is None and rel is None:\n            raise TypeError(\n                \"pytest.approx() requires an explicit tolerance for \"\n                \"datetime/timedelta comparisons: \"\n                \"e.g. approx(expected, abs=timedelta(seconds=1)) \"\n                \"or approx(expected, rel=0.01)\"\n            )\n        if abs is not None and not isinstance(abs, timedelta):\n            raise TypeError(\n                f\"absolute tolerance for datetime/timedelta must be a \"\n                f\"timedelta, got {type(abs).__name__}\"\n            )\n        if abs is not None and abs < timedelta(0):\n            raise ValueError(f\"absolute tolerance can't be negative: {abs}\")\n        if rel is not None:","sourceCodeStart":634,"sourceCodeEnd":670,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/approx.py#L634-L670","documentation":"ApproxTimedelta.__init__ rejects nan_ok=True for datetime/timedelta comparisons. NaN datetimes/timedeltas are not a supported concept, so allowing them would be meaningless.","triggerScenarios":"Call pytest.approx(dt_or_timedelta, abs=timedelta(seconds=1), nan_ok=True) (or with rel= for a timedelta).","commonSituations":"Copy-pasting a numeric approx() call (that used nan_ok=True) when migrating to datetime/timedelta values; blanket defaulting nan_ok=True across a parametrized test matrix.","solutions":["Drop the nan_ok=True argument for datetime/timedelta comparisons.","If you genuinely need NaN handling, you are comparing the wrong type — restructure the test."],"exampleFix":"// before\nassert got == approx(dt, abs=timedelta(seconds=1), nan_ok=True)\n// after\nassert got == approx(dt, abs=timedelta(seconds=1))","handlingStrategy":"validation","validationCode":"def approx_dt(value, abs_td):\n    return pytest.approx(value, abs=abs_td, nan_ok=False)  # never pass nan_ok for datetime","typeGuard":"from datetime import datetime, timedelta\n\ndef is_datetime_like(v) -> bool:\n    return isinstance(v, (datetime, timedelta))","tryCatchPattern":null,"preventionTips":["Do not blanket-default nan_ok=True across parametrized tests; set it per-type.","Audit copy-pasted approx() calls when changing the expected value's type."],"tags":["pytest","approx","datetime"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}