| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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. @@ Coverage Diff @@
## main #334 +/- ##
=======================================
Coverage 99.56% 99.56%
=======================================
Files 12 12
Lines 913 925 +12
=======================================
+ Hits 909 921 +12
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.
|
Sorry, something went wrong.
Merging this PR will not alter performance✅ 15 untouched benchmarks Comparing uttam12331:fix/333-naturaldelta-inf (99e251f) with main (3c577d7) |
Sorry, something went wrong.
There was a problem hiding this comment.
Thanks, just a couple of suggestions.
Sorry, something went wrong.
| def test_naturaldelta_non_finite() -> None: | ||
| """Non-finite floats are returned unchanged instead of raising (#333).""" | ||
| assert humanize.naturaldelta(float("nan")) == "nan" | ||
| assert humanize.naturaldelta(float("inf")) == "inf" | ||
| assert humanize.naturaldelta(float("-inf")) == "-inf" |
There was a problem hiding this comment.
Let's parameterise:
| def test_naturaldelta_non_finite() -> None: | |
| """Non-finite floats are returned unchanged instead of raising (#333).""" | |
| assert humanize.naturaldelta(float("nan")) == "nan" | |
| assert humanize.naturaldelta(float("inf")) == "inf" | |
| assert humanize.naturaldelta(float("-inf")) == "-inf" | |
| @pytest.mark.parametrize("value, expected", [ | |
| (float("nan"), "nan"), (float("inf"), "inf"), (float("-inf"), "-inf"), | |
| ]) | |
| def test_naturaldelta_non_finite() -> None: | |
| """Non-finite floats are returned unchanged instead of raising.""" | |
| assert humanize.naturaldelta(value) == expected |
Sorry, something went wrong.
|
|
||
| from __future__ import annotations | ||
|
|
||
| import math |
There was a problem hiding this comment.
Let's defer this so it's not imported if not needed.
Please move it inside the except OverflowError: block.
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Summary
Closes #333.
naturaldelta()'s docstring says values it cannot convert to a timedelta 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:
Fix
Catch OverflowError and return the value unchanged only when it is non-finite. A too-large finite value — whose OverflowError comes from timedelta(seconds=...) — is still re-raised, preserving the documented Raises: OverflowError contract:
Tests
Verified locally: pytest tests/test_time.py → 385 passed; ruff check clean.