{"id":"5e7b055f34604a5d","repo":"pytest-dev/pytest","slug":"pytest-approx-requires-an-explicit-tolerance-for","errorCode":null,"errorMessage":"pytest.approx() requires an explicit tolerance for datetime/timedelta comparisons: e.g. approx(expected, abs=timedelta(seconds=1)) or approx(expected, rel=0.01)","messagePattern":"pytest\\.approx\\(\\) requires an explicit tolerance for datetime/timedelta comparisons: e\\.g\\. approx\\(expected, abs=timedelta\\(seconds=1\\)\\) or approx\\(expected, rel=0\\.01\\)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/approx.py","lineNumber":657,"sourceCode":"        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:\n            if not isinstance(rel, (int, float)):\n                raise TypeError(\n                    f\"relative tolerance for timedelta must be a \"\n                    f\"number, got {type(rel).__name__}\"\n                )","sourceCodeStart":639,"sourceCodeEnd":675,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/approx.py#L639-L675","documentation":"ApproxTimedelta.__init__ requires an explicit tolerance because there is no sensible default for datetime/timedelta comparisons (unlike the 1e-12 default for floats). You must pass either abs=timedelta(...) or rel=<float>.","triggerScenarios":"Call pytest.approx(some_datetime) or pytest.approx(some_timedelta) with neither abs nor rel supplied.","commonSituations":"Assuming approx() has the same default-tolerance behavior for datetimes as for floats; quick one-liner tests written without reading the datetime docs.","solutions":["Pass an absolute tolerance: approx(dt, abs=timedelta(seconds=1)).","Or, for timedelta expected values, pass a relative tolerance: approx(td, rel=0.01).","Choose a tolerance magnitude that reflects the precision you actually care about."],"exampleFix":"// before\nassert got == approx(datetime(2024,1,1,12,0,0))\n// after\nfrom datetime import timedelta\nassert got == approx(datetime(2024,1,1,12,0,0), abs=timedelta(seconds=1))","handlingStrategy":"validation","validationCode":"from datetime import datetime, timedelta\n\ndef approx_dt(value, *, abs_seconds=1.0):\n    if isinstance(value, (datetime, timedelta)):\n        return pytest.approx(value, abs=timedelta(seconds=abs_seconds))\n    return pytest.approx(value)","typeGuard":"from datetime import datetime, timedelta\n\ndef needs_explicit_tol(v) -> bool:\n    return isinstance(v, (datetime, timedelta))","tryCatchPattern":null,"preventionTips":["Provide a project helper for datetime approximations so the tolerance can never be forgotten.","Document the helper so contributors do not call bare approx() on datetimes."],"tags":["pytest","approx","datetime"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}