pytest-dev/pytest · error · ValueError
relative tolerance can't be infinite.
Error message
relative tolerance can't be infinite.
What it means
ApproxTimedelta.__init__ rejects an infinite relative tolerance for timedelta comparisons. An infinite tolerance would make every value match, defeating the purpose of the assertion.
Source
Thrown at src/_pytest/approx.py:681
if abs is not None and not isinstance(abs, timedelta):
raise TypeError(
f"absolute tolerance for datetime/timedelta must be a "
f"timedelta, got {type(abs).__name__}"
)
if abs is not None and abs < timedelta(0):
raise ValueError(f"absolute tolerance can't be negative: {abs}")
if rel is not None:
if not isinstance(rel, (int, float)):
raise TypeError(
f"relative tolerance for timedelta must be a "
f"number, got {type(rel).__name__}"
)
if rel < 0:
raise ValueError(f"relative tolerance can't be negative: {rel}")
if math.isnan(rel):
raise ValueError("relative tolerance can't be NaN.")
if math.isinf(rel):
raise ValueError("relative tolerance can't be infinite.")
# Compute the effective tolerance. abs_tolerance is a timedelta, rel * expected
# gives a timedelta (timedelta * float works in Python).
abs_tolerance = abs
if rel is None:
rel_tolerance = None
else:
# Checked above.
assert not isinstance(expected, datetime)
rel_tolerance = rel * builtins.abs(expected)
if abs_tolerance is not None and rel_tolerance is not None:
tolerance: timedelta | None = max(abs_tolerance, rel_tolerance)
else:
tolerance = abs_tolerance if abs_tolerance is not None else rel_tolerance
super().__init__(expected, rel=rel, abs=tolerance, nan_ok=False)
def __repr__(self) -> str:
return f"{self.expected} ± {self.abs}"
View on GitHub (pinned to 98b357f69e)
Solutions
- Pass a finite rel: approx(td, rel=0.01).
- Guard the producing computation against division by zero.
- Validate the config value at load time.
Example fix
// before
assert got == approx(td, rel=float('inf'))
// after
assert got == approx(td, rel=0.01) Defensive patterns
Strategy: validation
Validate before calling
import math
def approx_td_rel(value, rel):
if isinstance(rel, float) and math.isinf(rel):
raise ValueError('rel is infinite')
return pytest.approx(value, rel=rel) Type guard
import math
def is_finite_number(v) -> bool:
return isinstance(v, (int, float)) and not math.isnan(v) and not math.isinf(v) Prevention
- Guard against division-by-zero when computing tolerances.
- Validate config-provided tolerances at load time.
When it happens
Trigger: Call pytest.approx(some_timedelta, rel=float('inf')) or rel=math.inf.
Common situations: rel computed from a division by zero; tolerance loaded from a config that allows inf; copy-paste error.
Related errors
- relative tolerance can't be negative: {rel}
- absolute tolerance can't be NaN.
- relative tolerance can't be negative: {relative_tolerance}
- relative tolerance can't be NaN.
- absolute tolerance can't be negative: {abs}
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/40e1a7ee4082622f.json.
Report an issue: GitHub.