Return non-finite floats unchanged from naturaldelta - #334
Merged
hugovk merged 7 commits intoAug 29, 2026
Conversation
naturaldelta() documents that values it cannot convert are returned
unchanged, and float('nan') already is. But float('inf')/float('-inf')
raised an uncaught OverflowError from int(value), instead of being returned
unchanged like nan.
Catch OverflowError and return non-finite floats unchanged. A too-large
*finite* value, whose OverflowError comes from timedelta(), is still raised,
preserving the documented OverflowError contract.
Closes python-humanize#333
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #334 +/- ##
=======================================
Coverage 99.56% 99.56%
=======================================
Files 12 12
Lines 916 927 +11
=======================================
+ Hits 912 923 +11
Misses 4 4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
hugovk
approved these changes
Jun 30, 2026
hugovk
left a comment
Member
There was a problem hiding this comment.
Thanks, just a couple of suggestions.
This was referenced Jul 2, 2026
Closed
hugovk
reviewed
Aug 29, 2026
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
for more information, see https://pre-commit.ci
hugovk
enabled auto-merge (squash)
August 29, 2026 17:24
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
Closes #333.
naturaldelta()'s docstring says values it cannot convert to atimedeltaare returned unchanged, andfloat("nan")already is. Butfloat("inf")/float("-inf")raised an uncaughtOverflowError(fromint(value)) instead of being returned unchanged likenan:Fix
Catch
OverflowErrorand return the value unchanged only when it is non-finite. A too-large finite value — whoseOverflowErrorcomes fromtimedelta(seconds=...)— is still re-raised, preserving the documentedRaises: OverflowErrorcontract:Tests
test_naturaldelta_non_finite— assertsnan/inf/-infreturn"nan"/"inf"/"-inf".test_naturaldelta_too_large_value_raises— locks in that a too-large finite value (1e30) still raisesOverflowError.Verified locally:
pytest tests/test_time.py→ 385 passed;ruff checkclean.