{"id":"405375e8bb014fbd","repo":"pytest-dev/pytest","slug":"absolute-tolerance-can-t-be-negative-absolute-to","errorCode":null,"errorMessage":"absolute tolerance can't be negative: {absolute_tolerance}","messagePattern":"absolute tolerance can't be negative: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/approx.py","lineNumber":561,"sourceCode":"        return result\n\n    __hash__ = None\n\n    @property\n    def tolerance(self):\n        \"\"\"Return the tolerance for the comparison.\n\n        This could be either an absolute tolerance or a relative tolerance,\n        depending on what the user specified or which would be larger.\n        \"\"\"\n        # Figure out what the absolute tolerance should be.  ``self.abs`` is\n        # either None or a value specified by the user.\n        absolute_tolerance = (\n            self.abs if self.abs is not None else self.DEFAULT_ABSOLUTE_TOLERANCE\n        )\n\n        if absolute_tolerance < 0:\n            raise ValueError(\n                f\"absolute tolerance can't be negative: {absolute_tolerance}\"\n            )\n        if math.isnan(absolute_tolerance):\n            raise ValueError(\"absolute tolerance can't be NaN.\")\n\n        # If the user specified an absolute tolerance but not a relative one,\n        # just return the absolute tolerance.\n        if self.rel is None:\n            if self.abs is not None:\n                return absolute_tolerance\n\n        # Figure out what the relative tolerance should be.  ``self.rel`` is\n        # either None or a value specified by the user.  This is done after\n        # we've made sure the user didn't ask for an absolute tolerance only,\n        # because we don't want to raise errors about the relative tolerance if\n        # we aren't even going to use it.\n        rel = self.rel if self.rel is not None else self.DEFAULT_RELATIVE_TOLERANCE\n        # expected is SupportAbs, checked in __init__.","sourceCodeStart":543,"sourceCodeEnd":579,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/approx.py#L543-L579","documentation":"The absolute tolerance for ApproxScalar cannot be negative, since a negative tolerance would make no values match (abs(expected - actual) is always >= 0). The tolerance property checks absolute_tolerance < 0 and raises ValueError if so. This is a runtime validation, not an __init__ check, so it fires when the first comparison evaluates the tolerance.","triggerScenarios":"Calling approx(1.0, abs=-0.001) and then performing a comparison (assert 1.0 == approx(1.0, abs=-0.001)). The tolerance property computes absolute_tolerance, finds it negative, and raises ValueError.","commonSituations":"Computed tolerance values with a sign error, a config typo, or subtracting instead of taking absolute value when building the tolerance.","solutions":["Use a non-negative absolute tolerance: approx(1.0, abs=0.001).","Fix the computation that produces the tolerance to use abs(): abs_tol = abs(computed_value).","Review the error message to see the offending negative value and trace its source."],"exampleFix":"# before\napprox(1.0, abs=-0.001)\n\n# after\napprox(1.0, abs=0.001)","handlingStrategy":"validation","validationCode":"def validate_abs_tolerance_nonnegative(abs_tol):\n    if abs_tol is not None and abs_tol < 0:\n        raise ValueError(f'absolute tolerance cannot be negative: {abs_tol}')\n    return abs_tol\n\n# usage:\n# abs_tol = validate_abs_tolerance_nonnegative(computed_tolerance)\n# approx(expected, abs=abs_tol)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always use non-negative absolute tolerance values.","If computing tolerance dynamically, apply abs() to avoid sign errors: tol = abs(delta).","Add a guard check before passing computed tolerance to approx()."],"tags":["approx","tolerance","value-error","validation"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}