Skip to content

fix: handle infinite relative tolerance for timedelta values - #14754

Merged
Pierre-Sassoulas merged 5 commits into
pytest-dev:mainfrom
winklemad:fix-timedelta-infinite-relative-tolerance
Aug 2, 2026
Merged

fix: handle infinite relative tolerance for timedelta values#14754
Pierre-Sassoulas merged 5 commits into
pytest-dev:mainfrom
winklemad:fix-timedelta-infinite-relative-tolerance

Conversation

@winklemad

@winklemad winklemad commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Summary

pytest.approx() multiplies a timedelta by the relative tolerance while constructing the comparison. When rel=float("inf"), Python raises OverflowError because timedelta has no infinite representation.

Reject infinite relative tolerances alongside the existing negative and NaN validation, producing a clear ValueError before multiplication. Numeric approximate-comparison behavior remains unchanged.

Validation

  • tox -e py312 -- testing/python/approx.py::TestApproxDatetime::test_timedelta_rel_must_not_be_infinite
  • tox -e py312 -- testing/python/approx.py
  • tox -e py312
  • tox -e linting
  • git diff --check

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jul 22, 2026

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/_pytest/approx.py Outdated
@@ -0,0 +1 @@
``pytest.approx()`` now rejects infinite relative tolerances for ``datetime.timedelta`` values with a clear ``ValueError`` instead of raising ``OverflowError``.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be an improvement rather than a bugfix.

Samielakkad

This comment was marked as resolved.

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@winklemad

Copy link
Copy Markdown
Contributor Author

Renamed it to 14754.improvement.rst — thanks!

@winklemad

Copy link
Copy Markdown
Contributor Author

Heads up: the ubuntu-py315 red is TestPDB::test_pdb_unittest_postmortem timing out in pexpect (1 failed, 4331 passed) — looks like the usual pdb flake rather than anything from this branch, which only renamed the changelog file. Happy to rebase if you'd rather see it green first.

@Pierre-Sassoulas
Pierre-Sassoulas enabled auto-merge (squash) August 2, 2026 06:18
@Pierre-Sassoulas
Pierre-Sassoulas merged commit 224e9ef into pytest-dev:main Aug 2, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants