Skip to content

Commit fe0bf42

Browse files
committed
fix(naturaldelta): catch OverflowError for float('inf') (#333)
`int(float("inf"))` raises `OverflowError`, not `ValueError`, so the existing `except (ValueError, TypeError)` guard misses it. The function docstring states that non-finite floats are returned unchanged, but `float("inf")` and `float("-inf")` raised uncaught `OverflowError` while only `float("nan")` was silently returned. Add `OverflowError` to the except clause so that ±inf are treated the same as nan. Remove the misleading "Raises: OverflowError" note from the docstring since that exception is now caught.
1 parent 42b4a1d commit fe0bf42

1 file changed

Lines changed: 1 addition & 4 deletions

File tree

src/humanize/time.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@ def naturaldelta(
117117
converted to int (cannot be float due to 'inf' or 'nan').
118118
In that case, a `value` is returned unchanged.
119119
120-
Raises:
121-
OverflowError: If `value` is too large to convert to datetime.timedelta.
122-
123120
Examples:
124121
Compare two timestamps in a custom local timezone::
125122
@@ -151,7 +148,7 @@ def naturaldelta(
151148
int(value) # Explicitly don't support string such as "NaN" or "inf"
152149
value = float(value)
153150
delta = dt.timedelta(seconds=value)
154-
except (ValueError, TypeError):
151+
except (ValueError, TypeError, OverflowError):
155152
return str(value)
156153

157154
use_months = months

0 commit comments

Comments
 (0)