Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ lovetheguitar
Lukas Bednar
Luke Murphy
Maciek Fijalkowski
Madan Kumar
Maggie Chung
Maho
Maik Figura
Expand Down
1 change: 1 addition & 0 deletions changelog/14754.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``pytest.approx()`` now rejects infinite relative tolerances for ``datetime.timedelta`` values with a clear ``ValueError`` instead of raising ``OverflowError``.
2 changes: 2 additions & 0 deletions src/_pytest/approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,8 @@ def __init__(
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
Expand Down
6 changes: 6 additions & 0 deletions testing/python/approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,12 @@ def test_timedelta_rel_must_not_be_nan(self):
with pytest.raises(ValueError, match="relative tolerance can't be NaN"):
approx(timedelta(seconds=1), rel=float("nan"))

def test_timedelta_rel_must_not_be_infinite(self):
from datetime import timedelta

with pytest.raises(ValueError, match="relative tolerance can't be infinite"):
approx(timedelta(seconds=1), rel=inf)

def test_timedelta_abs_must_be_non_negative(self):
from datetime import timedelta

Expand Down