{"id":"a3f15e461a1d991a","repo":"pytest-dev/pytest","slug":"absolute-tolerance-for-a-scalar-value-must-be-an-i","errorCode":null,"errorMessage":"absolute tolerance for a scalar value must be an int, float or Decimal, got {type(abs).__name__}","messagePattern":"absolute tolerance for a scalar value must be an int, float or Decimal, got (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/approx.py","lineNumber":460,"sourceCode":"        expected: ExpectedT,\n        rel: float | Decimal | timedelta | None,\n        abs: float | Decimal | timedelta | None,\n        nan_ok: bool,\n    ) -> None:\n        __tracebackhide__ = True\n        if rel is not None:\n            if not isinstance(rel, (int, float, Decimal)):\n                raise TypeError(\n                    f\"relative tolerance for a scalar value must be an int, float or Decimal, \"\n                    f\"got {type(rel).__name__}\"\n                )\n            if not isinstance(expected, SupportsAbs):\n                raise TypeError(\n                    f\"expected value must support abs(...) when relative tolerance is used, \"\n                    f\"got {type(expected).__name__}\"\n                )\n        if abs is not None and not isinstance(abs, (int, float, Decimal)):\n            raise TypeError(\n                f\"absolute tolerance for a scalar value must be an int, float or Decimal, \"\n                f\"got {type(abs).__name__}\"\n            )\n        super().__init__(expected, rel=rel, abs=abs, nan_ok=nan_ok)\n\n    def __repr__(self) -> str:\n        \"\"\"Return a string communicating both the expected value and the\n        tolerance for the comparison being made.\n\n        For example, ``1.0 ± 1e-6``, ``(3+4j) ± 5e-6 ∠ ±180°``.\n        \"\"\"\n        # Don't show a tolerance for values that aren't compared using\n        # tolerances, i.e. non-numerics and infinities. Need to call abs to\n        # handle complex numbers, e.g. (inf + 1j).\n        if (\n            _is_bool(self.expected)\n            or (not isinstance(self.expected, Complex | Decimal))\n            or math.isinf(abs(self.expected))","sourceCodeStart":442,"sourceCodeEnd":478,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/approx.py#L442-L478","documentation":"When constructing an ApproxScalar, the abs (absolute tolerance) parameter must be None or an int, float, or Decimal. If abs is any other type (string, complex, etc.), pytest raises TypeError in __init__. This validates the tolerance type before any comparison occurs.","triggerScenarios":"Calling approx(1.0, abs='0.001') or approx(1.0, abs=1e-3j). The isinstance(abs, (int, float, Decimal)) check fails.","commonSituations":"Passing a tolerance value from a parsed config string, env var, or JSON without converting to float, or accidentally using a complex number.","solutions":["Convert abs to a Python float: approx(1.0, abs=float(value)).","Pass abs as a plain numeric literal: approx(1.0, abs=0.001).","Use Decimal explicitly if needed: approx(Decimal('1.0'), abs=Decimal('0.001'))."],"exampleFix":"# before\napprox(1.0, abs='1e-3')  # string\n\n# after\napprox(1.0, abs=1e-3)  # float","handlingStrategy":"type-guard","validationCode":"from decimal import Decimal\n\ndef validate_abs_tolerance(abs_tol):\n    if abs_tol is not None and not isinstance(abs_tol, (int, float, Decimal)):\n        raise TypeError(f'abs must be int, float, or Decimal, got {type(abs_tol).__name__}')\n    return abs_tol","typeGuard":"from decimal import Decimal\n\ndef is_valid_abs_tolerance(value) -> bool:\n    return value is None or isinstance(value, (int, float, Decimal))","tryCatchPattern":null,"preventionTips":["Always pass abs as a plain float literal or variable.","Convert config/string-sourced tolerance values with float() before passing to approx().","Use Decimal type consistently if working with Decimal expected values."],"tags":["approx","type-error","tolerance","validation"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}