fix: handle infinite relative tolerance for timedelta values#14754
Open
winklemad wants to merge 3 commits into
Open
fix: handle infinite relative tolerance for timedelta values#14754winklemad wants to merge 3 commits into
winklemad wants to merge 3 commits into
Conversation
Pierre-Sassoulas
requested changes
Jul 22, 2026
Pierre-Sassoulas
left a comment
Member
There was a problem hiding this comment.
Thank you for working on this ! I'm curious about what others expect from pytest in this case. Silentely succeed, or raise ? And from their linter ? Probably raise a warning as it's a constant condition or an underhanded way to check typing.
| rel_tolerance = None | ||
| elif math.isinf(rel): | ||
| if expected == timedelta(0): | ||
| raise ValueError("relative tolerance can't be NaN.") |
Member
There was a problem hiding this comment.
Can't be infinite ? Should it be be line 679 with the other sanity guards checks ? This value can be infinite but it doesn't make a lot of sense to test this. (Like an assert True) Maybe warning for this is a linter job and not pytest though.
nicoddemus
approved these changes
Jul 23, 2026
nicoddemus
reviewed
Jul 23, 2026
| @@ -0,0 +1 @@ | |||
| ``pytest.approx()`` now rejects infinite relative tolerances for ``datetime.timedelta`` values with a clear ``ValueError`` instead of raising ``OverflowError``. | |||
Member
There was a problem hiding this comment.
I think this should be an improvement rather than a bugfix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pytest.approx()multiplies atimedeltaby the relative tolerance while constructing the comparison. Whenrel=float("inf"), Python raisesOverflowErrorbecausetimedeltahas no infinite representation.Reject infinite relative tolerances alongside the existing negative and NaN validation, producing a clear
ValueErrorbefore multiplication. Numeric approximate-comparison behavior remains unchanged.Validation
tox -e py312 -- testing/python/approx.py::TestApproxDatetime::test_timedelta_rel_must_not_be_infinitetox -e py312 -- testing/python/approx.pytox -e py312tox -e lintinggit diff --check