pytest-dev/pytest · error · TypeError
absolute tolerance for a scalar value must be an int, float
Error message
absolute tolerance for a scalar value must be an int, float or Decimal, got {type(abs).__name__} What it means
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.
Source
Thrown at src/_pytest/approx.py:460
expected: ExpectedT,
rel: float | Decimal | timedelta | None,
abs: float | Decimal | timedelta | None,
nan_ok: bool,
) -> None:
__tracebackhide__ = True
if rel is not None:
if not isinstance(rel, (int, float, Decimal)):
raise TypeError(
f"relative tolerance for a scalar value must be an int, float or Decimal, "
f"got {type(rel).__name__}"
)
if not isinstance(expected, SupportsAbs):
raise TypeError(
f"expected value must support abs(...) when relative tolerance is used, "
f"got {type(expected).__name__}"
)
if abs is not None and not isinstance(abs, (int, float, Decimal)):
raise TypeError(
f"absolute tolerance for a scalar value must be an int, float or Decimal, "
f"got {type(abs).__name__}"
)
super().__init__(expected, rel=rel, abs=abs, nan_ok=nan_ok)
def __repr__(self) -> str:
"""Return a string communicating both the expected value and the
tolerance for the comparison being made.
For example, ``1.0 ± 1e-6``, ``(3+4j) ± 5e-6 ∠ ±180°``.
"""
# Don't show a tolerance for values that aren't compared using
# tolerances, i.e. non-numerics and infinities. Need to call abs to
# handle complex numbers, e.g. (inf + 1j).
if (
_is_bool(self.expected)
or (not isinstance(self.expected, Complex | Decimal))
or math.isinf(abs(self.expected))View on GitHub (pinned to 98b357f69e)
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')).
Example fix
# before approx(1.0, abs='1e-3') # string # after approx(1.0, abs=1e-3) # float
Defensive patterns
Strategy: type-guard
Validate before calling
from decimal import Decimal
def validate_abs_tolerance(abs_tol):
if abs_tol is not None and not isinstance(abs_tol, (int, float, Decimal)):
raise TypeError(f'abs must be int, float, or Decimal, got {type(abs_tol).__name__}')
return abs_tol Type guard
from decimal import Decimal
def is_valid_abs_tolerance(value) -> bool:
return value is None or isinstance(value, (int, float, Decimal)) Prevention
- 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.
When it happens
Trigger: Calling approx(1.0, abs='0.001') or approx(1.0, abs=1e-3j). The isinstance(abs, (int, float, Decimal)) check fails.
Common situations: Passing a tolerance value from a parsed config string, env var, or JSON without converting to float, or accidentally using a complex number.
Related errors
- relative tolerance for a scalar value must be an int, float
- expected value must support abs(...) when relative tolerance
- absolute tolerance can't be negative: {absolute_tolerance}
- cannot compare '{actual}' to numpy.ndarray
- pytest.approx() does not support nested dictionaries: key={!
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/a3f15e461a1d991a.json.
Report an issue: GitHub.