diff --git a/AUTHORS b/AUTHORS index 33ee644ea68..d9ab9bab855 100644 --- a/AUTHORS +++ b/AUTHORS @@ -288,6 +288,7 @@ lovetheguitar Lukas Bednar Luke Murphy Maciek Fijalkowski +Madan Kumar Maggie Chung Maho Maik Figura diff --git a/changelog/14754.bugfix.rst b/changelog/14754.bugfix.rst new file mode 100644 index 00000000000..08b4a96a297 --- /dev/null +++ b/changelog/14754.bugfix.rst @@ -0,0 +1 @@ +``pytest.approx()`` now rejects infinite relative tolerances for ``datetime.timedelta`` values with a clear ``ValueError`` instead of raising ``OverflowError``. diff --git a/src/_pytest/approx.py b/src/_pytest/approx.py index 9c5cd86a014..34b329df138 100644 --- a/src/_pytest/approx.py +++ b/src/_pytest/approx.py @@ -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 diff --git a/testing/python/approx.py b/testing/python/approx.py index fd3533592f5..a32693deba4 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -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