| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
There's already an approved PR for this: #334. What's wrong with that PR? What does this PR add or do better? |
Sorry, something went wrong.
for more information, see https://pre-commit.ci
… -st, -nd, -rd, -th
…) returns unchanged
The docstring says naturaldelta returns non-number input unchanged. int(float('inf')) raises OverflowError (not ValueError or TypeError), so the existing except (ValueError, TypeError) clause let it escape.
The two new test cases (float('inf') and float('-inf')) fail on the pre-fix code with OverflowError and pass once OverflowError is added to the except tuple. The existing 'NaN' string case still returns "NaN" because str('NaN') is 'NaN' and float('NaN') → nan → int(nan) still raises ValueError which is still caught.
for more information, see https://pre-commit.ci
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Fixes #333.
naturaldelta() documents that a non-finite float value (e.g. NaN, inf) is "returned unchanged." The previous code wrapped the int(value) probe in except (ValueError, TypeError), which catches NaN (int(float('nan')) raises ValueError) but misses float('inf') and float('-inf') — those raise OverflowError from the same conversion. The result was an uncaught OverflowError: cannot convert float infinity to integer for humanize.naturaldelta(float('inf')), in contradiction with the docstring.
Adding OverflowError to the caught tuple restores the documented behaviour: naturaldelta(float('inf')) now returns 'inf' and naturaldelta(float('-inf')) returns '-inf', symmetric with the existing NaN case.
Two new parametrized cases (float("inf") and float("-inf")) on the existing test_naturaldelta exercise the issue. They fail on the pre-fix code with the exact OverflowError reported in #333 and pass with the fix. The math.isfinite(test_input) guard on the existing naturaldelta(-test_input) symmetry assertion is needed because -float('inf') is float('-inf') (and vice versa), so negating and re-formatting would not round-trip.
Verified with python3 -m pytest tests/: 727 passed, 69 skipped (unrelated).