Skip to content

Add datetime/timedelta support to pytest.approx (#8395)#14414

Open
hamza-mobeen wants to merge 2 commits intopytest-dev:mainfrom
hamza-mobeen:issue-8395-approx-datetime
Open

Add datetime/timedelta support to pytest.approx (#8395)#14414
hamza-mobeen wants to merge 2 commits intopytest-dev:mainfrom
hamza-mobeen:issue-8395-approx-datetime

Conversation

@hamza-mobeen
Copy link
Copy Markdown

Closes #8395

Description

This PR introduces native support for comparing datetime.datetime and datetime.timedelta objects using pytest.approx().
Following the guidelines discussed in #8395:

  • An explicit absolute tolerance (abs) must be provided as a datetime.timedelta object. We intentionally avoid providing default tolerances as time comparisons lack sane defaults.
  • Relative tolerances (rel) are not supported for time comparisons and will raise a TypeError with an informative message.
  • nan_ok is also explicitly rejected for time comparisons.
  • Implemented ApproxTimedelta as an internal helper class to manage this specific comparison logic.
  • Included test coverage in testing/python/approx.py ensuring there are no regressions with existing numeric approximation behavior.

Usage Example

>>> from datetime import datetime, timedelta
>>> dt1 = datetime(2024, 1, 1, 12, 0, 0)
>>> dt2 = datetime(2024, 1, 1, 12, 0, 0, 500000)
>>> dt1 == approx(dt2, abs=timedelta(seconds=1))
True

Example Test Coverage

class TestApproxDatetime:
    # ...
    def test_datetime_error_message(self):
        from datetime import datetime, timedelta
        dt1 = datetime(2024, 1, 1, 12, 0, 0)
        dt2 = datetime(2024, 1, 1, 12, 0, 5)  # 5 seconds off
        with pytest.raises(AssertionError, match="comparison failed"):
            assert dt1 == approx(dt2, abs=timedelta(seconds=1))
    def test_rejects_rel(self):
        from datetime import datetime, timedelta
        with pytest.raises(TypeError, match="does not support relative tolerance"):
            approx(datetime(2024, 1, 1), rel=0.1, abs=timedelta(seconds=1))
    # ... (21 tests in total)

Checklist

  • Include documentation when adding new features.
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits. Please uncheck this if you prefer to squash the commits yourself.
  • Add text like closes #XYZW to the PR description and/or commits (where XYZW is the issue number).
  • If AI agents were used, they are credited in Co-authored-by commit trailers.
  • Create a new changelog file in the changelog directory, with a name like <ISSUE NUMBER>.<TYPE>.rst.
  • Add yourself to AUTHORS in alphabetical order.

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Apr 23, 2026
@hamza-mobeen hamza-mobeen force-pushed the issue-8395-approx-datetime branch from b4ef81a to 23e7a5e Compare April 23, 2026 11:15
Closes pytest-dev#8395

Co-authored-by: Antigravity <antigravity@google.com>
@hamza-mobeen hamza-mobeen force-pushed the issue-8395-approx-datetime branch from 2ea6f2f to e720397 Compare April 23, 2026 11:41
@hamza-mobeen
Copy link
Copy Markdown
Author

Hi @RonnyPfannschmidt ,
This PR is ready for review. Please I would like you take a look when you have a moment, and let me know if you have any feedback or if further adjustments are needed.
Thanks!

@RonnyPfannschmidt
Copy link
Copy Markdown
Member

At first glance this looks good

Im currently a bit stretched and hope to take a closer look next week

@hamza-mobeen
Copy link
Copy Markdown
Author

At first glance this looks good

Im currently a bit stretched and hope to take a closer look next week

no problem thanks.

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.

datetime and timedelta suport for approx

2 participants