{"id":"326a947ddbbead2b","repo":"pytest-dev/pytest","slug":"pytest-approx-does-not-support-relative-toleranc","errorCode":null,"errorMessage":"pytest.approx() does not support relative tolerance for datetime comparisons. Use abs=timedelta(...) instead.","messagePattern":"pytest\\.approx\\(\\) does not support relative tolerance for datetime comparisons\\. Use abs=timedelta\\(\\.\\.\\.\\) instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/approx.py","lineNumber":647,"sourceCode":"\nclass ApproxTimedelta(Approx[datetime | timedelta]):\n    \"\"\"Perform approximate comparisons where the expected value is a\n    datetime or timedelta.\n\n    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 \"","sourceCodeStart":629,"sourceCodeEnd":665,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/approx.py#L629-L665","documentation":"ApproxTimedelta.__init__ rejects relative tolerance for datetime (not timedelta) comparisons because a relative fraction of an absolute timestamp is not meaningful (and is timezone/epoch dependent). Use an absolute timedelta tolerance instead.","triggerScenarios":"Call pytest.approx(some_datetime, rel=0.01) — i.e. the expected value is a datetime instance and rel is not None.","commonSituations":"Reusing a numeric-style approx(expected, rel=0.05) call for datetime values; refactoring a timedelta test to use datetime without updating the tolerance; copy-pasting tolerance kwargs between tests.","solutions":["Switch to an absolute tolerance: approx(dt, abs=timedelta(seconds=1)).","Pick the timedelta magnitude that matches the precision you actually need (microseconds, seconds, days).","If you truly need a proportional tolerance, convert to timedelta first and compare timedeltas."],"exampleFix":"// before\nassert got == approx(datetime(2024,1,1,12,0,0), rel=0.01)\n// after\nfrom datetime import timedelta\nassert got == approx(datetime(2024,1,1,12,0,0), abs=timedelta(seconds=1))","handlingStrategy":"type-guard","validationCode":"from datetime import datetime, timedelta\n\ndef approx_dt(value, abs_seconds=None):\n    if isinstance(value, datetime):\n        if abs_seconds is None:\n            raise ValueError('datetime approx requires abs_seconds')\n        return pytest.approx(value, abs=timedelta(seconds=abs_seconds))\n    return pytest.approx(value)","typeGuard":"from datetime import datetime, timedelta\n\ndef is_datetime(v) -> bool:\n    return isinstance(v, datetime)","tryCatchPattern":null,"preventionTips":["Never reuse numeric-style rel= tolerances for datetime values.","Wrap approx() for datetime in a project helper that always uses timedelta tolerances."],"tags":["pytest","approx","datetime"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}